Ask Your Question

Substitute a function in a formal derivative

asked 2020-12-03 18:32:39 +0200

Cyrille gravatar image

updated 2020-12-03 18:49:06 +0200

This gives a nice result.

var('p w0 c g dc dg dp dw0')
dEUa=EUa_c*dc + EUa_g*dg 
dEUna=EUna_c*dc + EUna_g*dg
sol=solve(dEUa==dEUna, dg)

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 flag offensive 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?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-12-03 21:06:11 +0200 )edit

Hi @Emmanuel Charpentier

SageMath 9.2 notebook , W10


Object substitute_function not found.

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

Object substitute_function not found.

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

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

[ Snip...]

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-12-04 15:13:13 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-04 11:26:09 +0200

eric_g gravatar image

You should write

EUa = function('EUa')

instead of

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)
edit flag offensive delete link 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.

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

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


Asked: 2020-12-03 18:32:39 +0200

Seen: 87 times

Last updated: Dec 04 '20