0
import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
from ipywidgets.widgets import interact
sym.init_printing(use_latex="mathjax")
x, y, z, t = sym.symbols('x y z t')

We were given a function in class to write as code

\begin{equation}
p_w(z,t)=\frac{1}{\sqrt{\pi \left(1-\exp\left[-2 t\right]\right)}}
\exp\left[-\frac{\left(z-\exp\left[-t\right]\right)^{2}}{1-
\exp\left[-2t\right]}\right]
\end{equation}

which I have written as this

p_w = (1/(sym.sqrt((sym.pi)*(1-(sym.exp(-2*t))))))*(sym.exp((-(z-sym.exp(-t))**2)/(1-sym.exp(-2*t))))

Then find the partial differential equation

∂𝑡𝑝𝑤(𝑧,𝑡)=∂𝑧[𝑧𝑝𝑤(𝑧,𝑡)]+1/2 ∂2𝑧𝑝𝑤(𝑧,𝑡)

which I have written as this:

LHS=sym.diff(p_w,t,1)
#differentiate once with respect to t
RHS=sym.diff(z*p_w,z,1)+((1/2)*(sym.diff(p_w,z,2)))
#now differentiate with respect to z

Now we need to plot it and can only use matplotlib/numpy/sympy libraries.

Plot 𝑝𝑤(𝑧,𝑡) for the three values t=0.1,1,10 in a 𝑝𝑤(𝑧,𝑡) versus z diagram.

Here's what I've got so far:

t_points=[0.1,1,10]
#pw = sym.lambdify(t,p_w)
mytspace=np.linspace(0,10,200)
#myzspace=pw(mytspace)
plt.xlabel("t axis")
plt.ylabel("z axis")
plt.plot(t_array,np.zeros(3),'bs')

I haven't studied multivariable calculus before so I'm a bit lost!

1
  • Since t has been given (as 0.1, 1, and 10) you're only actually plotting with one variable, and your graphs will just be 2D. So just plot as normal but loop over your three values of t. Commented Nov 19, 2019 at 0:43

1 Answer 1

1

Since one of your variables is given (you know t must be t=0.1, t=1 or t=10) your only variable for plotting is z. I know you are using sympy for the calculations, but for plotting maybe it's simpler to just return p_w as a numpy array. You can define a function to return p_w as so:

import numpy as np
import matplotlib.pyplot as plt

def p_w(z, t):
    p_w = (1/(np.sqrt((np.pi)*(1-(np.exp(-2*t))))))*(np.exp((-(z-np.exp(-t))**2)/(1-np.exp(-2*t))))
    return p_w

This will give you a numpy array with the results of p_w(z, t) where z is an array and t is just one number. Then you can just iterate over the values of t that you need:

t_points=[0.1, 1, 10]
z = np.linspace(0,10,200)

for t in t_points:
    plt.plot(z, p_w(z, t), label='t = {0}'.format(t))   

plt.legend()
plt.show()

Result:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.