MATLAB: How can I fit the following points to equation “f(x) = a + b*exp(-c*x)”

curve fittingcustom fitMATLAB

x = 30, 60, 90, 120, 150, 180, 240, 270, 300, 330, 360, 390, 420
y = 333.15, 332.15, 330.65, 330.15, 329.15, 328.15, 327.65, 326.65, 326.15, 325.15, 324.65, 324.15, 323.15, 322.65, 322.15, 321.65

Best Answer

Try this:
x = [30, 60, 90, 120, 150, 180, 240, 270, 300, 330, 360, 390, 420];
y = [333.15, 332.15, 330.65, 330.15, 329.15, 328.15, 327.65, 326.65, 326.15, 325.15, 324.65, 324.15, 323.15, 322.65, 322.15, 321.6];
yt = y(1:numel(x)); % Truncate ‘y’ To Match Length Of ‘x’
f = @(b,x) b(1) + b(2).*exp(b(3).*x); % Objective Function
[B,fval] = fminsearch(@(b)norm(yt - f(b,x)), [100; 10; -1]); % Estimate Parameters
xe = linspace(min(x), max(x)); % Higher-Resolution ‘x’
figure
plot(x, yt, 'p')
hold on
plot(xe, f(B,xe), '-r')
hold off
grid
legend('Data','Regression Equation')
xl = xlim;
yl = ylim;
text(0.2*diff(xl)+min(xl), 0.1*diff(yl)+min(yl), sprintf('$y(x) = %.1f + %.1f e^{%.6f}$', B), 'Interpreter','latex')
Provide additional elements for ‘x’ to fit all the elements of ‘y’.