from scipy.optimize import * import numpy as np import matplotlib.pyplot as plt a=1;b=0.2# def fun_1(x,a): # return np.e**(-a*x**2) def fun_2(x,b): # return b*x**2 x0=round(fsolve(lambda x:fun_1(x,a)-fun_2(x,b),1)[0],3)# x=np.arange(0,x0+1,0.01) plt.figure() plt.title(' ', size=12) plt.xlabel(' ', size=12) plt.plot(x, fun_1(x,a), 'r', linestyle ='--',linewidth = 1, label=' ') plt.plot(x, fun_2(x,b), 'b', linestyle ='-' ,linewidth = 1, label=' ') plt.plot(x0, fun_2(x0,b), marker = 'o', markersize = 6, markerfacecolor = 'g', label=' ' ) plt.plot(x0, 0, marker = 's', markersize = 6, markerfacecolor = 'r', label=' ' ) plt.legend(loc='best') plt.grid(True) q=[];p=[];x=[] q.append(0.6) p.append(round(fsolve(lambda x:fun_1(x,a)-q[0],1)[0],3)) for i in np.arange(0,7): x.append(i) if i!=0: q.append(fun_2(p[i-1],b)) p.append(round(fsolve(lambda x:fun_1(x,a)-q[i],1)[0],3)) plt.figure() plt.title(' ', size=12) plt.xlabel(' ', size=12) plt.plot(x, p, 'r', marker = 's', markersize = 6, markerfacecolor = 'r', label=' ') plt.plot(x, q, 'b', marker = 'o', markersize = 6, markerfacecolor = 'b', label=' ') plt.legend(loc='best') plt.grid(True) plt.show()
from scipy.optimize import * from scipy.misc import derivative import numpy as np import matplotlib.pyplot as plt c=2.2;a=-0.9;h=0.1 def f(x): return 3*x-2*c*(a+x)**3+2*a**3-1.5 def s(x): return c*(a+x)**3-a**3+2 def l(x): return 3*xc*(a+x)**3+a**3+0.5 rx=[];ry=[];dy=[];dz=[] for i in np.arange(0,3): x=round(fsolve(lambda x:f(x),i)[0],3) z=derivative(f, x, dx=1e-6) rx.append(x) ry.append(s(x)) dz.append(z) x=[i*h for i in np.arange(0,18)] z=[s(i*h) for i in np.arange(0,18)] v=[l(i*h) for i in np.arange(0,18)] plt.figure() plt.title(' ', size=12) plt.xlabel('', size=12) plt.plot(x, z, 'r', linestyle ='--',linewidth = 2, label=' -s(x)') plt.plot(x, v, 'b', linestyle ='-' ,linewidth = 2, label=' -l(x)') plt.plot(rx[0], ry[0], marker = 's', markersize = 6, label=' y1 - df/dx<0,df/dx= '+ str(round(dz[0],3))) plt.plot(rx[1], ry[1], marker = 'o', markersize = 6, label=' y2 - df/dx>0,df/dx='+ str(round(dz[1],3))) plt.plot(rx[2], ry[2], marker = 's', markersize = 6, label=' y3 - df/dx<0,df/dx= '+ str(round(dz[2],3))) plt.legend(loc='best') plt.grid(True) T=4;N=50; h=T/N y=[];y.append(1.2) z=[];z.append(0.987) u=[];u.append(0.5) x=[ i*h for i in np.arange(0,N+2)] for i in np.arange(1,N+2): y.append(y[i-1]+h*f(y[i-1])) for i in np.arange(1,N+2): z.append(z[i-1]+h*f(z[i-1])) for i in np.arange(1,N+2): u.append(u[i-1]+h*f(u[i-1])) y1= [rx[0] for i in np.arange(0,N+2)] y3= [rx[2] for i in np.arange(0,N+2)] y2= [rx[1] for i in np.arange(0,N+2)] plt.figure() plt.title(' \n ', size=12) plt.plot(x, y,linewidth = 2, label=' f(x0)=1.2') plt.plot(x, z,linewidth = 2, label='f(x0)=0.987') plt.plot(x, u,linewidth = 2, label=' f(x0)=0.5') plt.plot(x, y1,linewidth = 1, label=' y1') plt.plot(x, y3,linewidth = 1, label=' y3') plt.plot(x, y2,linewidth = 1, label='y2') plt.legend(loc='best') plt.grid(True) plt.show()
Source: https://habr.com/ru/post/336946/
All Articles