How to curve fit a transcendental function to data?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Are Mjaavatten
2017-1-13
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 个评论
Sankalp Shukla
2018-5-4
编辑:Sankalp Shukla
2018-5-5
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
2018-5-18
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);
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!