# Substitute variable in expression

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 close merge delete

Sort by » oldest newest most voted

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.

more

Thank you!

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

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

eq0=V==V_f*cos(chi)eq1=l*psi==V_f*sin(chi)

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?

( 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().

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