How to do exponential curve fitting like y=a*exp(b*x)+c
112 次查看(过去 30 天)
显示 更早的评论
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
2020-9-1
编辑:Arturo Gonzalez
2020-9-1
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
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))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179369/image.png)
14 个评论
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!