ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 31 Oct 2018 15:46:08 -0500Symbolic Taylor expansionhttp://ask.sagemath.org/question/44125/symbolic-taylor-expansion/I would like to expand the symbol function $f$ as a Taylor series
$$\delta f(x)=\delta x\frac{d}{dx}f+\frac12(\delta x)^2\frac{d^2}{dx^2}f+O((\delta x)^3)$$
with
$$\delta x = a_1(\delta t)^{\frac12}+a_2(\delta t)+a_3(\delta t)^{\frac32}+O((\delta t)^2)$$
and expand and collect the same power terms of $\delta t$ up to a designated power, say, $\frac32$. $f$ is just a symbol, I just need Mathsage to produce the symbols of derivatives $\frac{d}{dx}$.
How should one set this up?
-----------------------
Inspired by eric_g's answer to this question, I am able to perform the same operation to $f(x,y)$ with
$$\delta y = a_1(\delta s)^{\frac12}+a_2(\delta s)+a_3(\delta s)^{\frac32}+O((\delta s)^2)$$.
I would like to collect terms according to the powers $dt^{\frac i2}ds^{\frac j2}$ where $i$ and $j$ are nonnegative integers and arranged in the ascending order of $i+j$. I would also like to have the capacity to collect terms according to the order of some other variables, say, $a_1$. I have tried using
g.full_simplify().maxima_methods().collectterms(dt)
for a single variable $dt$. But it does not seem to work well for the fractional power. I do not know if it will work for $dt^{\frac i2}ds^{\frac j2}$.
How can I set this up?Tue, 30 Oct 2018 20:08:34 -0500http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/Answer by eric_g for <p>I would like to expand the symbol function $f$ as a Taylor series
$$\delta f(x)=\delta x\frac{d}{dx}f+\frac12(\delta x)^2\frac{d^2}{dx^2}f+O((\delta x)^3)$$
with
$$\delta x = a_1(\delta t)^{\frac12}+a_2(\delta t)+a_3(\delta t)^{\frac32}+O((\delta t)^2)$$
and expand and collect the same power terms of $\delta t$ up to a designated power, say, $\frac32$. $f$ is just a symbol, I just need Mathsage to produce the symbols of derivatives $\frac{d}{dx}$.</p>
<p>How should one set this up?</p>
<hr>
<p>Inspired by eric_g's answer to this question, I am able to perform the same operation to $f(x,y)$ with
$$\delta y = a_1(\delta s)^{\frac12}+a_2(\delta s)+a_3(\delta s)^{\frac32}+O((\delta s)^2)$$.
I would like to collect terms according to the powers $dt^{\frac i2}ds^{\frac j2}$ where $i$ and $j$ are nonnegative integers and arranged in the ascending order of $i+j$. I would also like to have the capacity to collect terms according to the order of some other variables, say, $a_1$. I have tried using </p>
<p>g.full_simplify().maxima_methods().collectterms(dt)</p>
<p>for a single variable $dt$. But it does not seem to work well for the fractional power. I do not know if it will work for $dt^{\frac i2}ds^{\frac j2}$.</p>
<p>How can I set this up?</p>
http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?answer=44129#post-id-44129You can do
sage: f = function('f')
sage: var('h dt a1 a2 a3')
(h, dt, a1, a2, a3)
sage: dx = a1*h + a2*h^2 + a3*h^3
sage: s = taylor(f(x+dx), h, 0, 3); s
1/6*(a1^3*diff(f(x), x, x, x) + 6*a1*a2*diff(f(x), x, x) + 6*a3*diff(f(x), x))*h^3 + 1/2*(a1^2*diff(f(x), x, x) + 2*a2*diff(f(x), x))*h^2 + a1*h*diff(f(x), x) + f(x)
sage: sdt = s.subs({h: dt^(1/2)}); sdt
1/6*(a1^3*diff(f(x), x, x, x) + 6*a1*a2*diff(f(x), x, x) + 6*a3*diff(f(x), x))*dt^(3/2) + a1*sqrt(dt)*diff(f(x), x) + 1/2*(a1^2*diff(f(x), x, x) + 2*a2*diff(f(x), x))*dt + f(x)
sage: view(sdt)
$\frac{1}{6} {\left(a_{1}^{3} \frac{\partial^{3}}{(\partial x)^{3}}f\left(x\right) + 6 a_{1} a_{2} \frac{\partial^{2}}{(\partial x)^{2}}f\left(x\right) + 6 a_{3} \frac{\partial}{\partial x}f\left(x\right)\right)} \mathit{dt}^{\frac{3}{2}} + a_{1} \sqrt{\mathit{dt}} \frac{\partial}{\partial x}f\left(x\right) + \frac{1}{2} {\left(a_{1}^{2} \frac{\partial^{2}}{(\partial x)^{2}}f\left(x\right) + 2 a_{2} \frac{\partial}{\partial x}f\left(x\right)\right)} \mathit{dt} + f\left(x\right)$
Wed, 31 Oct 2018 04:59:52 -0500http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?answer=44129#post-id-44129Comment by Hans for <p>You can do</p>
<pre><code>sage: f = function('f')
sage: var('h dt a1 a2 a3')
(h, dt, a1, a2, a3)
sage: dx = a1*h + a2*h^2 + a3*h^3
sage: s = taylor(f(x+dx), h, 0, 3); s
1/6*(a1^3*diff(f(x), x, x, x) + 6*a1*a2*diff(f(x), x, x) + 6*a3*diff(f(x), x))*h^3 + 1/2*(a1^2*diff(f(x), x, x) + 2*a2*diff(f(x), x))*h^2 + a1*h*diff(f(x), x) + f(x)
sage: sdt = s.subs({h: dt^(1/2)}); sdt
1/6*(a1^3*diff(f(x), x, x, x) + 6*a1*a2*diff(f(x), x, x) + 6*a3*diff(f(x), x))*dt^(3/2) + a1*sqrt(dt)*diff(f(x), x) + 1/2*(a1^2*diff(f(x), x, x) + 2*a2*diff(f(x), x))*dt + f(x)
sage: view(sdt)
</code></pre>
<p>$\frac{1}{6} {\left(a_{1}^{3} \frac{\partial^{3}}{(\partial x)^{3}}f\left(x\right) + 6 a_{1} a_{2} \frac{\partial^{2}}{(\partial x)^{2}}f\left(x\right) + 6 a_{3} \frac{\partial}{\partial x}f\left(x\right)\right)} \mathit{dt}^{\frac{3}{2}} + a_{1} \sqrt{\mathit{dt}} \frac{\partial}{\partial x}f\left(x\right) + \frac{1}{2} {\left(a_{1}^{2} \frac{\partial^{2}}{(\partial x)^{2}}f\left(x\right) + 2 a_{2} \frac{\partial}{\partial x}f\left(x\right)\right)} \mathit{dt} + f\left(x\right)$</p>
http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?comment=44133#post-id-44133Excellent! Is there a way to expand using $\delta t$ directly without going through $h$? Also I would like to generalize this to the second half of my newly edited question. Could you please review my question? Thank you.Wed, 31 Oct 2018 15:46:08 -0500http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?comment=44133#post-id-44133Answer by Emmanuel Charpentier for <p>I would like to expand the symbol function $f$ as a Taylor series
$$\delta f(x)=\delta x\frac{d}{dx}f+\frac12(\delta x)^2\frac{d^2}{dx^2}f+O((\delta x)^3)$$
with
$$\delta x = a_1(\delta t)^{\frac12}+a_2(\delta t)+a_3(\delta t)^{\frac32}+O((\delta t)^2)$$
and expand and collect the same power terms of $\delta t$ up to a designated power, say, $\frac32$. $f$ is just a symbol, I just need Mathsage to produce the symbols of derivatives $\frac{d}{dx}$.</p>
<p>How should one set this up?</p>
<hr>
<p>Inspired by eric_g's answer to this question, I am able to perform the same operation to $f(x,y)$ with
$$\delta y = a_1(\delta s)^{\frac12}+a_2(\delta s)+a_3(\delta s)^{\frac32}+O((\delta s)^2)$$.
I would like to collect terms according to the powers $dt^{\frac i2}ds^{\frac j2}$ where $i$ and $j$ are nonnegative integers and arranged in the ascending order of $i+j$. I would also like to have the capacity to collect terms according to the order of some other variables, say, $a_1$. I have tried using </p>
<p>g.full_simplify().maxima_methods().collectterms(dt)</p>
<p>for a single variable $dt$. But it does not seem to work well for the fractional power. I do not know if it will work for $dt^{\frac i2}ds^{\frac j2}$.</p>
<p>How can I set this up?</p>
http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?answer=44127#post-id-44127Does this :
sage: f=function("f")
sage: latex(diff(f(x),x))
\frac{\partial}{\partial x}f\left(x\right)
answers your question ?Wed, 31 Oct 2018 03:42:42 -0500http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?answer=44127#post-id-44127Comment by Hans for <p>Does this :</p>
<pre><code>sage: f=function("f")
sage: latex(diff(f(x),x))
\frac{\partial}{\partial x}f\left(x\right)
</code></pre>
<p>answers your question ?</p>
http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?comment=44132#post-id-44132Well, that would be part of the solution. But I want a multiple term expansion of the Taylor series then substituting in the expansion of $\delta x$. The latex command is nice, though. I can use it at the last command to present the result better.Wed, 31 Oct 2018 15:42:43 -0500http://ask.sagemath.org/question/44125/symbolic-taylor-expansion/?comment=44132#post-id-44132