Subs in a python list

1

I have a small program in Python that calculates 4 equations based on a function and its derivatives.

The problem comes when I use the solution of the first equation and try to replace it in another equation. I get the error of:

  

the list attribute is not compatible with subs.

Does anyone know how I can modify the following equations based on the first result?

Note. the program continues later but I am interested first to solve this error that focuses on the last lines.

import sympy as sp

g, a, A, C, D, K, x = sp.symbols("g a A C D K x")

X=(A*sp.cos(g*x/a))+(sp.sin(g*x/a))+(C*sp.exp(-g*x/a))+(D*sp.exp(-g*(a-x)/a))

dx1=sp.diff(X,x,1)

dx2=sp.diff(X,x,2)

eq1= sp.Eq(X.subs(x,0),0)
eq2= sp.Eq(dx2.subs(x,0)/dx1.subs(x,0),K)
eq3= sp.Eq(X.subs(x,a),0)
eq4= sp.Eq(dx2.subs(x,a)/dx1.subs(x,a),-K)

D1=sp.solve(eq1,D)
eq33=D1.subs(D,D1)
    
asked by user51310 13.07.2017 в 09:38
source

1 answer

1

Your problem is on the line:

eq33=D1.subs(A,D1)

Where D1 you get in the previous line as D1=sp.solve(eq1,D) . The sp.solve method returns a list with the possible solutions to the equation. To apply the subs method you must do it on each expression of the list and not on the list:

D1=sp.solve(eq1,D)
eq33 = [expr.subs(D, expr) for expr in D1]

eq33 is a list of expressions just like D1 .

An example where you see how solve returns a list with the solutions and how to subtitle a symbol in all of them by going through that list:

>>> import sympy as sp    
>>> x, y = sp.symbols("x y")
>>> eq = (x**2 + 3*y + 42) 
>>> sol = sp.solve(eq,x)
>>> print(sol)
[-sqrt(-3*y - 42), sqrt(-3*y - 42)]
>>> sub = [expr.subs(y, expr) for expr in sol]
>>> print(sub)
[-sqrt(3*sqrt(-3*y - 42) - 42), sqrt(-3*sqrt(-3*y - 42) - 42)]

You can iterate, index, use list.pop() , etc to get the expressions from the list. If you know for sure that% co_of% always returns a single expression as a solution, you can simply do:

eq33=D1[0].subs(D, D1[0])
    
answered by 13.07.2017 в 12:39