-1

I am New to Python and am trying to solve an algebraic equation and having some trouble.

The Equation.

1.881 + 0.5 = x + ((2.2*2*1)^2)/(2*9.81*(1^2)*(x^2))

I can solve this in my calculator but would rather have python do this for me to make it automated

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
JFran
  • 1
  • 1
  • Please see https://stackoverflow.com/questions/22742951/solve-an-equation-using-a-python-numerical-solver-in-numpy – Eftal Gezer Sep 19 '22 at 21:40
  • Rushing a bit you have an equation of third grade, kind of: `cx^3-acx^2+b=0` for your case. So have a look at some numerical methods to solve this kind of equation. Check Newton Raphson for example. Regards. – Luis Alejandro Vargas Ramos Sep 19 '22 at 21:43
  • 1
    @JFran please add a clear, answerable question to your post ... this site is not a forum, it is a Q&A site – jsotola Sep 19 '22 at 21:56
  • rearranging this is simply: `x^3 - 2.381 (x^2) + (19.36/19.62) = 0` which is of the form `x^3 - b (x^2) + c = 0` – D.L Sep 20 '22 at 00:00
  • If you are not specified to only Python, I know there are many good websites that solves the equation for you. For example, https://www.symbolab.com/ –  Sep 20 '22 at 00:05

1 Answers1

1

re-arrange to this:

y = x**3 - 2.381 (x**2) + (19.36/19.62)

Then seek values where y = 0

from matplotlib import pyplot as plt
import numpy as np

b = - 2.381
c = 19.36/19.62

x = np.linspace(-1, 3, num=200)
y = []

x1 = [min(x),max(x)]
y1 = [0,0]

for i in x:
    y.append(i**3 + b*(i**2) + c)



fig, ax = plt.subplots()

ax.set_xlabel('x value')
ax.set_ylabel('y value')


plt.plot(x,y)
plt.plot(x1,y1)


plt.show()

which gives this:

enter image description here

You could do newton-raphson to find the values from there or just seek values of x linearly (which is slower).

D.L
  • 4,339
  • 5
  • 22
  • 45