ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 28 Dec 2021 08:07:32 +0100How can I substitute an unknown function in an expression with a known function?https://ask.sagemath.org/question/60468/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?
Tue, 28 Dec 2021 03:11:56 +0100https://ask.sagemath.org/question/60468/how-can-i-substitute-an-unknown-function-in-an-expression-with-a-known-function/Answer by Emmanuel Charpentier for <p>I have defined an unknown function "p" with command</p>
<pre><code>p=function("p")
</code></pre>
<p>Then I deduced another function, which contains "p":</p>
<pre><code>aws(time)=921600*(-1333.33340000000*time + 2000)/(-3.36000004800000e6*p(time) + 3840000)
</code></pre>
<p>Now I would like to plot the function aws(time), by subtituting p(time) with a known function, like:</p>
<pre><code>p(time)=time^0.54
</code></pre>
<p>I discovered I can't use .subs() as with free variables, so this doesn't work:</p>
<pre><code> aws.subs(p(time)==time^0.54)
</code></pre>
<p>Then how can I substitute the known function, to be able to plot aws(time) finally?</p>
https://ask.sagemath.org/question/60468/how-can-i-substitute-an-unknown-function-in-an-expression-with-a-known-function/?answer=60471#post-id-60471You can `subs`titute 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](https://www.sagemath.org/sagebook/english.html) (along a a good Python tutorial, of which there is legions...) to "get the hang" of Sage...
HTH,Tue, 28 Dec 2021 08:07:32 +0100https://ask.sagemath.org/question/60468/how-can-i-substitute-an-unknown-function-in-an-expression-with-a-known-function/?answer=60471#post-id-60471