Programma's bij Numerieke nulpunten
funCtie_grafiek.py
Download functie_grafiek.py
# Functie f(x) = x^3 - 3x + 1
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math
matplotlib.use('TkAgg')
PN = 601
X0 = -2
X1 = 2
x = np.linspace(X0, X1, PN)
Titel = 'f(x) = x^3 - 3x + 1'
f1 = x*x*x - 3*x + 1
Xas = 0
Yas = 0
fig = plt.figure(figsize=(6, 6), dpi=150)
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position(('data',Yas))
ax.spines['bottom'].set_position(('data',Xas))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set_title(Titel)
# plt.plot(x,f1, 'r',label='f(x) = x^3 - 3x + 1')
plt.plot(x,f1, 'r',linewidth=1)
plt.axis('equal')
# plt.legend(loc='upper left')
plt.show()
# plt.savefig('functie.png')
biseCtie.py
Download bisectie.py
# BISECTIE METHODE
import numpy as np
# Functie f(x) = x^3 - 3x + 1:
def f(x):
y = x*x*x - 3*x + 1
return y
# Startpunten (x0,f(x0)) en (x1,f(x1)):
x0 = 0
x1 = 1.5
y0 = f(x0)
y1 = f(x1)
print('startpunten: (',x0,',',y0,'),(',x1,',',y1,')')
print('iteratie, c, f(c):')
for i in range(60):
# De volgende benadering x2 ligt midden tussen x0 en x1:
x2 = (x0+x1)/2
y2 = f(x2)
print(i+1,x2,y2)
# Als f(x2) = 0, dan is x2 het nulpunt:
if y2 == 0:
break
# Anders wordt x0 of x1 aangepast:
# if y0 * y2 > 0:
if np.sign(y0) == np.sign(y2):
x0 = x2
y0 = y2
else:
x1 = x2
# y1 = y2
biseCtie_met_grafiek.py
Download bisectie_met_grafiek.py
# BISECTIE METHODE
# Functie f(x) = x^3 - 3x + 1
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math
matplotlib.use('TkAgg')
PN = 601
X0 = -2
X1 = 2
x = np.linspace(X0, X1, PN)
Titel = 'f(x) = x^3 - 3x + 1'
f1 = x*x*x - 3*x + 1
Xas = 0
Yas = 0
fig = plt.figure(figsize=(6, 6), dpi=150)
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position(('data',Yas))
ax.spines['bottom'].set_position(('data',Xas))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set_title(Titel)
# plt.plot(x,f1, 'r',label='f(x) = x^3 - 3x + 1')
plt.plot(x,f1, 'r',linewidth=1)
plt.axis('equal')
# plt.legend(loc='upper left')
def f(x):
y = x*x*x - 3*x + 1
return y
# Startpunten (x0,f(x0)) en (x1,f(x1)):
x0 = 0
x1 = 1.5
y0 = f(x0)
y1 = f(x1)
plt.plot(x0,0,marker='o',markersize=3,color='g')
plt.plot(x0,y0,marker='o',markersize=3,color='r')
plt.plot(x1,0,marker='o',markersize=3,color='g')
plt.plot(x1,y1,marker='o',markersize=3,color='r')
for i in range(6):
# De volgende benadering x2 ligt midden tussen x0 en x1:
x2 = (x0+x1)/2
y2 = f(x2)
plt.plot(x2,y2,marker='o',markersize=3,color='r')
plt.plot(x2,0,marker='o',markersize=3,color='g')
# Als f(x2) = 0, dan is x2 het nulpunt:
if y2 == 0:
break
# Anders wordt x0 of x1 aangepast:
# if y0 * y2 > 0:
if np.sign(y0) == np.sign(y2):
x0 = x2
y0 = y2
else:
x1 = x2
# y1 = y2
plt.show()
# plt.savefig('bisectie.png')
regfals.py
Download regfals.py
# REGULA FALSI METHODE
import numpy as np
# Functie f(x) = x^3 - 3x + 1:
def f(x):
y = x*x*x - 3*x + 1
return y
# Startpunten (x0,f(x0)) en (x1,f(x1)):
x0 = 0
x1 = 1.5
y0 = f(x0)
y1 = f(x1)
print('startpunten: (',x0,',',y0,'),(',x1,',',y1,')')
print('iteratie, c, f(c):')
for i in range(60):
# De volgende benadering x2:
x2 = (x0*y1 - x1*y0)/(y1 - y0)
y2 = f(x2)
print(i+1,x2,y2)
# Als f(x2) = 0, dan is x2 het nulpunt:
if y2 == 0:
break
# Anders wordt x0 of x1 aangepast:
# if y0 * y2 > 0:
if np.sign(y0) == np.sign(y2):
x0 = x2
y0 = y2
else:
x1 = x2
y1 = y2
regfals_met_grafiek.py
Download regfals_met_grafiek.py
# REGULA FALSI METHODE
# Functie f(x) = x^3 - 3x + 1
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math
matplotlib.use('TkAgg')
PN = 601
X0 = -2
X1 = 2
x = np.linspace(X0, X1, PN)
Titel = 'f(x) = x^3 - 3x + 1'
f1 = x*x*x - 3*x + 1
Xas = 0
Yas = 0
fig = plt.figure(figsize=(6, 6), dpi=150)
ax = fig.add_subplot(1, 1, 1)
ax.spines['left'].set_position(('data',Yas))
ax.spines['bottom'].set_position(('data',Xas))
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set_title(Titel)
# plt.plot(x,f1, 'r',label='f(x) = x^3 - 3x + 1')
plt.plot(x,f1, 'r',linewidth=1)
plt.axis('equal')
# plt.legend(loc='upper left')
def f(x):
y = x*x*x - 3*x + 1
return y
# Startpunten (x0,f(x0)) en (x1,f(x1)):
x0 = 0
x1 = 1.5
y0 = f(x0)
y1 = f(x1)
plt.plot(x0,0,marker='o',markersize=3,color='g')
plt.plot(x0,y0,marker='o',markersize=3,color='r')
plt.plot(x1,0,marker='o',markersize=3,color='g')
plt.plot(x1,y1,marker='o',markersize=3,color='r')
for i in range(4):
# De volgende benadering x2:
x2 = (x0*y1 - x1*y0)/(y1 - y0)
y2 = f(x2)
plt.plot(x2,y2,marker='o',markersize=3,color='r')
plt.plot(x2,0,marker='o',markersize=3,color='g')
# Als f(x2) = 0, dan is x2 het nulpunt:
if y2 == 0:
break
# Anders wordt x0 of x1 aangepast:
# if y0 * y2 > 0:
if np.sign(y0) == np.sign(y2):
x0 = x2
y0 = y2
else:
x1 = x2
y1 = y2
plt.show()
# plt.savefig('regfals.png')