The Student Room Group

Numerical solution

my goal is to solve this equation numerically on python 0=∫0sf(x)dx0=\int_{0}^{s} f(x) dx(assuming that a solution exists). I initially attempted it through sympy however it's quite limited in what functions it can solve, for example it fails for f(x)=1+sin(x).

I searched a bit online and it seems scipy.optimise.fsolve is a feasible solution? my issue is that the upper bound of the integral is symbolic so script.integrate doesn't work?

How do I go about implementing the code for this
(edited 11 months ago)
Original post by Student 999
my goal is to solve this equation numerically on python 0=∫0sf(x)dx0=\int_{0}^{s} f(x) dx(assuming that a solution exists). I initially attempted it through sympy however it's quite limited in what functions it can solve, for example it fails for f(x)=1+sin(x).

I searched a bit online and it seems scipy.optimise.fsolve is a feasible solution? my issue is that the upper bound of the integral is symbolic so script.integrate doesn't work?

How do I go about implementing the code for this


Assuming a solution exists limits you to a particular class of functions ff ... your example of 1+sin⁡x1 + \sin x is not in this class so of course it failed. There is no solution to find for this choice of function.

Test it for something like f(x)=1−xf(x) = 1-x where you know the solution must be s=2s = 2.
(edited 11 months ago)
Reply 2
Original post by Student 999
my goal is to solve this equation numerically on python 0=∫0sf(x)dx0=\int_{0}^{s} f(x) dx(assuming that a solution exists). I initially attempted it through sympy however it's quite limited in what functions it can solve, for example it fails for f(x)=1+sin(x).

I searched a bit online and it seems scipy.optimise.fsolve is a feasible solution? my issue is that the upper bound of the integral is symbolic so script.integrate doesn't work?

How do I go about implementing the code for this

Are you after a numerical or symbolic solution? Not used sympy but combining integrate and solve should do what yorue after?
Reply 3
Original post by RDKGames
Assuming a solution exists limits you to a particular class of functions ff ... your example of 1+sin⁡x1 + \sin x is not in this class so of course it failed. There is no solution to find for this choice of function.

Test it for something like f(x)=1−xf(x) = 1-x where you know the solution must be s=2s = 2.

I put the lefthand side as 0 for simplification reasons, really it should be a realisation of Z∼exp(1)Z \sim exp(1) . In which case sympy fails to solve
Reply 4
Original post by mqb2766
Are you after a numerical or symbolic solution? Not used sympy but combining integrate and solve should do what yorue after?

I'm after a numerical solution, I attempted it on python but it gives an error. The issue is that scipy doesn't accept symbolic bounds in the integral compared to sympy.

My code that I attempted is this:

scipy.optimize.fsolve(lambda x: scipy.integrate.quad(lambda x: x+1, 0, x), 0.1)

(for when f(x)=x+1)


Also I shouldn't have put 0 on the lefthand side, it should be positive, a realisation from Z∼exp(1)Z\sim exp(1)
(edited 11 months ago)
Reply 5
Original post by Student 999
I'm after a numerical solution, I attempted it on python but it gives an error. The issue is that scipy doesn't accept symbolic bounds in the integral compared to sympy.

My code that I attempted is this:

scipy.optimize.fsolve(lambda x: scipy.integrate.quad(lambda x: x+1, 0, x), 0.1)

(for when f(x)=x+1)

Does integrate() and solve() work for a sympbolic solution? Integrate() allows you to put a variable in as an upper bound which I presume could be used in solve() to get the value.

Also, I would have thought that x=0 would be the solution to your previous example?

Edit - for numerical integration in matlab for instance, Id define my own integration routine as a simple function which took the upper limit as a parmeter and did a simple trapezoid (for instance) and then pass that function across to something like fsolve.
(edited 11 months ago)
Reply 6
Original post by mqb2766
Does integrate() and solve() work for a sympbolic solution? Integrate() allows you to put a variable in as an upper bound which I presume could be used in solve() to get the value.

Also, I would have thought that x=0 would be the solution to your previous example?

Also I shouldn't have put 0 on the lefthand side, it should be positive, a realisation from Z∼exp(1)Z\sim exp(1).

I already implemented the code in sympy, it works for polynomials and other singular term functions that are easy to invert. But when I ran it for more complicated functions,z=∫si−1sif(x)z=\int_{s_{i-1}}^{s_i} f(x) (s_(i-1)<s_i) it comes up with the error saying there's no algorithm to solve it in sympy hence now I'm looking for a numerical solution for more complicated equations that sympy can't handle

