How to use exponential function to fit my data?
5 views (last 30 days)
Show older comments
Taoooooooooooo
on 3 Nov 2021
Commented: Mathieu NOE
on 19 Nov 2021
Hi,
I tried to use lsqcurvefit function to fit my data and the results did not look right at all. The x and y are attached above where y is the center_voxel (80-by-1Matrix). Please help and here is my code:
x = linspace(0,128,80);
y = center_voxel';
xdata=x;
ydata=y;
lb = [-100, -100, -100];
ub = [100, 100, 100];
fun = @(xx,xdata)xx(1).*exp(xx(2).*xdata)+xx(3);
x0 = [0.5, 0.5, 0.5];
xx = lsqcurvefit(fun,x0,xdata,ydata,ub,lb)
yy = linspace(xdata(1),xdata(end));
figure
plot(xdata, ydata, 'pg')
hold on
plot(yy, fun(xx,yy),'r-')
0 Comments
Accepted Answer
Mathieu NOE
on 5 Nov 2021
hello
try this
clc
clearvars
load('x axis.mat')
load('y axis.mat')
x = linspace(0,128,80);
y = center_voxel';
f = @(a,b,c,x) a.*exp(x.*b)+c;
obj_fun = @(params) norm(f(params(1), params(2), params(3),x)-y);
sol = fminsearch(obj_fun, [-y(end),-1e-1,y(end)]);
a = sol(1)
b = sol(2)
c = sol(3)
figure;
plot(x, y, '+', 'MarkerSize', 10, 'LineWidth', 2)
hold on
plot(x, f(a, b,c, x), '-');grid on
xlabel('x');
ylabel('y');
a = -3.5863e+03
b = -0.0143
c = 3.4995e+03
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!