Given U(x) = ((x^2-1)^2- x^2) / ( x*(x^2-1)) , I'm trying to solve this equation :
U(x)- 1/U(x) = x using sympy and this is my code :
from sympy import *
x=symbols('x')
P,Q=x**2-1,x
t=(P**2-Q**2)/(P*Q)
print(solve(Eq(t-1/t,x),x))
I got a very long list (squeezed text with 1394 lines ) which is wrong comparing to the right solution i have got on wolfram alpha ( this is the right list : l3=[-0.507713305942872,0.507713305942872,-0.777861913430206,0.777861913430206,-1.46190220008154,1.46190220008154]
How can i get the same result in python using sympy ?
If you consider the numeric solutions
[-0.507713305942872,0.507713305942872,-0.777861913430206,0.777861913430206,-1.46190220008154,1.46190220008154]
to be correct, solve is probably not the right tool to use. These solutions are not actually "correct" they are just very very close. solve is trying to find an analytic i.e. 100% perfect solution. If you are fine with a little error you should use nsolve (numeric solve) instead of solve.
Also it seems you have an error in your equation code. I let sympy nsolve them, got a solution that wasn't among the WA ones. So I rewrote the equation and got nsolve to give me one of WA's solutions:
from sympy import *
x=symbols('x')
U = ((x**2-1)**2- x**2) / (x*(x**2-1))
eqn = U - 1/U - x
nsolve(eqn,x,0.1)
This yields 0.507713305942872. Which is the closest solution to the start value of 0.1.
As Oscar Benjamin has pointed out in the comments you can get all numeric solutions with
from sympy import *
x=symbols('x')
U = ((x**2-1)**2- x**2) / (x*(x**2-1))
eqn = U - 1/U - x
Poly(eqn.as_numer_denom()[0]).nroots()
By default nroots calculates 50 decimal places but you can specify how many you want by giving a keyword argument like so .nroots(n=decimal_places)
Related
I'm new to both StackOverflow and coding so I appreciate any and all support!
I'm trying to solve for x in this equation: x = (p^4).((15-(4p))-((10(p^2))/(1-((2p).(1-p)))))
I wrote the below Python code in PyCharm in order to calculate 'x'.
When p=60, the output I expect is 0.735729231 but the output I get when I run the code is [-2981888998.72899]
I'd appreciate some guidance on what I'd need to change in order to output the expected value.
Many thanks!
PS: for reference: here is an image showing the equation written in its original format
import numpy as np
from sympy import *
p = 60
x = symbols('x')
eqn = Eq(x,(p**4)*((15-(4*p))-((10*(p**2))/(1-((2*p)*(1-p))))))
sol = solve(eqn,x)
print(sol)
I think there something wrong with your calculation or your equation.
You can do this directly without any packages,
p = 60
print((p**4)*((15-(4*p))-((10*(p**2))/(1-((2*p)*(1-p))))))
You don't need sympy to solve this because all the p variables are on the right side already. x is basically just your answer for subbing in p = 60 and expanding and solving the equation.
And You get the same number -2981888998.72899
I'm testing dsolve method of sympy package in order to check if any differential can be solved and return solution if it is. Trying to solve equation of this kind
from sympy import dsolve
t = symbols('t')
x = Function('x')
dsolve(x(t).diff(t) - x(t)**2 + t**2)
returns an answer:
The last term of this expression tells me that an equation doesn't have explicit solution. How can I check if equation has an explicit solution using sympy?
I hope something like this:
is_solution_explicit(x(t).diff(t) - x(t)**2 + t**2) returns False
is_solution_explicit(2*x(t)/(1+t) + (1+t)**3) returns True
I am very new to programming, and had to use sympy for a school project.
I think I get that using nonlinsolve to return an angle gives an ImageSet with the angle + 2n*pi. However I want it to return only the value of the angle (in the interval [0,pi/2]), and as one value and not an interval.
from sympy import nonlinsolve, symbols,cos
x=symbols('x')
print(nonlinsolve([cos(x)-1],[x]).args[0][0])
I want the result to be 0 and not 2*n*pi.
Clarification : I know that the result is correct, but I only want one value, that I can use algebraically, and I don't know how Sympy works (how to manipulate ImageSets)
So I might be wrong because i dont use sympy, but the solution that solvers return seems to be corect to me.
ImageSet(Lambda(_n, 2*_n*pi), Integers)
From what I understand solver returned lambda function. Cosinus is a cyclic function which means it reapeats it's value every 2PI. So the solver says first solution (_n = 0) is 0, second (_n = 1) is 2pi and so on.
look at the function plot and it will hopefully make sense:
Wolfram Alpha - (cos(x) - 1)
EDIT: I think you need to use intersect method of imageset like this( note that intersect returns all the intersections, here i selected just the first one):
from sympy import nonlinsolve, symbols,cos, Interval
import math
x = symbols('x')
f = nonlinsolve([cos(x)-1], [x]).args[0][0]
sol = f.intersect(Interval(0, math.pi/2)).args[0]
print(sol)
I'm doing some manipulation of trig equations and would like the results back in trig form.
What I'm doing is this:
from sympy import *
B,D,a=symbols(r'B,D,alpha',real=True,positive=True)
eq1=Eq(D,B*((sin(a)*sin(a))/(sin(a+a))))
solve(eq1,a)
I expect the result to be atan(2*D/B) but I'm getting:
[-I*log(-sqrt((B + 2*I*D)/(B - 2*I*D))), -I*log((B + 2*I*D)/(B - 2*I*D))/2]
I know sympy is expanding the trig functions into exponential form, but I can't seem to convince it to convert the results back.
I've tried:
[n.rewrite(atan) for n in solve(eq1,a)]
but I get the same result back...
If you simplify before solving, the result looks better.
>>> solve(eq1.simplify(), a)
[atan(2*D/B)]
Also, the more mathematically rigorous solveset (a modern alternative to solve) returns a more mathematically correct answer without the need for simplification:
>>> solveset(eq1, a)
ConditionSet(alpha, Eq(tan(alpha)/2 - D/B, 0), Reals)
The point being that there are infinitely many solutions, so they cannot be given as a list: so, solveset presents them as the set of all alpha such that tan(alpha) is 2*D/B.
I am using Python with Sympy.
I need to solve the following equation, finding the 4 roots (omega is my unknown):
deter= 0.6*omega**4*cos(omega*t)**2 - 229.0*omega**2*cos(omega*t)**2 + 5880.0*cos(omega*t)**2
I tried to use solve:
eqcarr=solve(deter,omega,exclude=[t])
I get this output:
[-18.8143990830350, -5.26165884593044, 5.26165884593044, 18.8143990830350, 1.5707963267949/t, 4.71238898038469/t]
I only need the first 4 values, and not the values with the t coefficient. I expect the cos(omega*t)**2 to be simplified in solve, but this doesn't happen.
According to documentation solve will not solve for any of the free symbols passed in the exclude.
'exclude=[] (default)'
don't try to solve for any of the free symbols in exclude;
if expressions are given, the free symbols in them will
be extracted automatically.
It is not meant to filter solution.
You can solve your problem by doing this:
In [10]: from sympy import *
In [11]: from sympy.abc import omega, t
In [12]: deter= 0.6*omega**4*cos(omega*t)**2 - 229.0*omega**2*cos(omega*t)**2 + 5880.0*cos(omega*t)**2
In [13]: eqcarr=solve(deter,omega,exclude=[t])
In [14]: filtered = [i for i in eqcarr if not i.has(t)]
In [15]: filtered
Out[15]: [-18.8143990830350, -5.26165884593044, 5.26165884593044, 18.8143990830350]