How to do exponential curve fitting like y=a*exp(b*x)+c

조회 수: 52 (최근 30일)
MCC
MCC 2018년 2월 23일
댓글: Star Strider 2022년 11월 9일
Hi guys,
I have a set of data x and y, which is given below: x=[10 12.5 15 17.5 20 22.5 25 27.5 30 32.5 35 37.5 40 42.5 45 47.5 50]; y=[62.1 77.3 92.5 104 112.9 121.9 125 129.4 134 138.2 142.3 143.2 144.6 147.2 147.8 149.1 150.9];
I'd like to to have a curve fitting like y=a*exp(b*x)+c. I tried to use cftool box (custom equation). However, it didn't work well. I am wandering if someone could help me with this.
Thanks
  댓글 수: 1
Arturo Gonzalez
Arturo Gonzalez 2020년 9월 1일
편집: Arturo Gonzalez 2020년 9월 1일
Per this answer, you can do it as follows:
clear all;
clc;
% get data
dx = 0.02;
x = (dx:dx:1.5)';
y = -1 + 5*exp(0.5*x) + 4*exp(-3*x) + 2*exp(-2*x);
% calculate integrals
iy1 = cumtrapz(x, y);
iy2 = cumtrapz(x, iy1);
iy3 = cumtrapz(x, iy2);
% get exponentials lambdas
Y = [iy1, iy2, iy3, x.^3, x.^2, x, ones(size(x))];
A = pinv(Y)*y;
lambdas = eig([A(1), A(2), A(3); 1, 0, 0; 0, 1, 0]);
lambdas
%lambdas =
% -2.9991
% -1.9997
% 0.5000
% get exponentials multipliers
X = [ones(size(x)), exp(lambdas(1)*x), exp(lambdas(2)*x), exp(lambdas(3)*x)];
P = pinv(X)*y;
P
%P =
% -0.9996
% 4.0043
% 1.9955
% 4.9999

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2018년 2월 23일
Try this:
x=[10 12.5 15 17.5 20 22.5 25 27.5 30 32.5 35 37.5 40 42.5 45 47.5 50];
y=[62.1 77.3 92.5 104 112.9 121.9 125 129.4 134 138.2 142.3 143.2 144.6 147.2 147.8 149.1 150.9];
f = @(b,x) b(1).*exp(b(2).*x)+b(3); % Objective Function
B = fminsearch(@(b) norm(y - f(b,x)), [-200; -1; 100]) % Estimate Parameters
figure
plot(x, y, 'pg')
hold on
plot(x, f(B,x), '-r')
hold off
grid
xlabel('x')
ylabel('f(x)')
text(27, 105, sprintf('f(x) = %.1f\\cdote^{%.3f\\cdotx}%+.1f', B))
  댓글 수: 14
Kate Leary
Kate Leary 2022년 11월 9일
Thank you thank you! Is there a way to calculate prediction bounds as well? I know of predint but can't figure out how to use is without having a cfit or sfit object. Again, thank you for your help!
Star Strider
Star Strider 2022년 11월 9일
My pleasure!
They’re actually plotted (as ‘95% CI’, just so narrow that it’s difficult to see them here. the predict function call in my code calculates them:
[yv,yci] = predict(mdl,xv(:));
and they are then plotted in:
hp{3} = plot(xv, yci, ':r', 'DisplayName', '95% CI');
If you have any further questions, please feel free to follow up here.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by