How to fit a specific exponential function with numpy










1















I'm trying to fit a series of data to a exponential equation, I've found some great answer here: How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting But it didn't contain the step forward that I need for this question.



I'm trying to fit y and x against a equation: y = -AeBx + A. The final A has proven to be a big trouble and I don't know how to transform the equation like log(y) = log(A) + Bx as if the final A was not there.



Any help is appreciated.










share|improve this question
























  • Are you sure you want A twice in that equation?

    – Mad Physicist
    Nov 13 '18 at 2:55











  • Also, absorb the second A into the log on the left hand side.

    – Mad Physicist
    Nov 13 '18 at 2:57











  • @MadPhysicist Its unknown, hence can’t be calculated

    – Rocky Li
    Nov 13 '18 at 3:03











  • Did you mean y=-Ae^Bx + C? Or is C == A, and that's not a typo?

    – Mad Physicist
    Nov 13 '18 at 3:06






  • 1





    @MadPhysicist Yes that is not a typo, the original equation is y/A = -e^(Bx) + 1

    – Rocky Li
    Nov 13 '18 at 3:09
















1















I'm trying to fit a series of data to a exponential equation, I've found some great answer here: How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting But it didn't contain the step forward that I need for this question.



I'm trying to fit y and x against a equation: y = -AeBx + A. The final A has proven to be a big trouble and I don't know how to transform the equation like log(y) = log(A) + Bx as if the final A was not there.



Any help is appreciated.










share|improve this question
























  • Are you sure you want A twice in that equation?

    – Mad Physicist
    Nov 13 '18 at 2:55











  • Also, absorb the second A into the log on the left hand side.

    – Mad Physicist
    Nov 13 '18 at 2:57











  • @MadPhysicist Its unknown, hence can’t be calculated

    – Rocky Li
    Nov 13 '18 at 3:03











  • Did you mean y=-Ae^Bx + C? Or is C == A, and that's not a typo?

    – Mad Physicist
    Nov 13 '18 at 3:06






  • 1





    @MadPhysicist Yes that is not a typo, the original equation is y/A = -e^(Bx) + 1

    – Rocky Li
    Nov 13 '18 at 3:09














1












1








1


1






I'm trying to fit a series of data to a exponential equation, I've found some great answer here: How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting But it didn't contain the step forward that I need for this question.



I'm trying to fit y and x against a equation: y = -AeBx + A. The final A has proven to be a big trouble and I don't know how to transform the equation like log(y) = log(A) + Bx as if the final A was not there.



Any help is appreciated.










share|improve this question
















I'm trying to fit a series of data to a exponential equation, I've found some great answer here: How to do exponential and logarithmic curve fitting in Python? I found only polynomial fitting But it didn't contain the step forward that I need for this question.



I'm trying to fit y and x against a equation: y = -AeBx + A. The final A has proven to be a big trouble and I don't know how to transform the equation like log(y) = log(A) + Bx as if the final A was not there.



Any help is appreciated.







python numpy curve-fitting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 3:10







Rocky Li

















asked Nov 13 '18 at 2:44









Rocky LiRocky Li

2,8731316




2,8731316












  • Are you sure you want A twice in that equation?

    – Mad Physicist
    Nov 13 '18 at 2:55











  • Also, absorb the second A into the log on the left hand side.

    – Mad Physicist
    Nov 13 '18 at 2:57











  • @MadPhysicist Its unknown, hence can’t be calculated

    – Rocky Li
    Nov 13 '18 at 3:03











  • Did you mean y=-Ae^Bx + C? Or is C == A, and that's not a typo?

    – Mad Physicist
    Nov 13 '18 at 3:06






  • 1





    @MadPhysicist Yes that is not a typo, the original equation is y/A = -e^(Bx) + 1

    – Rocky Li
    Nov 13 '18 at 3:09


















  • Are you sure you want A twice in that equation?

    – Mad Physicist
    Nov 13 '18 at 2:55











  • Also, absorb the second A into the log on the left hand side.

    – Mad Physicist
    Nov 13 '18 at 2:57











  • @MadPhysicist Its unknown, hence can’t be calculated

    – Rocky Li
    Nov 13 '18 at 3:03











  • Did you mean y=-Ae^Bx + C? Or is C == A, and that's not a typo?

    – Mad Physicist
    Nov 13 '18 at 3:06






  • 1





    @MadPhysicist Yes that is not a typo, the original equation is y/A = -e^(Bx) + 1

    – Rocky Li
    Nov 13 '18 at 3:09

















Are you sure you want A twice in that equation?

– Mad Physicist
Nov 13 '18 at 2:55





Are you sure you want A twice in that equation?

– Mad Physicist
Nov 13 '18 at 2:55













Also, absorb the second A into the log on the left hand side.

– Mad Physicist
Nov 13 '18 at 2:57





Also, absorb the second A into the log on the left hand side.

– Mad Physicist
Nov 13 '18 at 2:57













@MadPhysicist Its unknown, hence can’t be calculated

– Rocky Li
Nov 13 '18 at 3:03





@MadPhysicist Its unknown, hence can’t be calculated

– Rocky Li
Nov 13 '18 at 3:03













Did you mean y=-Ae^Bx + C? Or is C == A, and that's not a typo?

– Mad Physicist
Nov 13 '18 at 3:06





Did you mean y=-Ae^Bx + C? Or is C == A, and that's not a typo?

