exponential curve fitting in MATLAB for any type of x and Y data with equation y=ae^bx to calculate a and b coefficients
7 views (last 30 days)
Show older comments
%code that i used is :
%program for the exponential curve fitting y=ae^bx
clc
clear
disp ('**program on exponential curve fitting y=ae^bx**')
k=xlsread("Pt-Fumion", "Sheet1");
x=input('enter value of x in matrix form:');
y=input('enter value of y in matrix form:');
n=length(x);
Y=log(y);
X=(x);
%first of all set all values to zero for addition of numbers
sumX=0;
sumY=0;
sumXY=0;
sumX2=0;
for i=1:n
sumX=sumX+X(i);
sumY=sumY+Y(i);
sumXY=sumXY+X(i)*Y(i);
sumX2=sumX2+X(i)*X(i);
end
d=[sumX n ;sumX2 sumX]; %error is displayed for this line when running the code
da=[sumY n ;sumXY sumX]; %error is displayed for this line when running the code
db=[sumXY ;sumX2 sumXY]; %error is displayed for this line when running the code
a1=det(da)/det(D);
b1=det(db)/det(d);
b=a1;
a=exp(b1);
%after finding the value of a and b the equation of line is
fprintf('the equation of line is y=%fe^%fx',a,b)
%queries: 1. there is error in running code for concatenation of the line da and db
%2. how can I add excel data into the input in the command window, when it asks for it
%3. there are negative values in the y axis, is that a probelm when log is taken to excecute the code
%4. I am doing this to to fit the data to an exponential equation and extrapolate to have a number from y axis
%I am attaching the excel file with x and y data points
0 Comments
Answers (1)
Torsten
on 10 Jul 2022
Instead of your code (in which you linearize f giving biassed parameter estimates) use the following:
fun = @(p)p(1)*exp(p(2)*x)-y;
p0 = [1 -0.1]
p = lsqnonlin(fun,p0)
plot(x,y)
hold on
plot(x,fun(p)+y)
0 Comments
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!