Got Questions? Get Answers.
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

Thread Subject:
non-linear least square method - lsqcurvefit

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 9 Apr, 2011 01:00:21

Message: 1 of 20

i'm trying to fit a curve using the lsqcurvefit.

I have experimental data over the time.
I need to estimate the following parameters

f(1), f(2), f(3), f(4) and M.
I have a constante k = 9.8

I established this initial guess parameters
x0=[1,1,1,1,1];

I call the function
f=lsqcurvefit(@myfun, x0, t, F);

And I get this
f (t) = exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + M*9.8

If I dont use the last parameteres (+ M * 9.8 ) the fit is ok but when I add this I get problems. This is possible to performe with this function? If yes can any one help me please? If not There is any function that I can you to estimate all of this 5 parameters?

chears,

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 9 Apr, 2011 12:54:05

Message: 2 of 20

Please can anyone help me I am really need to solve this.

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 9 Apr, 2011 14:42:32

Message: 3 of 20

On 4/8/2011 8:00 PM, Armindo wrote:
> i'm trying to fit a curve using the lsqcurvefit.
>
> I have experimental data over the time. I need to estimate the following
> parameters
> f(1), f(2), f(3), f(4) and M. I have a constante k = 9.8
>
> I established this initial guess parameters
> x0=[1,1,1,1,1];
> I call the function f=lsqcurvefit(@myfun, x0, t, F);
>
> And I get this f (t) =
> exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + M*9.8
> If I dont use the last parameteres (+ M * 9.8 ) the fit is ok but when I
> add this I get problems. This is possible to performe with this
> function? If yes can any one help me please? If not There is any
> function that I can you to estimate all of this 5 parameters?

That's essentially estimating a constant.

How many DOF do you have (ie, data points)?

Does the data when plotted show that a constant would be useful? It
would seem otherwise by the functional form it should decay to zero in
which case trying to force a baseline constant could indeed be
counterproductive (and quite probably meaningless)...

--

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 9 Apr, 2011 16:05:05

Message: 4 of 20

HI,

How many DOF do you have (ie, data points)?

Depends but I should have at least 2000

Does the data when plotted show that a constant would be useful? It
would seem otherwise by the functional form it should decay to zero in
which case trying to force a baseline constant could indeed be
counterproductive (and quite probably meaningless)...

Let say that data begin (between 200 and 300 N) and finish (between 200 and 300 N) and I want to determine is this value. I can approximate this by doing (F - mean(F)) and I put the data beginning and finishing in zero ( I have been doing this and then i apply the lsqcurvefit). The M* 9.81 will be this value. However I need to estimate this value. Is possible using this function?

chears

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 9 Apr, 2011 17:55:18

Message: 5 of 20

On 4/9/2011 11:05 AM, Armindo wrote:
> HI,
>
> How many DOF do you have (ie, data points)?
>
> Depends but I should have at least 2000
> Does the data when plotted show that a constant would be useful? It
> would seem otherwise by the functional form it should decay to zero in
> which case trying to force a baseline constant could indeed be
> counterproductive (and quite probably meaningless)...
>
> Let say that data begin (between 200 and 300 N) and finish (between 200
> and 300 N) and I want to determine is this value. I can approximate this
> by doing (F - mean(F)) and I put the data beginning and finishing in
> zero ( I have been doing this and then i apply the lsqcurvefit). The M*
> 9.81 will be this value. However I need to estimate this value. Is
> possible using this function?
...

OK, thought perhaps you were trying to estimate parameters being short
on points--it's amazing how many times we see that here...

I call the function f=lsqcurvefit(@myfun, x0, t, F);

And I get this f (t) =
exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + M*9.8

