clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
X = 0 : 0.5 : 20;
a = 10
b = 0.4
Y = a + exp(-X * b);
Y = Y + 0.05 * randn(1, length(Y));
plot(X, Y, 'b*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
tbl = table(X', Y');
modelfun = @(b,x) b(1) + exp(-b(2)*x(:, 1));
beta0 = [10, .4];
mdl = fitnlm(tbl, modelfun, beta0);
coefficients = mdl.Coefficients{:, 'Estimate'}
yFitted = coefficients(1) + exp(-coefficients(2)*X);
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 25;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
7 Comments
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447746
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447746
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447754
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447754
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447882
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447882
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447887
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447887
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447907
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447907
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447913
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447913
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447932
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/336608-how-do-i-find-the-coefficients-of-a-function-for-a-curve-fit#comment_447932
Sign in to comment.