63 lines
1.2 KiB
Python
63 lines
1.2 KiB
Python
from scipy.integrate import odeint
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import pylab as pl
|
|
import math
|
|
|
|
#plot f(x) vs. x
|
|
x = np.arange(-4.0,4.0,0.01)
|
|
fx = 4*x**2 - 16
|
|
zerox = 0.*x
|
|
|
|
#Create plot env
|
|
f = plt.figure()
|
|
plt.grid(True)
|
|
f.set_figwidth(8)
|
|
f.set_figheight(8)
|
|
|
|
#plot some stuff
|
|
plt.plot(x,fx,'g-',x,zerox,'b-')
|
|
|
|
plt.xlabel('x',fontsize = 15)
|
|
plt.ylabel('y', fontsize = 15)
|
|
plt.savefig("2024-09-09/ex1.png")
|
|
|
|
#Integrating stuff!
|
|
#Define the integration:
|
|
def dx_dt(xx,t):
|
|
return[4.*xx[0]**2 -16.]
|
|
|
|
f1 = plt.figure()
|
|
f.set_figwidth(12)
|
|
f.set_figheight(12)
|
|
|
|
# forward integration in time, so we need a time span
|
|
ts = np.linspace(0,3,1000)
|
|
ic = 1
|
|
xs = odeint(dx_dt, ic, ts)
|
|
#print(xs)
|
|
|
|
plt.plot(ts,xs)
|
|
plt.savefig("2024-09-09/ex2.png")
|
|
#plt.show()
|
|
|
|
#Let's do the same thing in the time domain!
|
|
ics = np.linspace(-4,2.0001,20)
|
|
ts = np.linspace(0,0.5,1000)
|
|
|
|
# loop over the initial conditions
|
|
for r in ics:
|
|
x0 = [r]
|
|
xs = odeint(dx_dt,x0,ts)
|
|
plt.plot(ts,xs[:,0],"b")
|
|
|
|
x1 = ts - ts - 2
|
|
x2 = ts - ts + 2
|
|
f2 = plt.figure()
|
|
plt.plot(ts,x1,'r',ts,x2,'r--')
|
|
plt.grid(True)
|
|
plt.xlabel('Time, some units.')
|
|
plt.ylabel('Voltage shrug?')
|
|
plt.savefig('2024-09-09/ex3')
|
|
|