import numpy as np import matplotlib.pyplot as plt %matplotlib inline plt.rc('font', size=16) def part_over_y_0(a,c): # assume a < 0 and c > 0 x0 = np.sqrt(-c/a) N = 100 xs = np.linspace(-x0,x0,N) ys = a*xs**2 + c return xs,ys a1 = -3 c1 = 5 a2 = -0.5 c2 = 2 # spmg a x,y = part_over_y_0(a1,c1) plt.plot(x,y) x,y = part_over_y_0(a2,c2) plt.plot(x,y) plt.grid() plt.axis('equal') plt.xlabel('$x$') plt.ylabel('$y$')
from scipy.optimize import fsolve y1 = lambda x: a1*x**2 + c1 y2 = lambda x: a2*x**2 + c2 y3 = lambda x: y1(x) - y2(x) x0 = fsolve(y3,1) xs = np.array([-x0,x0]) plt.scatter(xs,[y1(xs)],c='r',s=100) fig
import numpy as np N = 100 xs = np.linspace(-np.pi/2,np.pi/2,N) ys = np.cos(xs) L = np.sum(np.sqrt(np.diff(xs)**2+np.diff(ys)**2)) print('L',L) from scipy.integrate import quad f = lambda x: np.sqrt(1 + np.sin(x)**2) L2 = quad(f, -np.pi/2,np.pi/2)[0] print('L2',L2)
L 3.820147518333091 L2 3.8201977890277115
from scipy.integrate import quad from scipy.optimize import fsolve La = lambda a: quad(lambda x: np.sqrt(1 + (a*np.sin(x))**2), -np.pi/2,np.pi/2)[0] a = fsolve(lambda a: La(a)-5,2) print('A',a)
A [1.83146772]
import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors colors.colorConverter.colors['b'] = [0, 0.4470, 0.7410] colors.colorConverter.colors['g'] = [0.4660, 0.6740, 0.1880] plt.rc('font', size=16) fig,ax = plt.subplots(1,1) x0 = np.sqrt(3/4) f1 = lambda x: np.sqrt(1-x**2) for xinit,xfinal,sign,linestyle in [(-x0,x0,1,'-'), (-1,1,1,'--'), (-1,1,-1,'--')]: xs = np.linspace(xinit,xfinal) ax.plot(xs,sign*f1(xs),linestyle,color='g') f2 = lambda x,a: a*x**2 + 0.5 -0.75*a for xinit,xfinal,linestyle in [(-x0,x0,'-'), (-1.4,-x0,'--'), (x0,1.4,'--')]: xs = np.linspace(xinit,xfinal) ax.plot(xs,f2(xs,1),linestyle=linestyle,color='b') ax.grid() ax.axis('equal') ax.set_xlabel('$x$') ax.set_ylabel('$y$') ax.set_yticks([-1,0,1]) ax.text(0.3,1.1,'$f_1$',color='g') ax.text(0.3,0.2,'$f_2$',color='b')
from scipy.integrate import solve_ivp import matplotlib.pyplot as plt plt.rc('font', size=16) fig,ax = plt.subplots(1,1) dydx = lambda x, y: -0.5 * (x - 2) * y def plot_sol(xrange,yinit): mysol = solve_ivp(dydx, xrange, yinit) xs = mysol.t ys = mysol.y[0] ax.plot(xs,ys,'o',linestyle='--') xinit = 2 yinit = [1] for xfinal in [6,-2]: xrange = [xinit,xfinal] plot_sol(xrange,yinit) ax.grid() ax.set_ylim([0,1.2]) ax.set_xlabel('$x$') ax.set_ylabel('$y$')
Choose which booklet to go to: