最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How do I adjust my code so that the sympy command 'subs' substitutes the correct variables? - Stack Ove

programmeradmin0浏览0评论

I'm having trouble using the subs command in Python using the Sympy module. It's like the command doesn't exist. The question in the code is about the chain rule when x and y depend on a common variable.

Here is my code:

import sympy as sp
from sympy import E
import math

x1, y1, t1 = sp.symbols('x y t')

# Question 2

# Method 1

pi = math.pi

f2 = E**y1 * sp.cos(x1)

dfdx = sp.diff(f2, x1)

dfdy = sp.diff(f2, y1)

xt = (2*pi / 3)*t1 + pi/4
dxdt = sp.diff(xt, t1)

yt = -t1
dydt = sp.diff(yt, t1)

dfdt = dfdx *  dxdt + dfdy * dydt
print(dfdt.subs({x1 : xt}, {y1 : yt}))

My output is just the original dfdt in terms of x and y and not in terms of t:

PS C:\Users\maxko\OneDrive\Pulpit\Python> & C:/Users/maxko/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/maxko/OneDrive/Pulpit/Python/Lab 2.py"
-2.0943951023932*exp(y)*sin(x) - exp(y)*cos(x)

Follow up question, is there any way to prettify that long float? Maybe keep it in terms of pi or just 3 sig figs?

I was expecting for the subs command to substitute the 'x' and 'y' in the 'dfdt' for the 'xt' and 'yt' that were defined above.

I'm having trouble using the subs command in Python using the Sympy module. It's like the command doesn't exist. The question in the code is about the chain rule when x and y depend on a common variable.

Here is my code:

import sympy as sp
from sympy import E
import math

x1, y1, t1 = sp.symbols('x y t')

# Question 2

# Method 1

pi = math.pi

f2 = E**y1 * sp.cos(x1)

dfdx = sp.diff(f2, x1)

dfdy = sp.diff(f2, y1)

xt = (2*pi / 3)*t1 + pi/4
dxdt = sp.diff(xt, t1)

yt = -t1
dydt = sp.diff(yt, t1)

dfdt = dfdx *  dxdt + dfdy * dydt
print(dfdt.subs({x1 : xt}, {y1 : yt}))

My output is just the original dfdt in terms of x and y and not in terms of t:

PS C:\Users\maxko\OneDrive\Pulpit\Python> & C:/Users/maxko/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/maxko/OneDrive/Pulpit/Python/Lab 2.py"
-2.0943951023932*exp(y)*sin(x) - exp(y)*cos(x)

Follow up question, is there any way to prettify that long float? Maybe keep it in terms of pi or just 3 sig figs?

I was expecting for the subs command to substitute the 'x' and 'y' in the 'dfdt' for the 'xt' and 'yt' that were defined above.

Share Improve this question edited yesterday jared 9,0013 gold badges15 silver badges43 bronze badges asked 2 days ago Max KotlarzMax Kotlarz 11 silver badge1 bronze badge New contributor Max Kotlarz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 0
Add a comment  | 

1 Answer 1

Reset to default 2

You arguments for subs are incorrect. You should pass a single dictionary with the key-value pairs.

print(dfdt.subs({x1: xt, y1: yt}))

That will give you your desired substitution.

Regarding the pi part of your question, try not to mix numerical and symbolic libraries. You use math.pi when you should probably use sympy's pi (sp.pi, in your case). Then you don't have to worry about the decimals of the printout.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论