Non- Linear curve fitting
3 views (last 30 days)
Show older comments
Lomalsvi Bischerre
on 11 Oct 2020
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 Comments
Accepted Answer
Star Strider
on 11 Oct 2020
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 Comments
Star Strider
on 11 Oct 2020
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.)
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!