%matplotlib inline import matplotlib.pyplot as plt import numpy as np from scipy.optimize import fsolve plt.rc('font', size=16) np.set_printoptions(precision=3) f = lambda x: x**3 + x**2 - 10 * x + 8 fig, ax = plt.subplots() ax.grid() ax.set_xlim(-5,5) ax.set_ylim(-30,30) xs = np.linspace(-5,5,100) ax.plot(xs,f(xs)) # vandret linie i y=0 fra mindste til største x-værdi ax.plot([xs[0],xs[-1]],[0,0]) # loop over forskellige start-gæt på løsning for x0 in [-3,0,3]: x1 = fsolve(f,x0) ax.plot(x1,f(x1),'o')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np from scipy.optimize import fsolve plt.rc('font', size=16) np.set_printoptions(precision=3) # kald venstre side f og højre side g f = lambda x: 4 * np.sqrt(x) g = lambda x: 4 * x**2 - 25 + 2 * np.sqrt(10) fig, ax = plt.subplots() ax.grid() ax.set_xlim(0,5) ax.set_ylim(-30,30) xs = np.linspace(0,5,100) ax.plot(xs,f(xs)) ax.plot(xs,g(xs)) # lav ny (anonym) funktion, der er forskellen mellem f og g # og søg efter dens nulpunkt x1 = fsolve(lambda x: f(x)-g(x),3) # plot både venstre og højre side af ligningen ax.plot(x1,f(x1),'s',markerfacecolor='None',markersize=20) ax.plot(x1,g(x1),'o',markerfacecolor='None',markersize=15) fig.tight_layout() fig.savefig('loesning_ikke_lineaer_ligning.png')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np import scipy.optimize plt.rc('font', size=16) np.set_printoptions(precision=3) f = lambda x: x**3 - 6 * x**2 - x + 30 fig, ax = plt.subplots() ax.grid() ax.set_xlim(-5,5) ax.set_ylim(-20,40) xs = np.linspace(-5,5,100) ax.plot(xs,f(xs)) ax.plot([xs[0],xs[-1]],[0,0]) # minimum, start i x = 3 x1 = scipy.optimize.fmin(f,3) ax.plot(x1,f(x1),'o') # maximum, start i x = 1 x1 = scipy.optimize.fmin(lambda x: -f(x),1) ax.plot(x1,f(x1),'o') fig.tight_layout() fig.savefig('opgave_min_max_let.png')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np import scipy.optimize plt.rc('font', size=16) np.set_printoptions(precision=3) f1 = lambda x: np.sin(x/2) f2 = lambda x: np.exp(-x/3) * np.cos(x**2) fig, ax = plt.subplots() ax.grid() ax.set_xlim(-2,2) ax.set_ylim(-2,2) xs = np.linspace(-2,2,100) ax.plot(xs,f1(xs)) ax.plot(xs,f2(xs)) # funktion g er den absolutte forskel mellem f1 og f2 g = lambda x: np.abs(f1(x) - f2(x)) ax.plot(xs,g(xs)) ax.legend(['$f_1$','$f_2$',r'$\mathrm{abs}(f_1-f_2)$']) # bestem max-værdi for g når søgning starter i hhv -.5 og 1 x1 = scipy.optimize.fmin(lambda x: -g(x),-.5) x2 = scipy.optimize.fmin(lambda x: -g(x),1) # kig på de fundne løsninger print('Ved x=',x1,'er forskellen:',g(x1)) print('Ved x=',x2,'er forskellen:',g(x2)) # tag den x-værdi hvor forskellen er størst x0 = x1 if g(x1) > g(x2) else x2 ax.plot([x0,x0],[f1(x0),f2(x0)]) print('Længden:',np.abs(f1(x0)-f2(x0))) fig.tight_layout() fig.savefig('opgave_f1_f2_forskel.png')
import numpy as np from scipy.integrate import quad f = lambda theta: 2*np.sqrt(2)*np.cos(theta) - 5/np.pi integrale, usikkerhed = quad(f,0,np.pi/4) 56 * integrale
import numpy as np from scipy.integrate import quad f = lambda x: 2 * np.power(x,1/3) res = quad(f,0,12.1722)[0] print('Arealet er:',res)
%matplotlib inline import matplotlib.pyplot as plt import numpy as np from scipy.integrate import quad plt.rc('font', size=16) fig, ax = plt.subplots() x0 = 12.1722 res = quad(f,0,x0)[0] x1 = np.linspace(0,x0,100) y1 = f(x1) # hvis man kender denne kommando: #ax.fill_between(x1,0,y1,color='red') # ellers tilføj punktet (x0,0): x2 = [x0] # sidste x-værdi en gang til y2 = [0] # en tilhørende y-værdi på y-aksen xs = np.concatenate((x1,x2)) ys = np.concatenate((y1,y2)) ax.fill(xs,ys,'red') xs = np.linspace(0,15,100) ys = f(xs) ax.plot(xs,ys,linewidth=3,color='black') ax.grid() fig.savefig('third_root3.png')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np from scipy.integrate import quad plt.rc('font', size=16) fig, ax = plt.subplots() # de afledte er udregnet i hånden og indskrives her: dx_dtheta = lambda theta: np.cos(theta)/5 - theta/5 * np.sin(theta) dy_dtheta = lambda theta: np.sin(theta)/5 + theta/5 * np.cos(theta) integranden = lambda theta: np.sqrt(dx_dtheta(theta)**2 + dy_dtheta(theta)**2) # skal ikke bruge usikkerheden til noget, så "gemmer" den i variablen _: theta_slut = 20.391 laengde, _ = quad(integranden,0,theta_slut) print('Længden er:',laengde) # hvis man vil lave figuren: thetas = np.linspace(0,theta_slut,100) xs = thetas/5 * np.cos(thetas) ys = thetas/5 * np.sin(thetas) ax.plot(xs,ys,color='red') ax.set_aspect('equal') ax.grid() fig.savefig('spiral_theta_over_5.png')
%matplotlib inline import matplotlib.pyplot as plt plt.rc('font', size=16) from scipy.integrate import quad from scipy.optimize import fsolve import numpy as np # funktionen afhænger generelt af både sigma og x: f = lambda sigma,x: 1/sigma/np.sqrt(2*np.pi) * np.exp(-x**2/2/sigma**2) # hvis man bestemmer sig for et sigma, afhænger den nu kun af x: def f_for_bestemt_sigma(sigma): return lambda x: f(sigma,x) # a f1 = f_for_bestemt_sigma(1) f2 = f_for_bestemt_sigma(2) f3 = f_for_bestemt_sigma(3) l1,_ = quad(f1,-1,1) l2,_ = quad(f2,-2,2) l3,_ = quad(f3,-3,3) print('De tre værdier:',l1,l2,l3) fig, ax = plt.subplots() xs = np.linspace(-1,1,100) ax.fill_between(xs,0,f1(xs)) xs = np.linspace(-2,2,100) ax.fill_between(xs,0,f2(xs),zorder=0.5) # zorder gør at plottes lidt bagud xs = np.linspace(-3,3,100) ax.fill_between(xs,0,f3(xs),zorder=0.25) xs = np.linspace(-5,5,100) ax.plot(xs,f1(xs)) ax.plot(xs,f2(xs)) ax.plot(xs,f3(xs)) ax.legend([r'$f_{\sigma=1}$',r'$f_{\sigma=2}$',r'$f_{\sigma=3}$']) fig.savefig('tre_ens_arealer.png')
De tre værdier: 0.682689492137086 0.682689492137086 0.682689492137086
# b l1,_ = quad(f1,-2,2) l2,_ = quad(f2,-4,4) l3,_ = quad(f3,-6,6) print('De tre værdier:',l1,l2,l3)
De tre værdier: 0.9544997361036417 0.9544997361036417 0.9544997361036417
# c g1 = lambda q: quad(f1,-q,q)[0] - 0.999 g2 = lambda q: quad(f2,-2*q,2*q)[0] - 0.999 g3 = lambda q: quad(f3,-3*q,3*q)[0] - 0.999 from scipy.optimize import fsolve q1 = fsolve(g1,1) q2 = fsolve(g2,1) q3 = fsolve(g3,1) print('De tre løsninger:',q1,q2,q3)
De tre løsninger: [3.29052673] [3.29052673] [3.29052673]
import numpy as np from scipy.integrate import quad from scipy.optimize import fsolve # a f = lambda x: 8 * np.exp(-(x - 20)**2/400) x1 = fsolve(lambda x1: quad(f,0,x1)[0] - 244.3017,40) x1[0]
from scipy.optimize import fmin # b delta_x = 5.28052 x0 = fmin(lambda x0: -quad(f,x0,x0 + delta_x)[0],5) areal,_ = quad(f,x0,x0 + delta_x) areal
Choose which booklet to go to: