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.Thu, 01 Nov 2018 07:57:04 +0100Substituting a differential equation into an expressionhttps://ask.sagemath.org/question/44136/substituting-a-differential-equation-into-an-expression/ Say I have a (heat) partial differential equation of $f(t,x)$
$$\frac{\partial f}{\partial t}=\frac12\frac{\partial^2 f}{\partial x^2}.$$
I would like to express an algebraic function $g$ of the variable $\Big(\frac{\partial f}{\partial t}, \frac{\partial^2 f}{\partial t\partial x}\Big)$ all in the partial derivatives of $x$. That is
$$\frac{\partial^2 f}{\partial t\partial x}=\frac12\frac{\partial^3 f}{\partial x^3}.$$
The result should be
$$g\Big(\frac{\partial f}{\partial t}, \frac{\partial^2 f}{\partial t\partial x}\Big)=g\Big(\frac12\frac{\partial^2 f}{\partial x^2}, \frac12\frac{\partial^3 f}{\partial x^3}\Big).$$
How can I accomplish this with SageMath?
HansThu, 01 Nov 2018 07:57:04 +0100https://ask.sagemath.org/question/44136/Numerical calculation speed: Sagemath vs. C vs. Matlabhttps://ask.sagemath.org/question/43893/numerical-calculation-speed-sagemath-vs-c-vs-matlab/I mostly use Sagemath for performing symbolic operations on trigonometric polynomials. But I was wondering if it is also a good tool in numerical methods. This includes matrix operations and iterative solution of PDEs with finite difference methods. Usually I take the Sagemath outputs in another program more suited to numerical work. But I was wondering if Sagemath itself is a viable choice in this resepct. Can you tell me about its numerical performance and speed compared to other programs such as C, Matlab, Julia, Mathematica etc.? ThanksDanialBaghWed, 10 Oct 2018 09:22:46 +0200https://ask.sagemath.org/question/43893/Lazy evaluation of derivatives of an unknown functionhttps://ask.sagemath.org/question/10215/lazy-evaluation-of-derivatives-of-an-unknown-function/Hi,
I am using Sage to check some solutions to partial differential equations. I am wondering if a have an unknown function f, can I somehow form the PDE in terms of its derivatives and then substitute in the assumed solution and evaluate the derivatives after the fact?
Here is what I tried so far:
var('x y')
f = function('f', x, y)
g = derivative(f, x, y)
print(g)
D[0, 1](f)(x, y)
h = D[0, 1](f)(x, y)
print(h)
Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.symbolic.expression.Expression' object has no
attribute '__getitem__'
I figured out that D[0, 1] represents the derivatives with respect to the ith indepent variable of the function (is this a Maxima expression?), but I'm not sure then how to use these types of expressions when I finally want to substitute in the known form of f. I.e., since the output of the expression for g is in terms of D[], and when I try to reuse that expression as h, I get an error (since D is actually some other type of object). Any help would be appreciated. Let me know if my question is not clear.
Many thanks!nosnerosMon, 10 Jun 2013 12:27:40 +0200https://ask.sagemath.org/question/10215/Plot 3D Surface Heat Equationhttps://ask.sagemath.org/question/35323/plot-3d-surface-heat-equation/I am trying to plot a 3D surface using SageMath Cloud but I am having some trouble with my plot result. Anyways the program I have written is to plot the Heat Equation solution that I got from analytical method.
The case is: ![Heat Equation](https://s17.postimg.org/q4c2k8ii7/attachment.jpg)
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from sympy import *
from math import *
L = 10
x = np.linspace(0, 10, 5)
t = np.linspace(0, 20, 5)
n = symbols('n', integer=True)
X, T = np.meshgrid(x, t)
Z = []
y = symbols('y')
for ix, ea in enumerate(x):
ans = 0.
for n in range(L + 1): # do summation with simple for-loop
f_pi = 0.
if x[ix] >= 6 and x[ix] <= 8:
f_pi = 50
ans = ans + ((2 / 10.) * integrate(f_pi * sin(radians((2. * n + 1.) * pi * x[ix] / 20.)), (y, 0, 10)) * e**(-1 * (2. * n + 1. / 20.)**2*pi**2*t[ix]) * sin(radians((2. * n + 1.) * pi * x[ix] / 20)))
Z.append(ans)
Z = np.array(Z, dtype=float)
fig = plt.figure()
ax = fig.gca(projection = '3d')
plt.xticks(x, x)
plt.yticks(t, t)
surf = ax.plot_surface(X, T, Z,
rstride = 3,
cstride = 3,
cmap = cm.coolwarm,
linewidth = 0.5,
antialiased = True)
fig.colorbar(surf,
shrink=0.8,
aspect=16,
orientation = 'vertical')
ax.view_init(elev=60, azim=60)
ax.dist=8
plt.show()
The plot I got: https://s11.postimg.org/6lk5xrj2r/figure_1.png
It should be: https://s22.postimg.org/4kzl6ff1d/image.png (expected result, using Matlab)
What's wrong with my plotting?
Thanks in advancejustinusTue, 01 Nov 2016 03:02:21 +0100https://ask.sagemath.org/question/35323/solve differential equationhttps://ask.sagemath.org/question/24486/solve-differential-equation/I need to solve this third-order linear partial differential equation:
d^2/dx^2 d/dy f(x,y) = f(x,y) - x*y
Could you please help me to do this?
Thank you very much for your advise!Martin MaxaWed, 15 Oct 2014 00:10:24 +0200https://ask.sagemath.org/question/24486/How do I perform a change of variables for a pdehttps://ask.sagemath.org/question/8435/how-do-i-perform-a-change-of-variables-for-a-pde/How can I transform, step by step, the partial differential equation (pde) u.diff(z,z)*k/gw == mv*u.diff(t) into u.diff(Z,Z)=u.diff(Tv) where Z=z/H, Tv = cv*t/H^2 and cv = k/(gw*mv)? As you can see from my attempt below I can isolate cv; after that I have no idea how to do the change of variables. I can do this example by hand: let Z=z/H --> du/dz = du/dZ * dZ/dz --> du/dz = du/dZ *(1/H) ...etc. But Other examples may not be so easy (and I don't want to do it by hand!).
var('u,z,t,Z,H,Tv,k,gw,mv,cv')
u = function('u',z,t)
eq1 = u.diff(z,z)*k/gw == mv * u.diff(t)
print(eq1)
mycoeff=eq1.left_hand_side().coeff(u.diff(z,z),1)
print(mycoeff)
eq2 = eq1.divide_both_sides(mycoeff)
print(eq2)
mycoeff = eq2.right_hand_side().coeff(u.diff(t),1)
eq3 = cv == 1/mycoeff
print(eq3)
eq4 = solve(eq3,eq3.right_hand_side().default_variable(), solution_dict=true)
print(eq4)
eq5 = eq2.subs(eq4[0])
print(eq5)
which gives me the following output:
k*D[0, 0](u)(z, t)/gw == mv*D[1](u)(z, t)
k/gw
D[0, 0](u)(z, t) == gw*mv*D[1](u)(z, t)/k
cv == k/(gw*mv)
[{gw: k/(cv*mv)}]
D[0, 0](u)(z, t) == D[1](u)(z, t)/cvrtrwalkerThu, 03 Nov 2011 17:02:07 +0100https://ask.sagemath.org/question/8435/