Least squares Exponential fit using polyfit

Let's say I'm given x=[11,60,150,200] and y=[800,500,400,90] These are just random numbers (but imagine the solution is in the form of y=a*exp(b*t)
Now, I want to find what 'a' and 'b' are. This is what I'm thinking to do, but I'm not sure if it's correct:
So, if I take ln of both sides of the above equation, I'll get ln(y)= ln(a) +bx. This is in the form of y=mx+b (linear equation).
x= [10, 55, 120, 180]
y= [750, 550, 300, 100]
yPrime= log(y)%take natural logarithm of y data values
pPrime=polyfit(t,yPrime,1)%
aPrime=pPrime(1)
bPrime=pPrime(2)
so now I found the constants for my above LINEAR equation. To find 'a' and 'b' from 'y=a*exp(b*t)', should I now raise the linear constants I found to e? (e^aPrime = a, e^bPrime= b) ?
Is this how I find 'a' and 'b'?

 Akzeptierte Antwort

Star Strider
Star Strider am 21 Mär. 2018

3 Stimmen

Since you are starting with:
y = a * exp(b * t)
and linearising it yields:
log(y) = log(a) + b*t
however ‘aPrime’ and ‘bPrime’ are reversed with respect to the way polyfit works.
So polyfit returns:
bPrime = pPrime(1)
aPrime = pPrime(2)
you need to transform only ‘aPrime’. So:
a = exp(aPrime)
If you want to plot a line-of-fit, you could either use your originally log-transformed equation with log-transformed variables:
log(y) = aPrime + bPrime*t
or:
yfit = exp(log(aPrime)) * exp(b*t)
with your original data.
In code:
t = [11,60,150,200];
y = [800,500,400,90];
yPrime= log(y)%take natural logarithm of y data values
pPrime=polyfit(t,yPrime,1)%
aPrime=pPrime(2)
bPrime=pPrime(1)
figure(1)
plot(t, log(y), 'p', t, polyval(pPrime, t), '-r')
figure(2)
plot(t, y, 'p', t, exp(aPrime)*exp(t*bPrime), '-r')
figure(3)
semilogy(t, y, 'p', t, exp(aPrime)*exp(t*bPrime), '-r')

6 Kommentare

Rachel Dawn
Rachel Dawn am 21 Mär. 2018
Star Strider,
Thank you. I ended up figuring it out, except for the plotting part. Thank you for that! I really appreciate it.
Star Strider
Star Strider am 22 Mär. 2018
As always, my pleasure.
If my Answer helped you solve your problem please Accept it!
Rachel Dawn
Rachel Dawn am 22 Mär. 2018
Bearbeitet: Rachel Dawn am 22 Mär. 2018
Star, I accepted it.
I have one more question. I'm just curious, what if it were y=a*(x^b)? I could do "ln(y) = ln(a) + a*ln(x)" but then what would separate this from the exponential function (when writing the code)?
I'm thinking the only difference would be that I would add "xprime= log(x)".
Thank you.
That’s a power function. I’ll restate it to make it a bit clearer:
ln(y) = ln(b) + a*ln(x)
In that polyfit result, ‘bPrime’ would be ‘a’ and ‘aPrime’ would be ‘log(b)’. So to get the original value of ‘b’, calculate it as:
b = exp(aPrime);
The polyfit function returns the coefficients in descending powers of the independent variable, so in a two-parameter linear problem will return:
y = bPrime*x + aPrime
Tamir Suliman
Tamir Suliman am 11 Okt. 2021
Bearbeitet: Tamir Suliman am 11 Okt. 2021
didnt you also have to ployfit for log(t) values ?
yPrime= log(y)%take natural logarithm of y data values
tPrime = log(t)
pPrime=polyfit(tPrime,yPrime,1)%
can you write code for power function ? i am facing a problem

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by