ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 06 Dec 2014 15:09:09 +0100Substitute variable in expressionhttps://ask.sagemath.org/question/25119/substitute-variable-in-expression/ 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.Thu, 04 Dec 2014 14:46:29 +0100https://ask.sagemath.org/question/25119/substitute-variable-in-expression/Answer by slelievre for <p>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)</p>
<p>I tried doing expr.substitute(diff(psi)==omega) but it doesn't work.</p>
<p>What do I have to do?</p>
<p>Thank you very much.</p>
https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?answer=25120#post-id-25120[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.Thu, 04 Dec 2014 15:55:46 +0100https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?answer=25120#post-id-25120Comment by Silvia for <p>[Edited to answer your second question as well.]</p>
<p>To replace <code>diff(psi)</code> by say <code>t</code>, use <code>substitute_expression</code> as follows:</p>
<pre><code>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
</code></pre>
<p>To use the result of <code>solve</code> to perform replacement, do as follows:</p>
<pre><code>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)
</code></pre>
<p>Note that <code>sol</code> is a list containing one equation. We use that equation (item 0 of the list) for the replacement.</p>
https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?comment=25126#post-id-25126Thank you!Thu, 04 Dec 2014 18:21:36 +0100https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?comment=25126#post-id-25126Comment by rws for <p>[Edited to answer your second question as well.]</p>
<p>To replace <code>diff(psi)</code> by say <code>t</code>, use <code>substitute_expression</code> as follows:</p>
<pre><code>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
</code></pre>
<p>To use the result of <code>solve</code> to perform replacement, do as follows:</p>
<pre><code>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)
</code></pre>
<p>Note that <code>sol</code> is a list containing one equation. We use that equation (item 0 of the list) for the replacement.</p>
https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?comment=25147#post-id-25147The `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()`.Sat, 06 Dec 2014 15:09:09 +0100https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?comment=25147#post-id-25147Comment by Silvia for <p>[Edited to answer your second question as well.]</p>
<p>To replace <code>diff(psi)</code> by say <code>t</code>, use <code>substitute_expression</code> as follows:</p>
<pre><code>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
</code></pre>
<p>To use the result of <code>solve</code> to perform replacement, do as follows:</p>
<pre><code>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)
</code></pre>
<p>Note that <code>sol</code> is a list containing one equation. We use that equation (item 0 of the list) for the replacement.</p>
https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?comment=25128#post-id-25128Can 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?Thu, 04 Dec 2014 18:27:22 +0100https://ask.sagemath.org/question/25119/substitute-variable-in-expression/?comment=25128#post-id-25128