Dear APMonitor Thank you for uploading these videos. I have learn't a great deal from them. I am trying to find out if you would know how to solve for the nonlinear equations over a range of coefficients. ax^2 + ax + c = 0 where a & c are coefficients. Thus solve for x over the range of a: a = 0:5:1000?
Thanks for sharing! If I have a function which could have anywhere from 1 to 4 solutions, depending on some input parameters, and I want to return all of the solutions, is there a way to do this? I.e. without going through them one at a time, but instead returning a list or array of each of the 1,2,3 or 4 solutions?
Sympy returns all solutions but it is only applicable for analytic solutions (simple problems): github.com/APMonitor/data_science/blob/master/10.%20Solve_Equations.ipynb
Examples 2 and 3 with the Gekko Optimization Suite show how to use a constrained optimizer for root finding: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization
Good night, it's me again here from Brazil. I am trying to find two points of intersection between a circle, a line and a parabola, all curves in the plane. There are 3 equations with 2 variables. However, I would like your help to answer a question: - to solve the system I had to put the Z axis in the fsolve. The answer should be zero. Why is it different from zero? - Why, depending on the initial value that I put in fsolve, can he find the point of intersection between 2 curves and not 3? I will send my code. Note that for the initial palpilte (-3, -3, -20)) the answer is [-1.9 -0.9], which is the point of intersection between the circle and the parabola and not between the circle, parabola and the line. import numpy as np from scipy.optimize import fsolve def equations(p): X, Y, Z = p y1 = X-Y**2 +3 # parabolic y2 = X+Y+1 # line y3 = X**2+Y**2-5 # circle return (y1,y2,y3) X, Y, Z = fsolve(equations, (-3, -3, -20)) print(np. around((X,Y),2),Z) # Why the solution is just parabolic and circle? Why Z is not 0? I would appreciate it if you could help me. Luciana
The Z value can be anything and result in a successful solution. You may need to switch to an optimizer if you have 2 variables and 3 equations so that you can also impose a condition such as Minimize(Z**2) when it doesn't need to change to match the solution. Here is an updated version with fsolve.
import numpy as np from scipy.optimize import fsolve def equations(p): print(p) X, Y, Z = p y1 = X-Y**2 +3 # parabolic y2 = X+Y+1 # line y3 = X**2+Y**2-5 # circle return (y1,y2,y3) # solution 1: -2,-1,Z=anything # solution 2: 1, -2, Z = anything X, Y, Z = fsolve(equations, (-2.5, -1.5, 0)) print(np.around((X,Y),2),Z) # I printed the values p so that you can see what fsolve is guessing
Here is an alternative version with gekko (optimizer): from gekko import GEKKO m = GEKKO(remote=False) X,Y,Z = m.Array(m.Var,3) # add constraint to find one solution or the other # for solution 1 Y.lower=0; X.upper=0 m.Equations([X+3==Y**2,X+Y+1==0,X**2+Y**2==5]) m.Minimize(Z**2); m.options.RTOL=0.01 m.solve(disp=False) print(X.value[0],Y.value[0],Z.value[0]) # for solution 2 Y.lower=-1e8; X.upper=1e8 Y.upper=0; X.lower=0 m.solve(disp=False) print(X.value[0],Y.value[0],Z.value[0])
@@apm Again, thanks for your quick response and your help. But, I still have the doubt. I took your script and put two initial conditions: (-3, -3, 0) and (-3, -3, -20). The fsolve solution for (-3, -3, -20) does not exist for the 3 equations, only for two. The fsolve solution for the initial guess (-3, -3, 0) already gives a correct answer. I can not understand it. How could fsolve give a solution to only one two equations and not three? It is so confuse for me :(
Hi, thanks for sharing. I have one question though. @5:23: plt.plot(x, f(x)) plt.plot(x,np.zeros(len(x))) Why did we plot 'x' twice? Or is it something else that is happening? Thanks
One easy thing to try is to increase maxfev. See docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.fsolve.html The option to increase the maximum function evaluations to 500 is: maxfev = 500. There may be another problem with your problem, however, if it needed to reach this limit. I'd recommend simplifying your problem and checking that there is a good initial guess and a solution.
I have a problem with fsolve, sometimes it returns bad solutions I don't know why. I'm solving a function that has a parameter in a loop and it starts out very well, but at some point it returns bad solution eventhough I raise maxfev and lower xtol, what should I do ?
+The018fv, it is likely because of poor initial guesses, the problem is very nonlinear and fsolve isn't the right tool, or else the problem doesn't have a solution. I'd recommend that you try to improve the initial guess values (easiest fix) or else use a more capable optimization solver such as shown here apmonitor.com/che263/index.php/Main/PythonOptimization where you can include constraints to help guide the solution.
th-cam.com/video/66hOMWZec10/w-d-xo.html (3) and th-cam.com/video/Y7YBPqGKr9Y/w-d-xo.html (4). All of the assignments and solutions are listed here: apmonitor.com/che263/index.php/Main/CourseHomework This is assignment #14.
Thank you so much prof. Your lesson very good
This is helpful thank you .. Instantly subscribed!
Dear APMonitor
Thank you for uploading these videos. I have learn't a great deal from them. I am trying to find out if you would know how to solve for the nonlinear equations over a range of coefficients.
ax^2 + ax + c = 0
where a & c are coefficients.
Thus solve for x over the range of a: a = 0:5:1000?
For this equation, you could use the quadratic formula but plug in the value of a for b. en.wikipedia.org/wiki/Quadratic_formula
Thanks for sharing! If I have a function which could have anywhere from 1 to 4 solutions, depending on some input parameters, and I want to return all of the solutions, is there a way to do this? I.e. without going through them one at a time, but instead returning a list or array of each of the 1,2,3 or 4 solutions?
Sympy returns all solutions but it is only applicable for analytic solutions (simple problems): github.com/APMonitor/data_science/blob/master/10.%20Solve_Equations.ipynb
fsolve is very sensitive to initial guess.is there any other way to find root where we can find the root easily?
Examples 2 and 3 with the Gekko Optimization Suite show how to use a constrained optimizer for root finding: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization
Good night, it's me again here from Brazil.
I am trying to find two points of intersection between a circle, a line and a parabola, all curves in the plane. There are 3 equations with 2 variables.
However, I would like your help to answer a question:
- to solve the system I had to put the Z axis in the fsolve. The answer should be zero. Why is it different from zero?
- Why, depending on the initial value that I put in fsolve, can he find the point of intersection between 2 curves and not 3?
I will send my code.
Note that for the initial palpilte (-3, -3, -20)) the answer is [-1.9 -0.9], which is the point of intersection between the circle and the parabola and not between the circle, parabola and the line.
import numpy as np
from scipy.optimize import fsolve
def equations(p):
X, Y, Z = p
y1 = X-Y**2 +3 # parabolic
y2 = X+Y+1 # line
y3 = X**2+Y**2-5 # circle
return (y1,y2,y3)
X, Y, Z = fsolve(equations, (-3, -3, -20))
print(np. around((X,Y),2),Z)
# Why the solution is just parabolic and circle? Why Z is not 0?
I would appreciate it if you could help me.
Luciana
The Z value can be anything and result in a successful solution. You may need to switch to an optimizer if you have 2 variables and 3 equations so that you can also impose a condition such as Minimize(Z**2) when it doesn't need to change to match the solution. Here is an updated version with fsolve.
import numpy as np
from scipy.optimize import fsolve
def equations(p):
print(p)
X, Y, Z = p
y1 = X-Y**2 +3 # parabolic
y2 = X+Y+1 # line
y3 = X**2+Y**2-5 # circle
return (y1,y2,y3)
# solution 1: -2,-1,Z=anything
# solution 2: 1, -2, Z = anything
X, Y, Z = fsolve(equations, (-2.5, -1.5, 0))
print(np.around((X,Y),2),Z)
# I printed the values p so that you can see what fsolve is guessing
Here is an alternative version with gekko (optimizer):
from gekko import GEKKO
m = GEKKO(remote=False)
X,Y,Z = m.Array(m.Var,3)
# add constraint to find one solution or the other
# for solution 1
Y.lower=0; X.upper=0
m.Equations([X+3==Y**2,X+Y+1==0,X**2+Y**2==5])
m.Minimize(Z**2); m.options.RTOL=0.01
m.solve(disp=False)
print(X.value[0],Y.value[0],Z.value[0])
# for solution 2
Y.lower=-1e8; X.upper=1e8
Y.upper=0; X.lower=0
m.solve(disp=False)
print(X.value[0],Y.value[0],Z.value[0])
@@apm
Again, thanks for your quick response and your help.
But, I still have the doubt.
I took your script and put two initial conditions: (-3, -3, 0) and (-3, -3, -20).
The fsolve solution for (-3, -3, -20) does not exist for the 3 equations, only for two.
The fsolve solution for the initial guess (-3, -3, 0) already gives a correct answer.
I can not understand it. How could fsolve give a solution to only one two equations and not three? It is so confuse for me :(
@@pythonscienceanddatascienc4351 fsolve isn't a very good solver. You just need to give it a better guess value.
Hi Professor, These are great videos, where can i find the videos for other homework?
Here are the videos for the other solutions: apmonitor.com/che263/index.php/Main/CourseHomework
Thank you sir
Thanks sir!
Hi, thanks for sharing. I have one question though. @5:23:
plt.plot(x, f(x))
plt.plot(x,np.zeros(len(x)))
Why did we plot 'x' twice?
Or is it something else that is happening?
Thanks
You always plot x,y data as plt.plot(x,y). It is the values of the horizontal axis. The two trends share the same x values.
@@apm
Got it thanks!
When I am trying to solve a integral function. It appears: RuntimeWarning: The number of calls to function has reached maxfev. Can I solve it?
One easy thing to try is to increase maxfev. See docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.fsolve.html The option to increase the maximum function evaluations to 500 is: maxfev = 500. There may be another problem with your problem, however, if it needed to reach this limit. I'd recommend simplifying your problem and checking that there is a good initial guess and a solution.
I have a problem with fsolve, sometimes it returns bad solutions I don't know why.
I'm solving a function that has a parameter in a loop and it starts out very well, but at some point it returns bad solution eventhough I raise maxfev and lower xtol, what should I do ?
+The018fv, it is likely because of poor initial guesses, the problem is very nonlinear and fsolve isn't the right tool, or else the problem doesn't have a solution. I'd recommend that you try to improve the initial guess values (easiest fix) or else use a more capable optimization solver such as shown here apmonitor.com/che263/index.php/Main/PythonOptimization where you can include constraints to help guide the solution.
Can you kindly show how to plot the graph of the two nonlinear systems?
Here is a tutorial that shows how to add multiple trends to a plot: apmonitor.com/che263/index.php/Main/PythonPlots
What is the error of the fsolve solutions?
Could you point to time in the video where you have the question? You can include the reference with something like 5:05 (just include the time).
How can i use dependents equations (diffrential equations) using fsolve ?
Check out this example problem: apmonitor.com/pdc/index.php/Main/SimulateHIV - see the solution at the end for the source.
sorry, where is the video for problems 3 and 4??
th-cam.com/video/66hOMWZec10/w-d-xo.html (3) and th-cam.com/video/Y7YBPqGKr9Y/w-d-xo.html (4). All of the assignments and solutions are listed here: apmonitor.com/che263/index.php/Main/CourseHomework This is assignment #14.
thank you so much :)
thank u so much!
Thanks
based