Fitting and plotting non linear regression in R
<p>I am trying to fit a  non linear function to a given set of data (x and y in code snippet), the function is defined as</p>

<blockquote>
<pre><code>f(x) = a/((sin((x-b)/2))^4)
</code></pre>
</blockquote>

<pre><code>x &lt;- c(0, 5, -5, 10, -10, 15, -15, 20, -20, 25, -25, 30, -30)
y &lt;- c(4.21, 3.73, 2.08, 1.1, 0.61, 0.42, 0.13, 0.1, 0.04, 0.036667, 0.016667, 0.007778, 0.007778)
plot(x,y, log="y")
</code></pre>

<p>This is how the initial graph on which I should fit before mentioned function looks like.</p>

<p><img src="https://i.stack.imgur.com/NdWCu.png" alt="Initial graph"></p>

<p>But when I try to fit using nls and plot the curve, the graph does not look quite right </p>

<pre><code>f &lt;- function(x,a,b) { a/((sin((x-b)/2))^4) }
fitmodel &lt;- nls (y ~ f(x,a,b), start=list(a=1,b=1))
lines(x, predict(fitmodel))
</code></pre>

<p>This is what I see:</p>

<p><img src="https://i.stack.imgur.com/YPZ0n.png" alt="Plotted curve"></p>

<p>I am pretty sure I am doing something wrong here and will appreciate any help from you.</p>