– Mad Physicist
Nov 13 '18 at 3:06




1




1





@MadPhysicist Yes that is not a typo, the original equation is y/A = -e^(Bx) + 1

– Rocky Li
Nov 13 '18 at 3:09






@MadPhysicist Yes that is not a typo, the original equation is y/A = -e^(Bx) + 1

– Rocky Li
Nov 13 '18 at 3:09













1 Answer
1






active

oldest

votes


















3














You can always just use scipy.optimize.curve_fit as long as your equation isn't too crazy:



import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as sio

def f(x, A, B):
return -A*np.exp(B*x) + A

A = 2
B = 1
x = np.linspace(0,1)

y = f(x, A, B)
scale = (max(y) - min(y))*.10
noise = np.random.normal(size=x.size)*scale
y += noise

fit = sio.curve_fit(f, x, y)

plt.scatter(x, y)
plt.plot(x, f(x, *fit[0]))
plt.show()


This produces:



enter image description here






share|improve this answer























  • Interesting and nice explanation, I'll try it out.

    – Rocky Li
    Nov 13 '18 at 3:14











  • Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

    – Rocky Li
    Nov 13 '18 at 3:21







  • 1





    @RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

    – tel
    Nov 13 '18 at 3:33











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273033%2fhow-to-fit-a-specific-exponential-function-with-numpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can always just use scipy.optimize.curve_fit as long as your equation isn't too crazy:



import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as sio

def f(x, A, B):
return -A*np.exp(B*x) + A

A = 2
B = 1
x = np.linspace(0,1)

y = f(x, A, B)
scale = (max(y) - min(y))*.10
noise = np.random.normal(size=x.size)*scale
y += noise

fit = sio.curve_fit(f, x, y)

plt.scatter(x, y)
plt.plot(x, f(x, *fit[0]))
plt.show()


This produces:



enter image description here






share|improve this answer























  • Interesting and nice explanation, I'll try it out.

    – Rocky Li
    Nov 13 '18 at 3:14











  • Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

    – Rocky Li
    Nov 13 '18 at 3:21







  • 1





    @RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

    – tel
    Nov 13 '18 at 3:33
















3














You can always just use scipy.optimize.curve_fit as long as your equation isn't too crazy:



import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as sio

def f(x, A, B):
return -A*np.exp(B*x) + A

A = 2
B = 1
x = np.linspace(0,1)

y = f(x, A, B)
scale = (max(y) - min(y))*.10
noise = np.random.normal(size=x.size)*scale
y += noise

fit = sio.curve_fit(f, x, y)

plt.scatter(x, y)
plt.plot(x, f(x, *fit[0]))
plt.show()


This produces:



enter image description here






share|improve this answer























  • Interesting and nice explanation, I'll try it out.

    – Rocky Li
    Nov 13 '18 at 3:14











  • Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

    – Rocky Li
    Nov 13 '18 at 3:21







  • 1





    @RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

    – tel
    Nov 13 '18 at 3:33














3












3








3







You can always just use scipy.optimize.curve_fit as long as your equation isn't too crazy:



import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as sio

def f(x, A, B):
return -A*np.exp(B*x) + A

A = 2
B = 1
x = np.linspace(0,1)

y = f(x, A, B)
scale = (max(y) - min(y))*.10
noise = np.random.normal(size=x.size)*scale
y += noise

fit = sio.curve_fit(f, x, y)

plt.scatter(x, y)
plt.plot(x, f(x, *fit[0]))
plt.show()


This produces:



enter image description here






share|improve this answer













You can always just use scipy.optimize.curve_fit as long as your equation isn't too crazy:



import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as sio

def f(x, A, B):
return -A*np.exp(B*x) + A

A = 2
B = 1
x = np.linspace(0,1)

y = f(x, A, B)
scale = (max(y) - min(y))*.10
noise = np.random.normal(size=x.size)*scale
y += noise

fit = sio.curve_fit(f, x, y)

plt.scatter(x, y)
plt.plot(x, f(x, *fit[0]))
plt.show()


This produces:



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 3:12









teltel

7,30121431




7,30121431












  • Interesting and nice explanation, I'll try it out.

    – Rocky Li
    Nov 13 '18 at 3:14











  • Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

    – Rocky Li
    Nov 13 '18 at 3:21







  • 1





    @RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

    – tel
    Nov 13 '18 at 3:33


















  • Interesting and nice explanation, I'll try it out.

    – Rocky Li
    Nov 13 '18 at 3:14











  • Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

    – Rocky Li
    Nov 13 '18 at 3:21







  • 1





    @RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

    – tel
    Nov 13 '18 at 3:33

















Interesting and nice explanation, I'll try it out.

– Rocky Li
Nov 13 '18 at 3:14





Interesting and nice explanation, I'll try it out.

– Rocky Li
Nov 13 '18 at 3:14













Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

– Rocky Li
Nov 13 '18 at 3:21






Worked! I just needed to flip B to negative because it doesn't deal with negative numbers apparently.

– Rocky Li
Nov 13 '18 at 3:21





1




1





@RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

– tel
Nov 13 '18 at 3:33






@RockyLi Great, glad to hear it. And if you're ever bored and looking for some math to do, you may be able to derive an analytical expression for the fit using the least-squares approach. There's a well-known closed form equation for the fit of the standard exponential form y = A*e**(B*x)

– tel
Nov 13 '18 at 3:33


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273033%2fhow-to-fit-a-specific-exponential-function-with-numpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus