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:
lsqcurvefit-initial guess better than fitting

Subject: lsqcurvefit-initial guess better than fitting

From: HAN ZHOU

Date: 20 Apr, 2012 06:20:27

Message: 1 of 10

Hi there!

I have writing a program to fitting a trigonalmetrical series solution with the form

myfun(X,t) = - X(1)*X(2)*sum of [-exp(i^2*X(2)*t)], i goes from 1 to infinity (I take 100 terms)

however, when I use lsqcurvefit to get X (2 coefficients) the resulting curve was not close to the raw data at all, since the X value it found did not make sense (too far from initial guess, order of magnitude difference)

And I plot the function with my initial guess of X0, it matched even better than the X.

Can anyone shed some light on it (I have been trying this fitting for weeks !!!)

Thanks.

Subject: lsqcurvefit-initial guess better than fitting

From: Torsten

Date: 20 Apr, 2012 06:39:50

Message: 2 of 10

On 20 Apr., 08:20, "HAN ZHOU" <repeatcl...@gmail.com> wrote:
> Hi there!
>
> I have writing a program to fitting a trigonalmetrical series solution with the form
>
> myfun(X,t) = - X(1)*X(2)*sum of [-exp(i^2*X(2)*t)], i goes from 1 to infinity (I take 100 terms)
>
> however, when I use lsqcurvefit to get X (2 coefficients) the resulting curve was not close to the raw data at all, since the X value it found did not make sense (too far from initial guess, order of magnitude difference)
>
> And I plot the function with my initial guess of X0, it matched even better than the X.
>
> Can anyone shed some light on it (I have been trying this fitting for weeks !!!)
>
> Thanks.

1. Use myfun(X,t) = X(1)*sum of [exp(i^2*X(2)*t)]
(If you want your original X(1), divide the X(1) obtained from my
function by X(2)).
2. Constrain X(2) to be negative
3. Don't use i as loop index - i is usually used as the imaginary unit
4. In the evaluation of the infinite sum you should check how far you
have to sum until exp(i^2*X(2)*t) < epsilon

If all this does not help, you should supply the relevant part of your
matlab code.

Best wishes
Torsten.

Subject: lsqcurvefit-initial guess better than fitting

From: HAN ZHOU

Date: 20 Apr, 2012 07:28:08

Message: 3 of 10

Thanks so much!!!
I will try your method and use upper/lower bound and see!!!
Sorry I made a mistake in typing the last message, is actually
myfun(X,t) = - X(1)*X(2)*sum of [exp(-i^2*X(2)*t)]

Subject: lsqcurvefit-initial guess better than fitting

From: Torsten

Date: 20 Apr, 2012 07:38:37

Message: 4 of 10

On 20 Apr., 09:28, "HAN ZHOU" <repeatcl...@gmail.com> wrote:
> Thanks so much!!!
> I will try your method and use upper/lower bound and see!!!
> Sorry I made a mistake in typing the last message, is actually
> myfun(X,t) = - X(1)*X(2)*sum of [exp(-i^2*X(2)*t)]

