How to curve fit a transcendental function to data?
9 views (last 30 days)
Show older comments
Andreas Bovin
on 12 Jan 2017
Commented: Are Mjaavatten
on 18 May 2018
Hi,
I am trying to curve fit three parameters in a transcendental function to some data, and have searched both Google and MathWorks' fora to find an iterative method. However, so far none have seemed to yield results, or maybe I didn't understand the posted replys..
The function is as follows:
y = B - k1 (x - y/A) - k2 *(1 - exp( -k3(x - y/A)))
I already know the parameters A and B, and want to determine k1, k2 and k3 by fitting to a set of data containing values for x and y.
I have tried to isolate a zero on the left side, and then use the lsqcurvefit function to solve it using the following syntax (taken from one previously mentioned example):
B = 40; %Constants
A = 128e3;
k = [1 1 1]'; %Initial guesses of k1, k2, k3
xdata = epsilon; %x-data of 50 entries
ydata = sigma_matrix(:,5); %y-data of 50 entries
zeroes = zeros(1,length(xdata))'; %array of zeros to find root
predictedzeros = @(k,xdata,ydata) ydata + k1*(xdata-ydata/A) + k2(1-exp(-k3*(xdata-ydata/A))) - B
[ahat,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(predictedzeros,k,xdata,zeros(1,length(xdata))')
However, it gives me the following error:
predictedzeros =
@(k,xdata,ydata)ydata+k1*(xdata-ydata/A)+k2(1-exp(-k3*(xdata-ydata/A)))-B
Not enough input arguments.
Error in Fit>@(k,xdata,ydata)ydata+k1*(xdata-ydata/A)+k2(1-exp(-k3*(xdata-ydata/A)))-B
Error in lsqcurvefit (line 202)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in Fit (line 16)
[ahat,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(predictedzeros,k,xdata,zeros(1,length(xdata))')
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Does anyone have an idea of how to move forward from this point? I'm not an expert at MATLAB, but you're welcome to throw hardcore work-arounds at me :) I'm pretty frustrated at this point.
Thanks for your help.
0 Comments
Accepted Answer
Are Mjaavatten
on 13 Jan 2017
Set
k0 = [k1_0;k2_0;k3_0]
to a vector of initial values for your unknown parameters. Write your function as:
predictedzeros = @(k,xdata,ydata) ydata + k(1)*(xdata-ydata/A) + k(2)(1-exp(-k(3)*(xdata-ydata/A))) - B
and write the call to lsqcurvefit as:
k = lsqcurvefit(predictedzeros,k0,xdata,ydata)
You may have to try several initial value vectors.
4 Comments
Sankalp Shukla
on 4 May 2018
Edited: Sankalp Shukla
on 5 May 2018
Hi Are,
I am curious to know as to how the function F posted by Uday is any different from the function predictedzeros posted by Andreas? Doesn't Andreas's predictedzeros function also a function of both xdata and ydata? Thanks.
Are Mjaavatten
on 18 May 2018
Sankalp Shukla: Good point!
It seems my answer to Andreas Bovin was too hasty, and lsqcurvefit is not an appropriate tool for neither his nor Uday Saha's problem. The reason is that their expressions cannot be reformulated on the form y = f(k,x).
Using Uday's formula as an example, I would define function F as a function of k:
F=@(k) F = @(k) k(1)-k(2)*exp((xdata+ydata*k(3))/(0.025887*k(4)))...
-((xdata+ydata*k(3))/k(5));
and find the k that minimizes the sum of squares:
where:
by using use lsqnonlin:
kfit = lsqnonlin(F,k0);
More Answers (1)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!