apologies for not stating my question properly in the beginning
(edited 11 months ago)
Reply 7
Original post by Student 999
Also I shouldn't have put 0 on the lefthand side, it should be positive, a realisation from Z∼exp(1)Z\sim exp(1).

I already implemented the code in sympy, it works for polynomials and other singular term functions that are easy to invert. But when I ran it for z=∫0sf(x)z=\int_{0}^{s} f(x) it comes up with the error saying there's no algorithm to solve it in sympy hence now I'm looking for a numerical solution for more complicated equations that sympy can't handle

Like in wolfram
https://www.wolframalpha.com/input?i=solve+integrate+from+0+to+x+%281%2Bsin%28t%29%29+dt+%3D+e%5E1
youd not get a symbolic solution from
1 + x - cos(x) = e
But Id be surprised if you couldnt combine the symbolic integral (1+x-cos(x)) and evaluate it numerically and combine it with a numerical search. So something like combine it with subs() or evalf() and put a simple wrapper function round it?
(edited 11 months ago)
Reply 8
Original post by mqb2766
Like in wolfram
https://www.wolframalpha.com/input?i=solve+integrate+from+0+to+x+%281%2Bsin%28t%29%29+dt+%3D+e%5E1
youd not get a symbolic solution from
1 + x - cos(x) = e
But Id be surprised if you couldnt combine the symbolic integral (1+x-cos(x)) and evaluate it numerically and combine it with a numerical search. So something like combine it with subs() or evalf() and put a simple wrapper function round it?


When you say wrapper function is it like a while loop that iterates x until you're a certain tolerance away from 0?
(edited 11 months ago)
Reply 9
Original post by Student 999
When you say wrapper function is it like a while loop that iterates x until you're a certain tolerance away from 0?


fsolve calls a function, so if that function contains subs/feval/... and the symbolic integral then it shouldnt need a loop. If you dont use the symbolic integral, then youd need to do a numerical integral yourself or ... Depends on what you want to do.
(edited 11 months ago)
Reply 10
Original post by mqb2766
fsolve calls a function, so if that function contains subs/feval/... and the symbolic integral then it shouldnt need a loop. If you dont use the symbolic integral, then youd need to do a numerical integral yourself or ... Depends on what you want to do.

would you mind providing an example for your first suggestion?
Reply 11
Original post by Student 999
would you mind providing an example for your first suggestion?


Probably not. Havent got python on this machine and not used sympy (but used others). But Id guess its a combination of
* Defining your function for fsolve
https://www.youtube.com/watch?v=nnCDaHCulAU&ab_channel=APMonitor.com
* Evaluating the symbolic expression for the integral (upper limit is x say)
https://www.tutorialspoint.com/sympy/sympy_integration.htm
* Use something like lambidfy() to get the numeric value of the definite integral
https://www.tutorialspoint.com/sympy/sympy_lambdify_function.htm#:~:text=The%20lambdify%20function%20translates%20SymPy,given%20numerical%20library%2C%20usually%20NumPy.
The function should just be a couple of lines

Ideally, the symbolic integration should be evaluated one and either hard coded or passed into the function as it doesnt change, but whatever is easiest to get it working first. You could even just pass the function handle returned by lambidfy into the function fsolve calls (possibly/somehow).
(edited 11 months ago)
Reply 12
Original post by mqb2766
Probably not. Havent got python on this machine and not used sympy (but used others). But Id guess its a combination of
* Defining your function for fsolve
https://www.youtube.com/watch?v=nnCDaHCulAU&ab_channel=APMonitor.com
* Evaluating the symbolic expression for the integral (upper limit is x say)
https://www.tutorialspoint.com/sympy/sympy_integration.htm
* Use something like lambidfy() to get the numeric value of the definite integral
https://www.tutorialspoint.com/sympy/sympy_lambdify_function.htm#:~:text=The%20lambdify%20function%20translates%20SymPy,given%20numerical%20library%2C%20usually%20NumPy.
The function should just be a couple of lines

Ideally, the symbolic integration should be evaluated one and either hard coded or passed into the function as it doesnt change, but whatever is easiest to get it working first. You could even just pass the function handle returned by lambidfy into the function fsolve calls (possibly/somehow).

I've managed to fix my original code, the error that I made was that integrateequad returns a tuple.So all I had to do is index for the first term. Thanks for the links and help
(edited 11 months ago)

Quick Reply

Latest