# Substituting variable/function

asked 2017-03-19 16:07:00 +0200 This post is a wiki. Anyone with karma >750 is welcome to improve it.

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?

edit retag close merge delete

Sort by » oldest newest most voted

You 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

more

Thank you! My lack of understanding was the difference between P and 'p'. This helped a lot Thank you for that explanation. I will try by myself your substiture_fonction method.

more