Emmanuel Enukpere
Emmanuel Enukpere

Reputation: 11

Using fsolve in python3

I am trying to find a solve the following equation using Python3: 83.045269**(8)np.exp(-3.045269a) + 63.516372**(8) np.exp(-[a+1.042817]3.516372) - 42.8578**8 * np.exp(-2.8578a) - 4*3.3**(8) *exp(-[a+1.042817]*3.30) = 0 and I did the follow:

>>> import numpy as np
>>> from scipy.optimize import fsolve
>>> def equation(a):
...     term1 = 8 * 3.045269**8 * np.exp(-3.045269)
...     term2 = 6 * 3.516372**8 * np.exp(-(a + 1.042817) * 3.516372)
...     term3 = -4 * 2.8578**8 * np.exp(-2.8578 * a)
...     term4 = -4 * 3.3**8 * np.exp(-(a + 1.042817) *3.3)
...     return term1 + term2 +term3 + term4
... initial_guess = 0.5
File "<stdin>", line 7
initial_guess = 0.5
^^^^^^^^^^^^^
SyntaxError: invalid syntax
 >>> initial guess = 0.5
File "<stdin>", line 1
initial guess = 0.5
        ^^^^^
 SyntaxError: invalid syntax
>>> 

what is wrong with the above?

Upvotes: 1

Views: 32

Answers (1)

dev_light
dev_light

Reputation: 3889

This is not about fsolve rather it's about using the Python REPL correctly. When using the Python REPL, you need to be aware of the difference between the three greater-than signs known as the primary prompt and the three dots known as the secondary prompt.

The primary prompt (>>>) communicates that the interpreter is ready to accept input. The secondary prompt (...) appears when you’re entering compound statements or line continuations. In your case, you defined a function equation with a parameter a. After hitting the enter key, the secondary prompt appears for succeeding lines of codes signifying they are in the function block. After your return statement, you need to exit the function block meaning you should hit the enter key again until you see the primary prompt. Essentially, initial_guess = 0.5 should be entered only when you see the >>> prompt i.e.:

>>> import numpy as np
>>> from scipy.optimize import fsolve
>>> def equation(a):
...       term1 = 8 * 3.045269**8 *   np.exp(-3.045269)
...       term2 = 6 * 3.516372**8 * np.exp(-(a + 1.042817) * 3.516372)
...       term3 = -4 * 2.8578**8 * np.exp(-2.8578 * a)
...       term4 = -4 * 3.3**8 * np.exp(-(a + 1.042817) *3.3)
...       return term1 + term2 +term3 + term4
... 
>>> initial_guess = 0.5

Upvotes: 2

Related Questions