How to use exponential function to fit my data?

5 次查看(过去 30 天)
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-')

采纳的回答

Mathieu NOE
Mathieu NOE 2021-11-5
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

更多回答(0 个)

类别

Help CenterFile 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!

Translated by