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.Fri, 29 Oct 2021 12:45:30 +0200Assign value to symbolic function?https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/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:
\begin{equation}
f(x,y) = g(x,y) + 2x.
\end{equation}
If I calculate the derivative of $f(x,y)$ with respect to $x$:
\begin{equation}
\frac{df(x,y)}{dx}=\frac{dg(x,y)}{dx}+2.
\end{equation}
Now, I need to evaluate this at $x=0$, knowing that $\frac{dg(x,y)}{dx}\bigg\rvert_{x=0}=10$. This should give me:
\begin{equation}
\frac{df(x,y)}{dx}\bigg\rvert_{x = 0} = \frac{dg(x,y)}{dx}\bigg\rvert_{x = 0} +2=12
\end{equation}
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 $\frac{dg(x,y)}{dx}\bigg\rvert_{x=0}=10$,
2) evaluate $\frac{df(x,y)}{dx}$ at $x=0$, so that I obtain $\frac{df(x,y)}{dx}\bigg\rvert_{x = 0} = 12$.Fri, 29 Oct 2021 10:56:21 +0200https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/Answer by rburing for <p>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:
\begin{equation}
f(x,y) = g(x,y) + 2x.
\end{equation}</p>
<p>If I calculate the derivative of $f(x,y)$ with respect to $x$:
\begin{equation}
\frac{df(x,y)}{dx}=\frac{dg(x,y)}{dx}+2.
\end{equation}</p>
<p>Now, I need to evaluate this at $x=0$, knowing that $\frac{dg(x,y)}{dx}\bigg\rvert_{x=0}=10$. This should give me:
\begin{equation}
\frac{df(x,y)}{dx}\bigg\rvert_{x = 0} = \frac{dg(x,y)}{dx}\bigg\rvert_{x = 0} +2=12
\end{equation}</p>
<p>The code I have written to achieve this is the following:</p>
<pre><code>x = var('x')
y = var('y')
g = function('g')(x,y) #symbolic function
f = g + 2*x
der_f = diff(f,x); der_f
</code></pre>
<p>and this is what I get:</p>
<pre><code>diff(g(x, y), x) + 2
</code></pre>
<p>as I expected. However, I don't know how to follow. In particular, I need to know how to:</p>
<p>1) assign $\frac{dg(x,y)}{dx}\bigg\rvert_{x=0}=10$,</p>
<p>2) evaluate $\frac{df(x,y)}{dx}$ at $x=0$, so that I obtain $\frac{df(x,y)}{dx}\bigg\rvert_{x = 0} = 12$.</p>
https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/?answer=59527#post-id-59527Instead of "assigning" a value to $\partial g/\partial x \vert_{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)
12Fri, 29 Oct 2021 11:39:49 +0200https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/?answer=59527#post-id-59527Comment by rburing for <p>Instead of "assigning" a value to $\partial g/\partial x \vert_{x=0}$ beforehand, it's easier to make the substitution afterward:</p>
<pre><code>sage: der_f.subs(x==0).subs(diff(g,x).subs(x==0) == 10)
12
</code></pre>
<p>Or in steps, mimicking the order you proposed:</p>
<pre><code>sage: what_i_know = diff(g,x).subs(x==0) == 10
sage: der_f.subs(x==0).subs(what_i_know)
12
</code></pre>
https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/?comment=59529#post-id-59529Maybe it's possible by specifying a `derivative_func` in the definition of `g`, returning another [symbolic function](https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/function_factory.html#sage.symbolic.function_factory.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.Fri, 29 Oct 2021 12:45:30 +0200https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/?comment=59529#post-id-59529Comment by keko for <p>Instead of "assigning" a value to $\partial g/\partial x \vert_{x=0}$ beforehand, it's easier to make the substitution afterward:</p>
<pre><code>sage: der_f.subs(x==0).subs(diff(g,x).subs(x==0) == 10)
12
</code></pre>
<p>Or in steps, mimicking the order you proposed:</p>
<pre><code>sage: what_i_know = diff(g,x).subs(x==0) == 10
sage: der_f.subs(x==0).subs(what_i_know)
12
</code></pre>
https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/?comment=59528#post-id-59528That 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?Fri, 29 Oct 2021 12:00:22 +0200https://ask.sagemath.org/question/59524/assign-value-to-symbolic-function/?comment=59528#post-id-59528