user2151741 - 11 months ago 36

Python Question

I'm trying to use sympy to solve a polynomial equation, the coefficients of which have uncertainties. So for the uncertainties I'm trying to use the uncertainties module. Is there any way of doing the following:

`x=ufloat(10,0.2) #the xs are coefficients`

x1=ufloat(8,0.01)

x3=ufloat(25,2)

L=Symbol("L")

eqn=(x*(L**2))+(x1*(L*1))+(x3*(L**0))

solve(eqn,L) #ideally this should give the value of L with it's propagated uncertainty

without it throwing the error:

`TypeError: unsupported operand type(s) for *: 'Variable' and 'Pow'`

Answer

One solution would be to use `Symbol('x')`

and then substitute it for your ufloat (you'll probably need to use `lambdify`

to do this). This should work, assuming that SymPy is able to solve the equation in the general form with the symbolic coefficient. Since this is just a quadratic, it will. For a cubic it would too, but for higher order polynomials, you are out of luck. I'm also assuming that `ufloat`

will do the right thing when plugged into the quadratic equation.

Something like

```
x, x1, x3 = symbols('x x1 x3')
L=Symbol("L")
eqn=(x*(L**2))+(x1*(L*1))+(x3*(L**0))
s = solve(eqn,L)
lambdify([x, x1, x3], s)(ufloat(10,0.2), ufloat(8,0.01), ufloat(25,2))
```

(note there are two solutions to the quadratic, so this will give both).