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:
Curvefitting with fminsearch

Subject: Curvefitting with fminsearch

From: Kapila Bandara

Date: 6 Sep, 2013 06:48:41

Message: 1 of 3

Hi

I used fminserach function to for optimize the fitting with custom function. However, it gives deference results at each simulation . I have attached both function file and code file here with. could you please help me to adjust my programme to get consistence answer.

unction [estimates, model] = fitcurvedemo1(xdata, ydata)
% Call fminsearch with a random starting point.
start_point = rand(1,2);
model = @expfun;
estimates = fminsearch(model, start_point);
% expfun accepts curve parameters as inputs, and outputs sse,
% the sum of squares error for A*exp(-lambda*xdata)-ydata,
% and the FittedCurve. FMINSEARCH only needs sse, but we want
% to plot the FittedCurve at the end.
    function [sse, FittedCurve] = expfun(params)
        A = params(1);
        lambda = params(2);
       
       % B=params(3);
       % beta=params(4);
        FittedCurve =A.*(exp(-lambda.*((1.8*1^-26)./xdata)));
        ErrorVector = FittedCurve - ydata;
        sse = sum(ErrorVector .^ 2);
    end
end
........................
clear all
ydata=10^-12*[8.51 51 100 ]
xdata=[1 2 3 ]


[estimates, model] = fitcurvedemo1(xdata,ydata)
plot(xdata, ydata, '*')
hold on
[sse, FittedCurve] = model(estimates);
plot(xdata, FittedCurve, 'r')
 
xlabel('xdata')
ylabel('f(estimates,xdata)')
title(['Fitting to function ', func2str(model)]);
legend('data', ['fit using ', func2str(model)])
hold off

Subject: Curvefitting with fminsearch

From: Torsten

Date: 6 Sep, 2013 08:52:08

Message: 2 of 3

Kapila Bandara <kmkssanju@gmail.com> wrote in message <7c6b0e44-91d4-4012-9269-3f446903ae16@googlegroups.com>...
> Hi
>
> I used fminserach function to for optimize the fitting with custom function. However, it gives deference results at each simulation . I have attached both function file and code file here with. could you please help me to adjust my programme to get consistence answer.
>
> unction [estimates, model] = fitcurvedemo1(xdata, ydata)
> % Call fminsearch with a random starting point.
> start_point = rand(1,2);
> model = @expfun;
> estimates = fminsearch(model, start_point);
> % expfun accepts curve parameters as inputs, and outputs sse,
> % the sum of squares error for A*exp(-lambda*xdata)-ydata,
> % and the FittedCurve. FMINSEARCH only needs sse, but we want
> % to plot the FittedCurve at the end.
> function [sse, FittedCurve] = expfun(params)
> A = params(1);
> lambda = params(2);
>
> % B=params(3);
> % beta=params(4);
> FittedCurve =A.*(exp(-lambda.*((1.8*1^-26)./xdata)));
> ErrorVector = FittedCurve - ydata;
> sse = sum(ErrorVector .^ 2);
> end
> end
> ........................
> clear all
> ydata=10^-12*[8.51 51 100 ]
> xdata=[1 2 3 ]
>
>
> [estimates, model] = fitcurvedemo1(xdata,ydata)
> plot(xdata, ydata, '*')
> hold on
> [sse, FittedCurve] = model(estimates);
> plot(xdata, FittedCurve, 'r')
>
> xlabel('xdata')
> ylabel('f(estimates,xdata)')
> title(['Fitting to function ', func2str(model)]);
> legend('data', ['fit using ', func2str(model)])
> hold off

1. Use lsqcurvefit for such kind of problems.
2. Your ydata are very small - so you will have to choose a very small tolerance
TolFun.

Best wishes
Torsten.

 

Subject: Curvefitting with fminsearch

From: Kapila Bandara

Date: 7 Sep, 2013 07:23:31

Message: 3 of 3

On Friday, September 6, 2013 6:52:08 PM UTC+10, Torsten wrote:
> Kapila Bandara <kmkssanju@gmail.com> wrote in message <7c6b0e44-91d4-4012-9269-3f446903ae16@googlegroups.com>...
>
> > Hi
>
> >
>
> > I used fminserach function to for optimize the fitting with custom function. However, it gives deference results at each simulation . I have attached both function file and code file here with. could you please help me to adjust my programme to get consistence answer.
>
> >
>
> > unction [estimates, model] = fitcurvedemo1(xdata, ydata)
>
> > % Call fminsearch with a random starting point.
>
> > start_point = rand(1,2);
>
> > model = @expfun;
>
> > estimates = fminsearch(model, start_point);
>
> > % expfun accepts curve parameters as inputs, and outputs sse,
>
> > % the sum of squares error for A*exp(-lambda*xdata)-ydata,
>
> > % and the FittedCurve. FMINSEARCH only needs sse, but we want
>
> > % to plot the FittedCurve at the end.
>
> > function [sse, FittedCurve] = expfun(params)
>
> > A = params(1);
>
> > lambda = params(2);
>
> >
>
> > % B=params(3);
>
> > % beta=params(4);
>
> > FittedCurve =A.*(exp(-lambda.*((1.8*1^-26)./xdata)));
>
> > ErrorVector = FittedCurve - ydata;
>
> > sse = sum(ErrorVector .^ 2);
>
> > end
>
> > end
>
> > ........................
>
> > clear all
>
> > ydata=10^-12*[8.51 51 100 ]
>
> > xdata=[1 2 3 ]
>
> >
>
> >
>
> > [estimates, model] = fitcurvedemo1(xdata,ydata)
>
> > plot(xdata, ydata, '*')
>
> > hold on
>
> > [sse, FittedCurve] = model(estimates);
>
> > plot(xdata, FittedCurve, 'r')
>
> >
>
> > xlabel('xdata')
>
> > ylabel('f(estimates,xdata)')
>
> > title(['Fitting to function ', func2str(model)]);
>
> > legend('data', ['fit using ', func2str(model)])
>
> > hold off
>
>
>
> 1. Use lsqcurvefit for such kind of problems.
>
> 2. Your ydata are very small - so you will have to choose a very small tolerance
>
> TolFun.
>
>
>
> Best wishes
>
> Torsten.

Thank you very much

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