The biggest problem probably is that "M" isn't in the parameter vector f
in the function definition (which is confused nomenclater w/ lowercase f
as the response. This doesn't seem to be workable; I'm guessing you
need something like

y = exp(-c(1).*t).*(c(2).*cos(c(3).*t)+c(4).*sin(c(3).*t)) + c(5)*9.8;

as your function in your 'myfun' routine where I've use c() as estimated
coefficients and y as the response.

--

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 9 Apr, 2011 22:13:05

Message: 6 of 20

dpb <none@non.net> wrote in message <inq6i4$72g$1@speranza.aioe.org>...
> On 4/9/2011 11:05 AM, Armindo wrote:
> > HI,
> >
> > How many DOF do you have (ie, data points)?
> >
> > Depends but I should have at least 2000
> > Does the data when plotted show that a constant would be useful? It
> > would seem otherwise by the functional form it should decay to zero in
> > which case trying to force a baseline constant could indeed be
> > counterproductive (and quite probably meaningless)...
> >
> > Let say that data begin (between 200 and 300 N) and finish (between 200
> > and 300 N) and I want to determine is this value. I can approximate this
> > by doing (F - mean(F)) and I put the data beginning and finishing in
> > zero ( I have been doing this and then i apply the lsqcurvefit). The M*
> > 9.81 will be this value. However I need to estimate this value. Is
> > possible using this function?
> ...
>
> OK, thought perhaps you were trying to estimate parameters being short
> on points--it's amazing how many times we see that here...
>
> I call the function f=lsqcurvefit(@myfun, x0, t, F);
>
> And I get this f (t) =
> exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + M*9.8
>
> The biggest problem probably is that "M" isn't in the parameter vector f
> in the function definition (which is confused nomenclater w/ lowercase f
> as the response. This doesn't seem to be workable; I'm guessing you
> need something like
>
> y = exp(-c(1).*t).*(c(2).*cos(c(3).*t)+c(4).*sin(c(3).*t)) + c(5)*9.8;
>
> as your function in your 'myfun' routine where I've use c() as estimated
> coefficients and y as the response.
>

nop. I may have explain me wrong but the M is in the vector so is like this.
f (t) = exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + f(5)*9.8

when I do this I shoulg get from M =f(5) about 20 (if the inicial base line was in 200N) however I get strange values and of about - 0.12 or 1.2

 

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 9 Apr, 2011 22:50:36

Message: 7 of 20

On 4/9/2011 5:13 PM, Armindo wrote:
...

> nop. I may have explain me wrong but the M is in the vector so is like
> this.
> f (t) = exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + f(5)*9.8
...

Why do you have the coefficients mixed w/ the same variable name as the
response?

Also, if you're trying to use an OLS routine as it sounds like
lsqcurvefit _might_ be, it isn't linear in the coefficients.

--

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 10 Apr, 2011 01:53:04

Message: 8 of 20

I have experimental data (force over time) from an oscillatory movement (underdamped).

The solution for this have the form:

F (t) = exp(-gama*t)*(A* cos *omega*t + B* sin *omega*t) + Mg

This parameters (gama, A, B, omega and Mg) can be estimated using a non-linear least square method.

So I am trying to estimate these parameters by the non-linear least square method using the function lsqcurvefit.


What I have done was:

t = timedata
F = forcedata

Considering that
f(1) = gama;
f(2) = A
f(3) = omega
f(4) and B
f(5) = M
g = constant 9.81

I established this initial guess parameters
x0=[1,1,1,1,1];

I call the function
f=lsqcurvefit(@myfun, x0, t, F);

And I get this
f_lsq= exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + f(5) *9.81


If I dont use the last parameteres (+ M * 9.81 ) the fit most of the times is ok but when I add this I get problems. This function (lsqcurvefit) is suitable to perform this operation? Or I should use other? Please can anyone help me I really need to solve this issue. If needed, I can send some data to test this procedure.

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 10 Apr, 2011 11:04:04

Message: 9 of 20

I have experimental data (force over time) from an oscillatory movement (underdamped).

The solution for this have the form:

F (t) = exp(-gama*t)*(A* cos *omega*t + B* sin *omega*t) + Mg

This parameters (gama, A, B, omega and Mg) can be estimated using a non-linear least square method.

So I am trying to estimate these parameters by the non-linear least square method using the function lsqcurvefit.


What I have done was:

t = timedata
F = forcedata

Considering that
f(1) = gama;
f(2) = A
f(3) = omega
f(4) and B
f(5) = M
g = constant 9.81

I established this initial guess parameters
x0=[1,1,1,1,1];

I call the function
f=lsqcurvefit(@myfun, x0, t, F);

And I get this
f_lsq= exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + f(5) *9.81


If I dont use the last parameteres (+ M * 9.81 ) the fit most of the times is ok but when I add this I get problems. This function (lsqcurvefit) is suitable to perform this operation? Or I should use other? Please can anyone help me I really need to solve this issue. If needed, I can send some data to test this procedure.

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 10 Apr, 2011 15:02:58

Message: 10 of 20

On 4/9/2011 8:53 PM, Armindo wrote:
> I have experimental data (force over time) from an oscillatory movement
> (underdamped).
> The solution for this have the form:
>
> F (t) = exp(-gama*t)*(A* cos *omega*t + B* sin *omega*t) + Mg
>
> This parameters (gama, A, B, omega and Mg) can be estimated using a
> non-linear least square method.
> So I am trying to estimate these parameters by the non-linear least
> square method using the function lsqcurvefit.
>
> What I have done was:
> t = timedata
> F = forcedata
>
> Considering that f(1) = gama;
> f(2) = A
> f(3) = omega
> f(4) and B
> f(5) = M
> g = constant 9.81
>
> I established this initial guess parameters
> x0=[1,1,1,1,1];
>
> I call the function f=lsqcurvefit(@myfun, x0, t, F);
>
> And I get this f_lsq=
> exp(-f(1).*t).*(f(2).*cos(f(3).*t)+f(4).*sin(f(3).*t)) + f(5) *9.81
>
>
> If I dont use the last parameteres (+ M * 9.81 ) the fit most of the
> times is ok but when I add this I get problems. This function
> (lsqcurvefit) is suitable to perform this operation? Or I should use
> other? Please can anyone help me I really need to solve this issue. If
> needed, I can send some data to test this procedure.

I don't have optimization toolbox so I can't test the actual
implementation...

Reading current doc's it looks like lsqcurvefit is at least potentially
able to solve the problem.

I'd start w/ looking at the initial guesses; I'm guessing the estimates
aren't particularly close to unity for most if any of the parameters;
since it is a nonlinear estimation it isn't purely a direct solution so
may help.

Second, have you looked at the error/diagnostic information returned if
you use the optional return arguments from lsqcurvefit? They may point
you to what's going on.

You might selectively parse the data down to a 20-30 points set that
represents a typical dataset and post it; that should be enough that
could get an idea regarding the data to tell regarding what it looks
like. You could alternatively post a full dataset to one of the free
internet hosting sites for folks to download altho I'd think for this
case a small sample would be sufficient as a quickie.

--

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 10 Apr, 2011 16:06:04

Message: 11 of 20

> I'd start w/ looking at the initial guesses; I'm guessing the estimates
> aren't particularly close to unity for most if any of the parameters;
> since it is a nonlinear estimation it isn't purely a direct solution so
> may help.

I try to changes this parameters but nothing change the values obtained.
>
> Second, have you looked at the error/diagnostic information returned if
> you use the optional return arguments from lsqcurvefit? They may point
> you to what's going on.

Dont seems to give an error...

I add a file with data if need to perform some tests.
I hope that siome one could help and thanks for the support.

The file is here.
http://ifile.it/h1qc3x2/For%20test.xls

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 10 Apr, 2011 18:14:38

Message: 12 of 20

On 4/10/2011 11:06 AM, Armindo wrote:
>> I'd start w/ looking at the initial guesses; I'm guessing the
>> estimates aren't particularly close to unity for most if any of the
>> parameters; since it is a nonlinear estimation it isn't purely a
>> direct solution so may help.
>
> I try to changes this parameters but nothing change the values obtained.
>>
>> Second, have you looked at the error/diagnostic information returned
>> if you use the optional return arguments from lsqcurvefit? They may
>> point you to what's going on.
>
> Dont seems to give an error...

What value is returned for exitflag?

> I add a file with data if need to perform some tests. I hope that siome
> one could help and thanks for the support.
> The file is here.
> http://ifile.it/h1qc3x2/For%20test.xls

I'll try to find some time later; other things press at the moment.

Have you tried alternate solution techniques, constraints on estimated
parameters, etc., etc, etc., ...?

Nonlinear fitting is as much an art as anything...

--

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 10 Apr, 2011 18:17:45

Message: 13 of 20

On 4/10/2011 11:06 AM, Armindo wrote:
...

> The file is here.
> http://ifile.it/h1qc3x2/For%20test.xls

Got message "file removed by user"...

--

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 10 Apr, 2011 18:25:59

Message: 14 of 20

On 4/10/2011 1:17 PM, dpb wrote:
> On 4/10/2011 11:06 AM, Armindo wrote:
> ...
>
>> The file is here.
>> http://ifile.it/h1qc3x2/For%20test.xls
>
> Got message "file removed by user"...

BTW, post plain text file, not a spreadsheet format.

--

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 10 Apr, 2011 19:49:04

Message: 15 of 20

dpb <none@non.net> wrote in message <inssnk$b91$2@speranza.aioe.org>...
> On 4/10/2011 1:17 PM, dpb wrote:
> > On 4/10/2011 11:06 AM, Armindo wrote:
> > ...
> >
> >> The file is here.
> >> http://ifile.it/h1qc3x2/For%20test.xls
> >
> > Got message "file removed by user"...
>
> BTW, post plain text file, not a spreadsheet format.
>
> --

It give an error so I think the file was not ok uploaded and remove it. However mean wile as you suggested I change the initial guess of parameters and the estimation work like a charme. Thank you very much for the help. With this I have solved half of the problem now I need to performe another nonlinear estimation. But I dont know how to do it because it give me an error and Iam newbie in matlab.

Since I dont have right now experimental data I create some points doing this:
f = (100:100:800)
k = [80,150,210,260,300, 330, 350, 360];

the function is k = (h * g *f)/(h + g*f)

I need to estimate h and g but I get this error.

Error using ==> lsqcurvefit at 253
Function value and YDATA sizes are incommensurate.

Can some one help me please.

chears and many thanks for the help given until now.

Subject: non-linear least square method - lsqcurvefit

From: dpb

Date: 10 Apr, 2011 20:25:07

Message: 16 of 20

On 4/10/2011 2:49 PM, Armindo wrote:
> dpb <none@non.net> wrote in message <inssnk$b91$2@speranza.aioe.org>...
>> On 4/10/2011 1:17 PM, dpb wrote:
>> > On 4/10/2011 11:06 AM, Armindo wrote:
>> > ...
>> >
>> >> The file is here.
>> >> http://ifile.it/h1qc3x2/For%20test.xls
>> >
>> > Got message "file removed by user"...
>>
>> BTW, post plain text file, not a spreadsheet format.
>>
>> --
>
> It give an error so I think the file was not ok uploaded and remove it.
> However mean wile as you suggested I change the initial guess of
> parameters and the estimation work like a charme. Thank you very much
> for the help. With this I have solved half of the problem now I need to
> performe another nonlinear estimation. But I dont know how to do it
> because it give me an error and Iam newbie in matlab.
> Since I dont have right now experimental data I create some points doing
> this:
> f = (100:100:800)
> k = [80,150,210,260,300, 330, 350, 360];
>
> the function is k = (h * g *f)/(h + g*f)
>
> I need to estimate h and g but I get this error.
> Error using ==> lsqcurvefit at 253
> Function value and YDATA sizes are incommensurate.
...

you didn't vectorize the function (at least as you wrote it above).

Use the debugger and look at

size(k)
size(whatever_you_passed_as_YDATA)

(although again, can't tell but did you use 'k' as a variable for two
separate things or are they in the function subroutine and the workspace
so not in same scope? Either way, makes it hard to tell what you've done...

--

--

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 10 Apr, 2011 21:14:05

Message: 17 of 20

What I have done:

f = (100:100:800)';
k = [80,150,210,260,300, 330, 350, 360]';

% initial guess of parameters
x0=[1,1];

%least squares curve fit of parameters to function myfun1
x=lsqcurvefit(@myfun1,x0,f,k);

K_lsq = (x(1).*x(2).*f)/(x(1)+ x(2).*f)

in myfun1 i have
K = (x(1).*x(2).*xdata)/(x(1)+ x(2).*xdata)

always get the error:

Error using ==> lsqcurvefit at 253
Function value and YDATA sizes are incommensurate.

after make whos I get
Name Size Bytes Class Attributes

  f 8x1 64 double
  k 8x1 64 double
  x0 1x2 16 double

Subject: non-linear least square method - lsqcurvefit

From: Miroslav Balda

Date: 10 Apr, 2011 21:34:05

Message: 18 of 20

"Armindo" wrote in message <int6it$23j$1@fred.mathworks.com>...
> What I have done:
>
> f = (100:100:800)';
> k = [80,150,210,260,300, 330, 350, 360]';
>
> % initial guess of parameters
> x0=[1,1];
>
> %least squares curve fit of parameters to function myfun1
> x=lsqcurvefit(@myfun1,x0,f,k);

SNIP

Hi Armindo,

I have tried to test your problem on my function LMFnlsq
     www.mathworks.com/matlabcentral/fileexchange/17534
The testing script uses function inp.m:
     www.mathworks.com/matlabcentral/fileexchange/9033
There is the script:

% Armindo.m 2011.04.10
%%%%%%%%%%%

f = (100:100:800)';
k = [80,150,210,260,300, 330, 350, 360]';
% k = (h * g *f)/(h + g*f)

res = @(x) (a(1)*a(2)*x(1)*x(2)*f)./(a(1)*x(1)+a(2)*x(2)*f)-k;

format short e
while strcmp(inp('Continue','yes'),'yes') % inp.m from FEX
    a = randn(2,1)*1e5
    [x,ssq,cnt] = LMFnlsq(res,[1;1],'Display',-1) % LMFnlsq from FEX
end

res(x)

%====================================
% Solution for the given "a":

% x =
% 7.7912e-002
% -1.1357e+004
% ssq =
% 7.1400e+004
% cnt =
% 4

The solution is stable and is reached in 4-5 iterations, however, for the other muliplier for a ,the solution is different. In this case the sum of squares dropped from 10^7 to 10^4.
The problem is rather stiff, because it can not obtain better ssq. The residuals at the end of the k-range are big, nevertheless, a solution has been reached.
Good luck

Mira

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 10 Apr, 2011 23:06:04

Message: 19 of 20

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <int7od$imk$1@fred.mathworks.com>...
> "Armindo" wrote in message <int6it$23j$1@fred.mathworks.com>...
> > What I have done:
> >
> > f = (100:100:800)';
> > k = [80,150,210,260,300, 330, 350, 360]';
> >
> > % initial guess of parameters
> > x0=[1,1];
> >
> > %least squares curve fit of parameters to function myfun1
> > x=lsqcurvefit(@myfun1,x0,f,k);
>
> SNIP
>
> Hi Armindo,
>
> I have tried to test your problem on my function LMFnlsq
> www.mathworks.com/matlabcentral/fileexchange/17534
> The testing script uses function inp.m:
> www.mathworks.com/matlabcentral/fileexchange/9033
> There is the script:
>
> % Armindo.m 2011.04.10
> %%%%%%%%%%%
>
> f = (100:100:800)';
> k = [80,150,210,260,300, 330, 350, 360]';
> % k = (h * g *f)/(h + g*f)
>
> res = @(x) (a(1)*a(2)*x(1)*x(2)*f)./(a(1)*x(1)+a(2)*x(2)*f)-k;
>
> format short e
> while strcmp(inp('Continue','yes'),'yes') % inp.m from FEX
> a = randn(2,1)*1e5
> [x,ssq,cnt] = LMFnlsq(res,[1;1],'Display',-1) % LMFnlsq from FEX
> end
>
> res(x)
>
> %====================================
> % Solution for the given "a":
>
> % x =
> % 7.7912e-002
> % -1.1357e+004
> % ssq =
> % 7.1400e+004
> % cnt =
> % 4
>
> The solution is stable and is reached in 4-5 iterations, however, for the other muliplier for a ,the solution is different. In this case the sum of squares dropped from 10^7 to 10^4.
> The problem is rather stiff, because it can not obtain better ssq. The residuals at the end of the k-range are big, nevertheless, a solution has been reached.
> Good luck
>
> Mira

thank you for the help.
Please can you explain me what is the a? and why you subtract k?
How can I see the fit? I try to plot but it give an error also

Additionally Iam geting the following error:

??? Undefined function or method 'a' for input arguments of type 'double'.

Error in ==> @(x)(a(1)*a(2)*x(1)*x(2)*f)./(a(1)*x(1)+a(2)*x(2)*f)-k

Error in ==> LMFnlsq at 251
r = feval(FUN,x);

Subject: non-linear least square method - lsqcurvefit

From: Armindo

Date: 11 Apr, 2011 15:07:05

Message: 20 of 20

Thank you all.

Problem solved.

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