How do I fit an exponential curve fit function using fminsearch. Program runs, but result is incorrect.
조회 수: 7 (최근 30일)
이전 댓글 표시
Attached is my code
yfirst=[.8967e07 1.6294e07 2.6587e07 3.2537e07 3.6136e07 3.8419e07 3.9706e07]';
xfirst=[1,2,4,6,8,10,15];
[estimates, model] = myfun(xfirst,yfirst);
[sse, FittedCurve] = model(estimates);
semilogx(xfirst,FittedCurve,'-g*'); hold on;
semilogx(xfirst,yfirst,'-rd');hold off;
xlim([ 0 50 ]);
%%%%FUNCTION FILE
function [estimates, model] = myfun(xdata, ydata)
% Call fminsearch with guessed starting point.
% start_point =[2.23e7;.005];
model = @expfun;
estimates = fminsearch(model,start_point);
function [sse, FittedCurve] = expfun(params)
A=params(1)
lambda=params(2)
FittedCurve =(A .* exp(lambda * xdata));
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
I get a large error and the curve does not match when plotted together. Thanks
댓글 수: 0
채택된 답변
Star Strider
2015년 6월 19일
There are several problems with your code.
This works:
yfirst=[.8967e07 1.6294e07 2.6587e07 3.2537e07 3.6136e07 3.8419e07 3.9706e07]';
xfirst=[1,2,4,6,8,10,15]';
expfun = @(b,xdata) b(1) -b(2) .* exp(b(3) .* xdata); % Objective Funciton
SSECF = @(b) sum((yfirst - expfun(b,xfirst)).^2); % Sum-Squared-Error Cost Function
start_point =[4E+7; 2.23e7; -.005];
[B, SSE] = fminsearch(SSECF, start_point);
figure(1)
plot(xfirst, yfirst, 'bp')
hold on
plot(xfirst, expfun(B,xfirst), '-r')
hold off
grid
text(5.2, 1.75E+7, sprintf('f(x) = %9.2E - %9.2E\\cdote^{%9.2E\\cdotx}', B))
댓글 수: 2
Star Strider
2015년 6월 22일
In this instance (that is with your data), you do. It is an asymptotically-increasing exponential, so you have to have parameters for the asymptote, amplitude, and exponential rate. The integrated differential equation for the process that created your data would require values for all three parameters. (With a simple decaying exponential, you would only need parameters for the initial value and exponential rate.)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!