
Non- Linear curve fitting
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a set of data and I need to fit it to the curve F(x,xdata) and then find the values of five unknown coefficients .
a,b,c,d and e are the five coefficients which are replaced by x(0) , x(1) , x(2), x(3), x(4) and x(5) in the code below.
Running this code produces an error message. But when I removed "xdata" which was originally multiplied to the sqaured expression in the later part of the function, I got an output containing the values of the coefficients I needed.
How do i get an ouput while not having to remove "xdata" from the later part of the function?
clc;
Data = ...
[-0.02 2000
0 1650
0.03 1300
0.06 1050
0.09 880
0.12 700
0.15 550
0.18 400
0.21 240
0.24 120
0.27 0 ];
k = Data(:,1);
y = Data(:,2);
F = @(x,xdata)6*(x(1)*exp(-xdata*x(2))-x(3))*(2*(1-xdata*(x(4)*exp(-100000)-x(5)))^2-1);
plot(k,y,'r');
x0 = [1 1 1 1 1];
[x] = lsqcurvefit(F,x0,k,y)
댓글 수: 0
채택된 답변
Star Strider
2020년 10월 11일
It is necessary to vectorise every multiplication, division,. and exponentiation in an objective function.
With those changes:
F = @(x,xdata)6*(x(1)*exp(-xdata*x(2))-x(3)).*(2*(1-xdata*(x(4)*exp(-100000)-x(5))).^2-1);
and:
figure
plot(k,y,'xr')
hold on
plot(k, F(x,k), '-b')
hold off
grid
xlabel('k')
ylabel('y')
legend('Data', 'F')
produces:

.
댓글 수: 3
Star Strider
2020년 10월 11일
As always, my pleasure!
For relatively long expressions, it is sometimes easier to copy the function expression as a character array and use the vectorize function on it. You can then copy that result expression to the original function (obviously without the quotes), and run it. (The documentation says that vectorize is ‘not recommended’ however I have no idea what the reason is, since it is quite useful. I brought this up with MathWorks a few months ago who were as mystified as I was about the designation, however it still exists.)
추가 답변 (0개)
참고 항목
카테고리
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!