Processing math: 100%
Ask Your Question
2

Assign value to symbolic function?

asked 3 years ago

keko gravatar image

I have a symbolic function g(x,y), which depends on the variables x and y. Using this, I define the function f(x,y) as: f(x,y)=g(x,y)+2x.

If I calculate the derivative of f(x,y) with respect to x: df(x,y)dx=dg(x,y)dx+2.

Now, I need to evaluate this at x=0, knowing that dg(x,y)dxx=0=10. This should give me: df(x,y)dxx=0=dg(x,y)dxx=0+2=12

The code I have written to achieve this is the following:

x = var('x')
y = var('y')
g = function('g')(x,y) #symbolic function
f = g + 2*x
der_f = diff(f,x); der_f

and this is what I get:

diff(g(x, y), x) + 2

as I expected. However, I don't know how to follow. In particular, I need to know how to:

1) assign dg(x,y)dxx=0=10,

2) evaluate df(x,y)dx at x=0, so that I obtain df(x,y)dxx=0=12.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 3 years ago

rburing gravatar image

updated 3 years ago

Instead of "assigning" a value to g/x|x=0 beforehand, it's easier to make the substitution afterward:

sage: der_f.subs(x==0).subs(diff(g,x).subs(x==0) == 10)
12

Or in steps, mimicking the order you proposed:

sage: what_i_know = diff(g,x).subs(x==0) == 10
sage: der_f.subs(x==0).subs(what_i_know)
12
Preview: (hide)
link

Comments

That definitely did the work! Nevertheless, is there a way I can set the value of ∂g/∂x|x=0 beforehand, so that it automatically does the substitution ∂g/∂x|x=0 = 10 every time it finds that expression?

keko gravatar imagekeko ( 3 years ago )

Maybe it's possible by specifying a derivative_func in the definition of g, returning another symbolic function with a custom eval_func, but even if it worked (I didn't manage) it would be very awkward and convoluted to define. I think it's better to be explicit about such substitutions anyway.

rburing gravatar imagerburing ( 3 years ago )

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 384 times

Last updated: Oct 29 '21