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:
Optimization --> Help w/ Non-Linear Least Squares using lsqnonlin

Subject: Optimization --> Help w/ Non-Linear Least Squares using lsqnonlin

From: Pat Finder

Date: 13 Apr, 2008 17:19:02

Message: 1 of 3

[Using student version 7.1.0.124 (R14) SP3]

I am trying to teach myself how to use lsqnonlin. This is
for Nonlinear Least Squares (NLLSQ) regression.

I am confused by the documentation. lsqnonlin is used to
fit a non-linear function to given data.

In my case, I'm trying to fit a Gaussian, which is a
function of the mean and the variance -- two parameters.

The documentation says to write your own function which
takes in the parameters you are trying to estimate:

"function F = myfun(x)
 k= 1:10;
 F = 2 + 2*k-exp(k*x(1))-exp(k*x(2));"

In my case, the vector x would be the values of the mean and
the variance. BUT -- how do I get the data points I'm
trying to fit to in? Do I have to hard-code them into
myfunc(x)?

I don't want to have to write my own function for every data
set I get...

- Pat

Subject: Optimization --> Help w/ Non-Linear Least Squares using lsqnonlin

From: √Člodie

Date: 13 Apr, 2008 17:21:44

Message: 2 of 3

MainProgram.m
--------------------------------
Coeff_init = [1 1];
global data
data.F = ... % give your data
data.k = [1:1:10];
Coeff = lsqnonlin('myoptifunction',Coeff_init);
--------------------------------

myoptifunction.m
---------------------------------
function rest = myoptifunction(Coeff)
global data
rest = data.F - 2 + 2*k-exp(data.k*Coeff(1))-exp(data.k*Coeff(2)) ;
------------------------------------

I hope it helps ...



"Pat Finder" <pfinder.nospam@netacc.net> a écrit dans le message de news:
fttfa6$hgr$1@fred.mathworks.com...
> [Using student version 7.1.0.124 (R14) SP3]
>
> I am trying to teach myself how to use lsqnonlin. This is
> for Nonlinear Least Squares (NLLSQ) regression.
>
> I am confused by the documentation. lsqnonlin is used to
> fit a non-linear function to given data.
>
> In my case, I'm trying to fit a Gaussian, which is a
> function of the mean and the variance -- two parameters.
>
> The documentation says to write your own function which
> takes in the parameters you are trying to estimate:
>
> "function F = myfun(x)
> k= 1:10;
> F = 2 + 2*k-exp(k*x(1))-exp(k*x(2));"
>
> In my case, the vector x would be the values of the mean and
> the variance. BUT -- how do I get the data points I'm
> trying to fit to in? Do I have to hard-code them into
> myfunc(x)?
>
> I don't want to have to write my own function for every data
> set I get...
>
> - Pat
>

Subject: Optimization --> Help w/ Non-Linear Least Squares using lsqnonlin

From: Pat Finder

Date: 15 Apr, 2008 11:46:02

Message: 3 of 3

"?lodie" <elodie.roux@ifrance.com> wrote in message
<480242a4$0$24572$426a74cc@news.free.fr>...
> MainProgram.m
> --------------------------------
> Coeff_init = [1 1];
> global data

Thanks. I didn't know you could declare
global data.

I have it working now.

I did notice though that when I tried to tighten the
tolerances for lsqnonlin, the solution got worse.
I don't have time to investigate now, but I will
mention for others that the default values work
best.

Fortunately, I already know the analytic solution...

Thanks again!

Tags for 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