Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
Fitting experimental data to an equation using lsqcurvefit

Subject: Fitting experimental data to an equation using lsqcurvefit

From: Michael Naicker

Date: 26 Sep, 2013 19:30:08

Message: 1 of 4

Hi..I am new to Matlab and this forum. Please forgive me if I do anything wrong.The aim of my code is to fit experimental vapour pressure data to the equation shown below. When I run the m file I get the following error messages. I can't seem to figure out what the problem is. Thank you for your help.
..................................................................................................................
Error messages:

??? Error using ==> snls at 259
lsqcurvefit cannot continue: user function is returning Inf or NaN values.

Error in ==> lsqncommon at 149
    [xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...

Error in ==> lsqcurvefit at 258
[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

Error in ==> Untitled at 82
[x,resnorm,~,exitflag,output] = lsqcurvefit(@myfun,x0,xdata,ydata);

.............................................................................................................
Equation in normal form:

 P = Pc*(exp((x(1)*(1-T/Tc)+x(2)*(1-T/Tc).^1.5+x(3)*(1-T/Tc).^3+x(4)*(1-T/Tc).^6)./(1-(1-T/Tc))));

Tc= 282.35;
Pc= 5041.97;


................................................................................................................


Function that I created :

function F = myfun(x,xdata)
Tc= 282.35;
Pc= 5041.97;

 F = Pc*(exp((x(1)*(1-xdata/Tc)+x(2)*(1-xdata/Tc).^1.5+x(3)*(1-xdata/Tc).^3+x(4)*(1-xdata/Tc).^6)./(1-(1-xdata/Tc))));

end


........................................................................................................................
Rest of my code:

xdata=[141.49 %created a column vector of the T(K) data
143.89
146.49
147.92
149.51
152.76
153.27
158.39
162.55
165.34
169.15
171.15
173.42
175.84
179.24
188.569
193.4
201.35
205
215
220
223.15
230
240
247.4
257.2
263.15
274.67
280
281.65
282.3];

ydata=[13.466 %Created column vector of pressure data
16.587
20.589
23.191
26.237
33.596
34.877
50.197
66.207
79.046
99.592
111.724
127.212
145.508
174.027
276.608
343.99
480.29
556.189
805.389
956.753
1062.2
1319.603
1772.392
2173
2799
3239.12
4241.7
4783.343
4963.31
5036.12];



plot(xdata,ydata,'ro')
title('data points')
xlabel('temperature, x'); ylabel('pressure, y');
grid on;

 x0= [ 4000,-2,300,1000 ];


[x,resnorm,~,exitflag,output] = lsqcurvefit(@myfun,x0,xdata,ydata);

hold on
plot(xdata,F(x,xdata))
hold off

Subject: Fitting experimental data to an equation using lsqcurvefit

From: dpb

Date: 26 Sep, 2013 22:17:51

Message: 2 of 4

On 9/26/2013 2:30 PM, Michael Naicker wrote:
...
> ... my code is to fit experimental vapour pressure
> data to the equation shown below. When I run the m file I get the
> following error messages. ...

> Error messages:
>
> ??? Error using ==> snls at 259
> lsqcurvefit cannot continue: user function is returning Inf or NaN values.
>
...

> function F = myfun(x,xdata)
> Tc= 282.35;
> Pc= 5041.97;
>
> F =
> Pc*(exp((x(1)*(1-xdata/Tc)+x(2)*(1-xdata/Tc).^1.5+ ...
     x(3)*(1-xdata/Tc).^3+x(4)*(1-xdata/Tc).^6)./(1-(1-xdata/Tc))));
> end
....

> x0= [ 4000,-2,300,1000 ];
>
> [x,resnorm,~,exitflag,output] = lsqcurvefit(@myfun,x0,xdata,ydata);
>
....

I'm guessing the exp() term is "blowing up" -- yep, plugging in your
values I get

 >> x0= [ 4000,-2,300,1000 ];
 >> x=x0
x =
         4000 -2 300 1000
 >> T=141.49;
 >> P =
Pc*(exp((x(1)*(1-T/Tc)+x(2)*(1-T/Tc).^1.5+x(3)*(1-T/Tc).^3+x(4)*(1-T/Tc).^6)./(1-(1-T/Tc))));
 >> P
P =
    Inf
 >>

Your mission, should you choose to accept it, is to figure out where you
went wrong -- is the exponential term missing a minus sign, perhaps?

--

Subject: Fitting experimental data to an equation using lsqcurvefit

From: dpb

Date: 27 Sep, 2013 13:29:05

Message: 3 of 4

On 9/26/2013 5:17 PM, dpb wrote:
...

>
> I'm guessing the exp() term is "blowing up" -- yep, plugging in your
> values I get
>
> >> x0= [ 4000,-2,300,1000 ];
> >> x=x0;
...
> >> T=141.49;
> >> P =
> Pc*(exp((x(1)*(1-T/Tc)+x(2)*(1-T/Tc).^1.5+x(3)*(1-T/Tc).^3+x(4)*(1-T/Tc).^6)./(1-(1-T/Tc))));
>
> >> P
> P =
> Inf
> >>
>
> Your mission, should you choose to accept it, is to figure out where you
> went wrong -- is the exponential term missing a minus sign, perhaps?
...

ADDENDUM--

Presuming the functional form is correct, it looks like your initial
coefficient estimates are way out of whack -- just computing the
argument for the exponential gives

 >>
(((x(1)*(1-T/Tc)+x(2)*(1-T/Tc).^1.5+x(3)*(1-T/Tc).^3+x(4)*(1-T/Tc).^6)./(1-(1-T/Tc))));

ans =
        4085.9
 >>

So, even if the previous supposition of a missing sign were the problem
with the starting values you'll then just get 0.0 as underflow instead
of Inf.

--

Subject: Fitting experimental data to an equation using lsqcurvefit

From: Michael Naicker

Date: 27 Sep, 2013 14:31:05

Message: 4 of 4

"Michael Naicker" <naicker730@gmail.com> wrote in message <l22200$f66$1@newscl01ah.mathworks.com>...
> Hi..I am new to Matlab and this forum. Please forgive me if I do anything wrong.The aim of my code is to fit experimental vapour pressure data to the equation shown below. When I run the m file I get the following error messages. I can't seem to figure out what the problem is. Thank you for your help.
> ..................................................................................................................
> Error messages:
>
> ??? Error using ==> snls at 259
> lsqcurvefit cannot continue: user function is returning Inf or NaN values.
>
> Error in ==> lsqncommon at 149
> [xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...
>
> Error in ==> lsqcurvefit at 258
> [xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
>
> Error in ==> Untitled at 82
> [x,resnorm,~,exitflag,output] = lsqcurvefit(@myfun,x0,xdata,ydata);
>
> .............................................................................................................
> Equation in normal form:
>
> P = Pc*(exp((x(1)*(1-T/Tc)+x(2)*(1-T/Tc).^1.5+x(3)*(1-T/Tc).^3+x(4)*(1-T/Tc).^6)./(1-(1-T/Tc))));
>
> Tc= 282.35;
> Pc= 5041.97;
>
>
> ................................................................................................................
>
>
> Function that I created :
>
> function F = myfun(x,xdata)
> Tc= 282.35;
> Pc= 5041.97;
>
> F = Pc*(exp((x(1)*(1-xdata/Tc)+x(2)*(1-xdata/Tc).^1.5+x(3)*(1-xdata/Tc).^3+x(4)*(1-xdata/Tc).^6)./(1-(1-xdata/Tc))));
>
> end
>
>
> ........................................................................................................................
> Rest of my code:
>
> xdata=[141.49 %created a column vector of the T(K) data
> 143.89
> 146.49
> 147.92
> 149.51
> 152.76
> 153.27
> 158.39
> 162.55
> 165.34
> 169.15
> 171.15
> 173.42
> 175.84
> 179.24
> 188.569
> 193.4
> 201.35
> 205
> 215
> 220
> 223.15
> 230
> 240
> 247.4
> 257.2
> 263.15
> 274.67
> 280
> 281.65
> 282.3];
>
> ydata=[13.466 %Created column vector of pressure data
> 16.587
> 20.589
> 23.191
> 26.237
> 33.596
> 34.877
> 50.197
> 66.207
> 79.046
> 99.592
> 111.724
> 127.212
> 145.508
> 174.027
> 276.608
> 343.99
> 480.29
> 556.189
> 805.389
> 956.753
> 1062.2
> 1319.603
> 1772.392
> 2173
> 2799
> 3239.12
> 4241.7
> 4783.343
> 4963.31
> 5036.12];
>
>
>
> plot(xdata,ydata,'ro')
> title('data points')
> xlabel('temperature, x'); ylabel('pressure, y');
> grid on;
>
> x0= [ 4000,-2,300,1000 ];
>
>
> [x,resnorm,~,exitflag,output] = lsqcurvefit(@myfun,x0,xdata,ydata);
>
> hold on
> plot(xdata,F(x,xdata))
> hold off
...............................................................................................................
I seem to have sorted out the problem. I changed x0 to ( 1, 1, 1, 0 ). Worked fine after that.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us