Ask Your Question

Substituting variable/function

asked 2017-03-19 10:07:00 -0500

this post is marked as community wiki

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)


_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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-03-20 04:18:03 -0500

eric_g gravatar image

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)
sage: v.substitute_function(p, p1)
edit flag offensive delete link more


Thank you! My lack of understanding was the difference between P and 'p'. This helped a lot

ceee gravatar imageceee ( 2017-03-20 06:17:13 -0500 )edit

answered 2017-03-27 05:08:10 -0500

Thank you for that explanation. I will try by myself your substiture_fonction method.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2017-03-19 10:07:00 -0500

Seen: 148 times

Last updated: Mar 20