Reputation: 53
I want to write a code to solve for "y" with relative error %1 in equation below:
In the equation we have the values of "b=2, x=1, n=0.015, S_0=0.002, Q=21
" and y should be calculated.
I write this code:
b=float(input('b='))
x=float(input('x= '))
n=float(input('n= '))
s=float(input('S_0= '))
Q=float(input('Q= '))
Q=(1/n)*((y*(b+x*y))**(5/3))/((b+2*y*(1+x**2)**(1/2))**(2/3)))*s
print(y)
It doesn't work.
I am not familiar how to solve the implicit equation in python. if it wasn't implicit I could write the equation with respect to y then write inputs. but here I don't know what I should do.
Upvotes: 3
Views: 9338
Reputation: 331
You can rephrase your equation by defining like below f(y) and then find the root of it with fsolve
from scipy.optimize import fsolve
def f(y,b=2,x=1,n=0.015,S_0=0.002,Q=21):
return (1/n)*((y*(b+x*y))**(5/3))/((b+2*y*(1+x**2)**(1/2))**(2/3))*S_0-Q
a=fsolve(f,1)
print(a)
print(f(a))
Upvotes: 5