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.Mon, 27 Mar 2017 12:08:10 +0200Substituting variable/functionhttps://ask.sagemath.org/question/36994/substituting-variablefunction/I got a problem with substituting a solution into another equation. I could break down my problem to a simple example:
r = var('r');
p = function('p')(r);
v = var('v');
v = p + p.derivative(r);
p1(r)=desolve(p.derivative(r)==0,p,ivar=r); p1(r)
v.subs(p(r)==p1)
Output:
_C
_C + diff(p(r), r)
What's happening here? Why is p substituted, but not diff(p,r)=0?
What's the correct way to do this substitution?Sun, 19 Mar 2017 16:07:00 +0100https://ask.sagemath.org/question/36994/substituting-variablefunction/Answer by kaassama for <p>I got a problem with substituting a solution into another equation. I could break down my problem to a simple example:</p>
<pre><code>r = var('r');
p = function('p')(r);
v = var('v');
v = p + p.derivative(r);
p1(r)=desolve(p.derivative(r)==0,p,ivar=r); p1(r)
v.subs(p(r)==p1)
</code></pre>
<p>Output:</p>
<pre><code>_C
_C + diff(p(r), r)
</code></pre>
<p>What's happening here? Why is p substituted, but not diff(p,r)=0?
What's the correct way to do this substitution?</p>
https://ask.sagemath.org/question/36994/substituting-variablefunction/?answer=37094#post-id-37094Thank you for that explanation. I will try by myself your substiture_fonction method.Mon, 27 Mar 2017 12:08:10 +0200https://ask.sagemath.org/question/36994/substituting-variablefunction/?answer=37094#post-id-37094Answer by eric_g for <p>I got a problem with substituting a solution into another equation. I could break down my problem to a simple example:</p>
<pre><code>r = var('r');
p = function('p')(r);
v = var('v');
v = p + p.derivative(r);
p1(r)=desolve(p.derivative(r)==0,p,ivar=r); p1(r)
v.subs(p(r)==p1)
</code></pre>
<p>Output:</p>
<pre><code>_C
_C + diff(p(r), r)
</code></pre>
<p>What's happening here? Why is p substituted, but not diff(p,r)=0?
What's the correct way to do this substitution?</p>
https://ask.sagemath.org/question/36994/substituting-variablefunction/?answer=36998#post-id-36998You should use the method `substitute_function` instead of `subs` and use a symbol different from `p` to denote `p(r)` (otherwise there is a confusion between the function `p` and its value at `r`). Besides the semicolons at the end of each line are not necessary in Python and the line `v = var('v')` is useless, since the Python variable `v` is redeclared in the next line. So basically, your code should be:
sage: r = var('r')
sage: P = function('p')(r)
sage: v = P + P.derivative(r)
sage: p1(r) = desolve(P.derivative(r)==0, P, ivar=r); p1(r)
_C
sage: v.substitute_function(p, p1)
_C
Mon, 20 Mar 2017 10:18:03 +0100https://ask.sagemath.org/question/36994/substituting-variablefunction/?answer=36998#post-id-36998Comment by ceee for <p>You should use the method <code>substitute_function</code> instead of <code>subs</code> and use a symbol different from <code>p</code> to denote <code>p(r)</code> (otherwise there is a confusion between the function <code>p</code> and its value at <code>r</code>). Besides the semicolons at the end of each line are not necessary in Python and the line <code>v = var('v')</code> is useless, since the Python variable <code>v</code> is redeclared in the next line. So basically, your code should be:</p>
<pre><code>sage: r = var('r')
sage: P = function('p')(r)
sage: v = P + P.derivative(r)
sage: p1(r) = desolve(P.derivative(r)==0, P, ivar=r); p1(r)
_C
sage: v.substitute_function(p, p1)
_C
</code></pre>
https://ask.sagemath.org/question/36994/substituting-variablefunction/?comment=37001#post-id-37001Thank you! My lack of understanding was the difference between P and 'p'. This helped a lotMon, 20 Mar 2017 12:17:13 +0100https://ask.sagemath.org/question/36994/substituting-variablefunction/?comment=37001#post-id-37001