Home > Back-end >  Sympy 'solve' returns 'list index out of range' error
Sympy 'solve' returns 'list index out of range' error

Time:01-02

I am using JuypterLab trying to solve a simple algebra problem:

3 * r**0.25 * s**0.75 = 14.086

I need to solve for s for values of r between 3 and 17.

import numpy as np
import pandas  
import sympy as sym
from sympy import symbols, solve




s = Symbol('s')

expr = 3 * 18**0.25 * s**0.75 = 14.086

sol = solve(expr)

num = sol[10]
num

When I run the code I get the list out of range error

I also tried

s = Symbol('s')

expr = 3 * 18**0.25 * s**0.75

sol = solve(expr)
u = 14.086

num = sol[u]
num

When I run this code I get list indices must be integers or slices, not float

I would like to find the value of s for values of r between 3 and 17.

CodePudding user response:

Equations in sympy are written using Eq(). You can call solve with such an equation (or a list of multiple equations). You can tell solve that you look for an expression for s given r, by providing s as a second parameter.

The result of solve can take a few different forms, depending on the equations. Usually it is a list, and in this case there is only one expression that solves for s, so a list with one element. (E.g. a quadratic equation could have multiple solutions.)

The result of solve often isn't a single number, but a symbolic expression. In this case, it is an expression involving the variable r. To get a value for s for a specific r (e.g. r=10), expr.subs(r, 10) can be used. Sometimes the solution is numeric, but still in its symbolic form (e.g. sqrt(2)). Calling .evalf() will give a fully numeric solution.

from sympy import Symbol, solve, Eq, plot

r = Symbol('r')
s = Symbol('s')

equation = Eq(3 * r**0.25 * s**0.75, 14)
sol = solve(equation, s) # [7.79846345438935/r**(1/3)]
s_of_r = sol[0]
for ri in range(3, 18):
    si = s_of_r.subs(r, ri).evalf()
    print(f"for r={ri} the corresponding value for s={si:.6f}")

Sympy can also do some basic plotting:

plot(s_of_r, (r, 3, 17))

plot of s vs r

CodePudding user response:

Regarding the first error "When I run the code I get the list out of range error":

  • this is because sol is a one element list, and you are trying to access the tenth element that doesn't exist.

Regarding the second error "When I run this code I get list indices must be integers or slices, not float"

  • this is because to access a position of a list you are trying with a decimal, which besides not working it doesn't make sense

As for the output of sol it is [0.0].

Isn't the code bellow what you are looking for? Cause It seems to me you wanto to find out the output of the expression when s is 14.086, right?

import numpy as np
import pandas  
import sympy as sym
from sympy import Symbol, solve

s = Symbol('s')

expr = 3 * 18**0.25 * s**0.75

y = expr.replace(s, 14.086)
print(y)

Instead, if you want to find the value of sol given a expression value you can use this approach

# equation example 3   s, given an output of 5
y = solveset(Eq(3   s, 5), s)
print(y)
  • Related