# Substitute a function in a formal derivative

This gives a nice result.

var('p w0 c g dc dg dp dw0')
EUa=function('EUa')(p,w0,c,g)
EUa_c=diff(EUa,c)
EUa_g=diff(EUa,g)
#
EUna=function('EUna')(p,w0,c,g)
EUna_g=diff(EUna,g)
EUna_c=diff(EUna,c)
##
dEUa=EUa_c*dc + EUa_g*dg
dEUna=EUna_c*dc + EUna_g*dg
###
show(EUa)
show(EUna)
show(dEUa)
show(dEUna)
sol=solve(dEUa==dEUna, dg)
sol=(sol[0]/dc).full_simplify()
show(sol)


But now, I would like to substitute to EUa(p,w0,c,g) = p*U(w0)+(1-p)*U(0) and EUna(p,w0,c,g) = p*U(w0-c)+(1-p)*U(g*w0-c) then, later change the unknown function U(w) to say ln(w) or w^(1/2). I suppose I need to define first a function U=function('U')(w) after to define w as a variable. But all my tentatives fail.

edit retag close merge delete

Hints :

• use variable names in formal differentiations (e. g. diff(f(x,y).x) instead of diff(f, x)).

• lookup substitute_function?

( 2020-12-03 21:06:11 +0200 )edit

SageMath 9.2 notebook , W10

substitute_function?


Object substitute_function not found.

( 2020-12-04 09:13:06 +0200 )edit

It's a method of symbolic expression objects. Dotting the "i"s and crossing the "t"s, try :

sage: x.substitute_function?
Docstring:
Return this symbolic expressions all occurrences of the function
*original* replaced with the function *new*.


[ Snip...]

( 2020-12-04 15:13:13 +0200 )edit

Sort by » oldest newest most voted

You should write

EUa = function('EUa')


EUa = function('EUa')(p,w0,c,g)


in order to distinguish the function EUa from its value at the point (p,w0,c,g). In this way, you will be able to use the method substitute_function to perform the substitution that you wish. If you want a shortcut for EUa(p,w0,c,g), then you should give it a name distinct from EUa. The next step to achieve what you want is to introduce a callable symbolic expression that contains the value that you want to substitute, e.g.

U = function('U')
EUa_U(p,w0,c,g) = p*U(w0) + (1-p)*U(0)
EUna_U(p,w0,c,g) = p*U(w0-c) + (1-p)*U(g*w0-c)


Then you can perform the substitution via

sol_U = sol.substitute_function(EUa, EUa_U).substitute_function(EUna, EUna_U)


Finally, the second operation that you mention is achieved by

U_1(w) = ln(w)
sol_U.substitute_function(U, U_1)


Here is the full example:

var('p w0 c g dc dg dp dw0')
EUa = function('EUa')
EUa_c = diff(EUa(p,w0,c,g), c)
EUa_g = diff(EUa(p,w0,c,g), g)
#
EUna = function('EUna')
EUna_g = diff(EUna(p,w0,c,g), g)
EUna_c = diff(EUna(p,w0,c,g), c)
#
dEUa = EUa_c*dc + EUa_g*dg
dEUna = EUna_c*dc + EUna_g*dg
#
sol = solve(dEUa==dEUna, dg)
sol = (sol[0]/dc).full_simplify()
#
U = function('U')
EUa_U(p,w0,c,g) = p*U(w0) + (1-p)*U(0)
EUna_U(p,w0,c,g) = p*U(w0-c) + (1-p)*U(g*w0-c)
sol_U = sol.substitute_function(EUa, EUa_U).substitute_function(EUna, EUna_U)
#
U_1(w) = ln(w)
sol_U.substitute_function(U, U_1)


The outcome is

dg/dc == (g*w0 - c)*((p - 1)/(g*w0 - c) + p/(c - w0))/((p - 1)*w0)

more

Thanks a lot. I was just trying to do whazt you have perform nicely but without the distinction of the function and its value to the point.

( 2020-12-04 14:27:43 +0200 )edit