Ask Your Question

Substitute variable in expression

asked 2014-12-04 14:46:29 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hello, I have to do a substitution in an expression: i want to substitute diff(psi) with omega, where i declared psi as psi = function('psi',t)

I tried doing expr.substitute(diff(psi)==omega) but it doesn't work.

What do I have to do?

Thank you very much.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-12-04 15:55:46 +0200

slelievre gravatar image

updated 2014-12-06 17:28:31 +0200

[Edited to answer your second question as well.]

To replace diff(psi) by say t, use substitute_expression as follows:

sage: t = var('t')
sage: psi = function('psi',t)
sage: a = 3 * diff(psi)^2 - 2 * diff(psi) + 1 
sage: a
3*D[0](psi)(t)^2 - 2*D[0](psi)(t) + 1
sage: a.substitute_expression(diff(psi)==t)
3*t^2 - 2*t + 1

To use the result of solve to perform replacement, do as follows:

sage: var('V V_f chi l psi')
sage: eq0 = V == V_f * cos(chi)
sage: eq1 = l * psi == V_f * sin(chi)
sage: sol = solve(eq0, V_f)
sage: sol
[V_f == V/cos(chi)]
sage: eq0.subs_expr(sol[0])
V == V
sage: eq1.subs_expr(sol[0])
l*psi == V*sin(chi)/cos(chi)

Note that sol is a list containing one equation. We use that equation (item 0 of the list) for the replacement.

edit flag offensive delete link more


Thank you!

Silvia gravatar imageSilvia ( 2014-12-04 18:21:36 +0200 )edit

Can i ask you another question? I've got two equations:


and i did x=solve(eq0, V_f), so x=v/cos(chi).

Now i want to substitute the value of V_f that i've just calculated into the eq2. How can i do that?

Silvia gravatar imageSilvia ( 2014-12-04 18:27:22 +0200 )edit

The x is a list, so the content you want is got with x[0]. This is an equation, and you want the right hand side of that, so you say x[0].rhs().

rws gravatar imagerws ( 2014-12-06 15:09:09 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-12-04 14:46:29 +0200

Seen: 15,643 times

Last updated: Dec 06 '14