Home > Blockchain >  Why is this code outputting the wrong answer?
Why is this code outputting the wrong answer?

Time:10-27

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)

CodePudding user response:

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

  • Related