Ask Your Question
1

Substituting variable/function

asked 2017-03-19 16:07:00 +0100

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

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-03-20 10:18:03 +0100

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

Comments

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

ceee gravatar imageceee ( 2017-03-20 12:17:13 +0100 )edit
0

answered 2017-03-27 12:08:10 +0100

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

Stats

Asked: 2017-03-19 16:07:00 +0100

Seen: 1,182 times

Last updated: Mar 20 '17