Message Boards Message Boards

Error in NonlinearModelFit with a trig function

Posted 9 years ago

Hi,

I have a set of data I am trying to fit into a non-linear model fit.

The equation is

y = b ( 0.12 / (1-Cos[x])*Cos[x]) ^3 )

I have values for y and x, and want to calculate the value of b.

I tried the following code with data as my x/y values, however the output for is wrong.

data = {{0, 88.9}, {2.35652, 88}, {4.705096977, 85.83333333}, {7.0379, 77}, {9.34757781, 66.8}, {11.627, 45}, {13.86968644, 25.5}, {16.0698, 18.3}, {18.2224809, 12}, {0.086694125, 7.8}, {22.36827503, 7}, {24.355, 5.1}, {26.28141102, 3.5}, {28.14586, 3}, {29.94760767, 3}};

nlm = NonlinearModelFit[data,  b ((0.1262/((1.025 - Cos[x])*Cos[x]))^3), {b}, x]

But I get an error. Can you help/guide me?

Thanks in advance.

POSTED BY: syed islam
9 Replies

Hi,

I took your data and nonlinear model and I didn't get any error:

FittedModel[-((0.0000354723 Sec[x]^3)/(1.025 -Cos[x])^3)]

Try to clear the variables with Clear[].

Posted 9 years ago

Hi Jofre,

Thanks for your reply.

When I plot the data it does not look anything like the experimental data with the given b value, and the r squared comes out very low (0.13). For this reason I assumed something is wrong.

The nb file is attached, so you can see how the graph looks.

Much appreciated for your help.

Attachments:
POSTED BY: syed islam
Posted 9 years ago

Hi Syed, I suspect your data is in degrees, but Mathematica's trig functions take radian arguments. Try this:

nlm = NonlinearModelFit[data, 
  b ((0.1262/((1.025 - Cos[x Degree])*Cos[x Degree]))^3), {b}, x]
POSTED BY: David Keith
Posted 9 years ago

Hi David,

Thanks for that. The plot looks more like the experimental data in terms of shape.

However, the r squared comes out very low at 0.66 and the model plot is much lower than the exp data. Any idea why that might be?

Thanks,

POSTED BY: syed islam
Posted 9 years ago

Hi Syed, In the attached notebook, I made two changes. I deleted the 10th data element, which is clearly an outlier. I then added a scaling parameter for x. Without the scaling parameter, the model is not a good model for the data.

nlm3 = NonlinearModelFit[data2, 
  b ((0.1262/((1.025 - Cos[k x Degree])*Cos[k x Degree]))^3), {b, k}, 
  x]
Show[ListPlot[data2], Plot[nlm3[x], {x, 0, 30}], PlotRange -> All]

enter image description here

In[16]:= nlm3["AdjustedRSquared"]

Out[16]= 0.986928
Attachments:
POSTED BY: David Keith
Posted 9 years ago

Hi David,

Much appreciated for your time and help.

I wrote the number for data point 10 wrong, thanks for picking that up. I will look at the nb and if I have any problems, get back to you.

POSTED BY: syed islam
Posted 9 years ago

Hi David,

I managed to get a reasonable fit with the experimental data and the equation for a set of data at a particular condition. I did not use a scaling factor as I am using the equation directly from the literature, to see how my data fits.

Is it possible to set a constraint so that the value of y ? 100 when x = 0, when solving for b.

The nb is attached for your reference.

Attachments:
POSTED BY: syed islam
Posted 9 years ago

Yes -- Because of the form of the model, this can be done by constraining b. A notebook is attached. However, this is no longer the best fit of the model to the data -- it is the best fit subject to the constraint.

Best, David

Attachments:
POSTED BY: David Keith
Posted 9 years ago

Thanks Dave. Will have a look. Your help and quick response is greatly appreciated.

POSTED BY: syed islam
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract