# How can I substitute an unknown function in an expression with a known function?

I have defined an unknown function "p" with command

p=function("p")


Then I deduced another function, which contains "p":

aws(time)=921600*(-1333.33340000000*time + 2000)/(-3.36000004800000e6*p(time) + 3840000)


Now I would like to plot the function aws(time), by subtituting p(time) with a known function, like:

p(time)=time^0.54


I discovered I can't use .subs() as with free variables, so this doesn't work:

 aws.subs(p(time)==time^0.54)


Then how can I substitute the known function, to be able to plot aws(time) finally?

edit retag close merge delete

Sort by » oldest newest most voted

You can substitute the expression p(time) by an expression having the desired value :

sage: p=function("p")
sage: aws(time)=921600*(-1333.33340000000*time + 2000)/(-3.36000004800000e6*p(time) + 3840000)
sage: aws(time).subs(p(time)==(time^0.54))
(-1.22880006144000e9*time + 1843200000)/(-3.36000004800000e6*time^0.540000000000000 + 3840000)


You can also explicitly define your new function, then use the substitute_function method :

sage: foo(time)=time^0.54
sage: aws(time).substitute_function(p,foo)
(-1.22880006144000e9*time + 1843200000)/(-3.36000004800000e6*time^0.540000000000000 + 3840000)


You can also dispense with explicitly defining foo, and use the function method of symbolic expressions to create an anonymous function on the fly :

sage: aws(time).substitute_function(p,(time^0.54).function(time))
(-1.22880006144000e9*time + 1843200000)/(-3.36000004800000e6*time^0.540000000000000 + 3840000)


Note : your code may be easier to read if you use either supplementary arguments, symbolic variables or Python variables in place of your numeric constants :

# Python variables :

sage: c1 = -1.22880006144000e9
sage: c2 = 1843200000
sage: c3 = -3.36000004800000e6
sage: c4 = 3840000
sage: aws2(time) = (c1*time + c2)/(c3*p(time) + c4) ; aws2
time |--> (-1.22880006144000e9*time + 1843200000)/(-3.36000004800000e6*p(time) + 3840000)

# Symbolic variables

sage: var("k1, k2, k3, k4")
sage: aws3(time) = (k1*time + k2)/(k3*p(time) + k4) ; aws3(time).subs({k1:-1.22880006144000e9, k2:1843200000, k3:-3.36000004800000e6, k4:3840000})
(-1.22880006144000e9*time + 1843200000)/(-3.36000004800000e6*p(time) + 3840000)

# Symbolic arguments

sage: aws4(time, k1, k2, k3, k4) = (k1*time + k2)/(k3*p(time) + k4) ; aws4(time, -1.22880006144000e9, 1843200000, -3.36000004800000e6, 3840000)
(-1.22880006144000e9*time + 1843200000)/(-3.36000004800000e6*p(time) + 3840000)


I strongly recommend this book (along a a good Python tutorial, of which there is legions...) to "get the hang" of Sage...

HTH,

more