Then use
myfun(X,t) = -X(1)*sum of [exp(-n^2*X(2)*t]
and restrict X(2) to be positive.

Best wishes
Torsten.

Subject: lsqcurvefit-initial guess better than fitting

From: HAN ZHOU

Date: 20 Apr, 2012 14:52:12

Message: 5 of 10

Hi Torsten!

I tried to fit with 2 coeff as you told me today.
However after the fitting, the resulting X will remain the same with my X0

here is my function
%%%%%%%%%%%%%%%%%%%
function f = myfun(X,Input)
        t = Input{1};
        deltax = Input{2};
        
        f=0;
                for k=1:100
                    inc = -X(1)*deltax*exp(-k^2*X(2)*t);
                    f = f + inc;
                end
    end
%%%%%%%%%%%%%%%%%%%%



And in another script I do the fitting
%%%%%%%%%%%%%%%%%%%%
[X,resnorm] = lsqcurvefit(@myfun,[3e-4 0.5e-5],Input,ydata,[0 0],[4e-4 3e-3])
%%%%%%%%%%%%%%%%%%%%

I get the initial value of X1 X2 by using cftool to fit with only 1 term, i.e.
y = -d1*exp(-d2*t)

Thank you so much for everything !!!
Han






Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <16668383-934e-4e45-a1be-d2585d5666f6@21g2000vbh.googlegroups.com>...
> On 20 Apr., 09:28, "HAN ZHOU" <repeatcl...@gmail.com> wrote:
> > Thanks so much!!!
> > I will try your method and use upper/lower bound and see!!!
> > Sorry I made a mistake in typing the last message, is actually
> > myfun(X,t) = - X(1)*X(2)*sum of [exp(-i^2*X(2)*t)]
>
> Then use
> myfun(X,t) = -X(1)*sum of [exp(-n^2*X(2)*t]
> and restrict X(2) to be positive.
>
> Best wishes
> Torsten.

Subject: lsqcurvefit-initial guess better than fitting

From: Torsten

Date: 21 Apr, 2012 10:29:19

Message: 6 of 10

On 20 Apr., 16:52, "HAN ZHOU" <repeatcl...@gmail.com> wrote:
> Hi Torsten!
>
> I tried to fit with 2 coeff as you told me today.
> However after the fitting, the resulting X will remain the same with my X0
>
> here is my function
> %%%%%%%%%%%%%%%%%%%
> function   f = myfun(X,Input)
>         t = Input{1};
>         deltax = Input{2};
>
>         f=0;
>                 for k=1:100
>                     inc = -X(1)*deltax*exp(-k^2*X(2)*t);
>                     f = f + inc;
>                 end
>     end
> %%%%%%%%%%%%%%%%%%%%
>
> And in another script I do the fitting
> %%%%%%%%%%%%%%%%%%%%
> [X,resnorm] = lsqcurvefit(@myfun,[3e-4 0.5e-5],Input,ydata,[0 0],[4e-4 3e-3])
> %%%%%%%%%%%%%%%%%%%%
>
> I get the initial value of X1 X2 by using cftool to fit with only 1 term, i.e.
> y = -d1*exp(-d2*t)
>
> Thank you so much for everything !!!
> Han
>
> Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <16668383-934e-4e45-a1be-d2585d566...@21g2000vbh.googlegroups.com>...
> > On 20 Apr., 09:28, "HAN ZHOU" <repeatcl...@gmail.com> wrote:
> > > Thanks so much!!!
> > > I will try your method and use upper/lower bound and see!!!
> > > Sorry I made a mistake in typing the last message, is actually
> > > myfun(X,t) = - X(1)*X(2)*sum of [exp(-i^2*X(2)*t)]
>
> > Then use
> > myfun(X,t) = -X(1)*sum of [exp(-n^2*X(2)*t]
> > and restrict X(2) to be positive.
>
> > Best wishes
> > Torsten.

1. Your call to lsqcurvefit and the interpretation of the arguments in
myfun are wrong.
2. At the moment, myfun returns the model y-data for only _one_ data
point to lsqcurvefit.

Look at the example problems for lsqcurvefit in the documentation.

Best wishes
Torsten.

Subject: lsqcurvefit-initial guess better than fitting

From: HAN ZHOU

Date: 22 Apr, 2012 15:36:09

Message: 7 of 10

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <a4fcc76b-acfb-41ce-914c-a0e5cdea1ae6@w5g2000vbp.googlegroups.com>...

> 1. Your call to lsqcurvefit and the interpretation of the arguments in
> myfun are wrong.
> 2. At the moment, myfun returns the model y-data for only _one_ data
> point to lsqcurvefit.
>
> Look at the example problems for lsqcurvefit in the documentation.
>
> Best wishes
> Torsten.

Dear Torsten,

I read the example many times but I could not really see why myfun will return only one model y-data. Could you give me some indication?
Is it because the 2 Input cell have different length?
The tricky point is that in myfun there is a problem-dependent parameter deltax, so I could not use only one input vector - xdata.

Danke Schoen und bis bald!!!

Han

Subject: lsqcurvefit-initial guess better than fitting

From: Torsten

Date: 23 Apr, 2012 06:53:09

Message: 8 of 10

On 22 Apr., 17:36, "Han Zhou" <repeatcl...@gmail.com> wrote:
> Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <a4fcc76b-acfb-41ce-914c-a0e5cdea1...@w5g2000vbp.googlegroups.com>...
> > 1. Your call to lsqcurvefit and the interpretation of the arguments in
> > myfun are wrong.
> > 2. At the moment, myfun returns the model y-data for only _one_ data
> > point to lsqcurvefit.
>
> > Look at the example problems for lsqcurvefit in the documentation.
>
> > Best wishes
> > Torsten.
>
> Dear Torsten,
>
> I read the example many times but I could not really see why myfun will return only one model y-data. Could you give me some indication?
> Is it because the 2 Input cell have different length?
> The tricky point is that in myfun there is a problem-dependent parameter deltax, so I could not use only one input vector - xdata.
>
> Danke Schoen und bis bald!!!
>
> Han

%%%%%%%%%%%%%%%%%%%
function f = myfun(x,xdata,deltax)
        f=0;
        for k=1:100
           f = f -X(1)*deltax.*exp(-k^2*X(2)*xdata);
        end
    end
%%%%%%%%%%%%%%%%%%%%


And in another script I do the fitting
%%%%%%%%%%%%%%%%%%%%
[X,resnorm] = lsqcurvefit(@(x,xdata)myfun(x,xdata,deltax),
[3e-4;0.5e-5],xdata,ydata,[0;0],[4e-4;3e-3])
%%%%%%%%%%%%%%%%%%%%

Best wishes
Torsten.

Subject: lsqcurvefit-initial guess better than fitting

From: Roger Stafford

Date: 23 Apr, 2012 19:11:06

Message: 9 of 10

"HAN ZHOU" <repeatcloud@gmail.com> wrote in message <jmqv7a$sg0$1@newscl01ah.mathworks.com>...
> I have writing a program to fitting a trigonalmetrical series solution with the form
>
> myfun(X,t) = - X(1)*X(2)*sum of [-exp(i^2*X(2)*t)], i goes from 1 to infinity (I take 100 terms)
>
> however, when I use lsqcurvefit to get X (2 coefficients) the resulting curve was not close to the raw data at all, since the X value it found did not make sense (too far from initial guess, order of magnitude difference)
- - - - - - - - - -
  You haven't told us what the range of t is in your 'xdata' vector. The model function you have defined has a singularity at x(2)*t = 0 where it shoots up to infinity, and you need to stay far enough away from that value to enable a good fit of your data to the model. In particular you need to strengthen Torsten's limit to make X(2) greater than or equal to some appropriate positive number (depending on how small t can be.) Otherwise you may get strange results and not a good fit.

Roger Stafford

Subject: lsqcurvefit-initial guess better than fitting

From: HAN ZHOU

Date: 24 Apr, 2012 07:10:08

Message: 10 of 10

@ Roger
Thank you so much for you suggestion. In fact the xdata range is ~0-5000. So I will try to limit x(2) then.


@ Torsten,

Thank you very much for the correction!!! Actually... it still does not fit>_< I guess it is a problem of x0, lb, ub etc, cuz everytime I change these parameter, the results varies a lot !!! But I will keep struggling...

Thank you so much guys and I will keep you informed.

Han



> - - - - - - - - - -
> You haven't told us what the range of t is in your 'xdata' vector. The model function you have defined has a singularity at x(2)*t = 0 where it shoots up to infinity, and you need to stay far enough away from that value to enable a good fit of your data to the model. In particular you need to strengthen Torsten's limit to make X(2) greater than or equal to some appropriate positive number (depending on how small t can be.) Otherwise you may get strange results and not a good fit.
>
> Roger Stafford

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