Python code bij Numerieke Nulpunten - deel 2

SeCant_Getallen.py

Download secant_getallen.py

# SECANTMETHODE
# uitvoer in getallen

import numpy as np

# >>> Voer hier de functie f(x) in:
def f(x):
    y = x*x*x - 3*x + 1
    return y
# <<<

# >>> Voer hier de startpunten x0 en x1 in:
x0 = -0.9
x1 = -0.4
# <<<

y0 = f(x0)
y1 = f(x1)
print('startpunten: (x0,y0) = (',x0,',',y0,'), (x1,y1) = (',x1,',',y1,')')

print('iteratie, x2, y2:')
# >>> Voer hier het aantal iteraties in:
for i in range(100):
# <<<
# Als y1 = y0, dan is x2 niet gedefinieerd:
    if y1 == y0:
        print('De berekening breekt af, omdat de secant horizontaal loopt.')
        break
# De volgende benadering x2:
    x2 = (x0*y1 - x1*y0)/(y1 - y0)
    y2 = f(x2)
    print(i+1,x2,y2)
# Als y2 = 0, dan is x2 het nulpunt:
    if y2 == 0:
        break
# Anders worden x0 en x1 aangepast:
    x0 = x1
    y0 = y1
    x1 = x2
    y1 = y2

SeCant_Grafiek.py

Download secant_grafiek.py

# SECANTMETHODE
# uitvoer in grafiek

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math

# Het plotten van de grafiek van de functie f(x):

matplotlib.use('TkAgg')

# >>> Voer hier het domein [X0,X1] van de grafiek in:
X0 = -2
X1 = 2
# <<<

PN = 601
x = np.linspace(X0,X1,PN)

# >>> Voer hier de functie f(x) in:
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,color='r',linewidth=1)
plt.axis('equal')

# >>> Voer hier nogmaals de functie f(x) in:
def f(x):
    y = x*x*x - 3*x + 1
    return y
# <<<

# >>> Voer hier de startpunten x0 en x1 in:
x0 = -0.9
x1 = -0.4
# <<<

y0 = f(x0)
y1 = f(x1)

# Het plotten van de startpunten:
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')

# >>> Voer hier het aantal iteraties in:
for i in range(10):
# <<<
# Als y1 = y0, dan is x2 niet gedefinieerd:
    if y1 == y0:
        print('De berekening breekt af, omdat de secant horizontaal loopt.')
        break
# 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 y2 = 0, dan is x2 het nulpunt:
    if y2 == 0:
        break
# Anders worden x0 en x1 aangepast:
    x0 = x1
    y0 = y1
    x1 = x2
    y1 = y2

# >>> Kies hier de uitvoer-modus:
# Uitvoer op het scherm:
plt.show()
# Uitvoer in een png-bestand:
# plt.savefig('secant.png')
# <<<

Newton_Getallen.py

Download newton_getallen.py

# NEWTON'S METHODE
# uitvoer in getallen

import numpy as np

# >>> Voer hier de functie f(x) en de afgeleide dfdx(x) in:
def f(x):
    y = x*x*x - 3*x + 1
    return y

def dfdx(x):
    z = 3*x*x - 3
    return z
# <<<

# >>> Voer hier het startpunt x0 in:
x0 = -0.6
# <<<

y0 = f(x0)
z0 = dfdx(x0)
print('startpunt: (x0,y0) = (',x0,',',y0,')')

print('iteratie, x1, y1:')
# >>> Voer hier het aantal iteraties in:
for i in range(100):
# <<<
# Als z0 = 0, dan is x1 niet gedefinieerd:
    if z0 == 0:
        print('De berekening breekt af, omdat de afgeleide nul is.')
        break
# De volgende benadering x1:
    x1 = x0 - (y0/z0)
    y1 = f(x1)
    z1 = dfdx(x1)
    print(i+1,x1,y1)
# Als y1 = 0, dan is x1 het nulpunt:
    if y1 == 0:
        break
# Anders wordt x0 aangepast:
    x0 = x1
    y0 = y1
    z0 = z1

Newton_Grafiek.py

Download newton_grafiek.py

# NEWTON'S METHODE
# uitvoer in grafiek

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math

# Het plotten van de grafiek van de functie f(x):

matplotlib.use('TkAgg')

# >>> Voer hier het domein [X0,X1] van de grafiek in:
X0 = -2
X1 = 2
# <<<

PN = 601
x = np.linspace(X0,X1,PN)

# >>> Voer hier de functie f(x) in:
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,color='r',linewidth=1)
plt.axis('equal')

# >>> Voer hier de functie f(x) en de afgeleide dfdx(x) in:
def f(x):
    y = x*x*x - 3*x + 1
    return y

def dfdx(x):
    z = 3*x*x - 3
    return z
# <<<

# >>> Voer hier het startpunt x0 in:
x0 = -0.6
# <<<

y0 = f(x0)
z0 = dfdx(x0)

# Het plotten van het startpunt:
plt.plot(x0,0,marker='o',markersize=3,color='g')
plt.plot(x0,y0,marker='o',markersize=3,color='r')

# >>> Voer hier het aantal iteraties in:
for i in range(10):
# <<<
# Als z0 = 0, dan is x1 niet gedefinieerd:
    if z0 == 0:
        print('De berekening breekt af, omdat de afgeleide nul is.')
        break
# De volgende benadering x1:
    x1 = x0 - (y0/z0)
    y1 = f(x1)
    z1 = dfdx(x1)
    plt.plot(x1,y1,marker='o',markersize=3,color='r')
    plt.plot(x1,0,marker='o',markersize=3,color='g')
# Als y1 = 0, dan is x1 het nulpunt:
    if y1 == 0:
        break
# Anders wordt x0 aangepast:
    x0 = x1
    y0 = y1
    z0 = z1

# >>> Kies hier de uitvoer-modus:
# Uitvoer op het scherm:
plt.show()
# Uitvoer in een png-bestand:
# plt.savefig('newton.png')
# <<<

Opgave 2

Download opgave2_pi.py

# NEWTON'S METHODE
# uitvoer in getallen

import numpy as np

# De functie f(x) = sin(x):
def f(x):
    y = np.sin(x)
    return y

# De afgeleide dfdx(x) = cos(x):
def dfdx(x):
    z = np.cos(x)
    return z

# >>> Voer hier het startpunt x0 in:
x0 = 2
# x0 = 1.8
# <<<

y0 = f(x0)
z0 = dfdx(x0)
print('startpunt: (x0,y0) = (',x0,',',y0,')')

print('iteratie, x1, y1:')
for i in range(10):
# Als z0 = 0, dan is x1 niet gedefinieerd:
    if z0 == 0:
        print('De berekening breekt af, omdat de afgeleide nul is.')
        break
# De volgende benadering x1:
    x1 = x0 - (y0/z0)
    y1 = f(x1)
    z1 = dfdx(x1)
    print(i+1,x1,y1)
# Als y1 = 0, dan is x1 het nulpunt:
    if y1 == 0:
        break
# Anders wordt x0 aangepast:
    x0 = x1
    y0 = y1
    z0 = z1

Opgave 3i

Download opgave3i.py

# NEWTON'S METHODE
# uitvoer in grafiek

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math

# Het plotten van de grafiek van de functie f(x):

matplotlib.use('TkAgg')

X0 = -4
X1 = 4
PN = 601
x = np.linspace(X0,X1,PN)

# De functie f(x) = arctan(x):
Titel = 'f(x) = arctan(x)'
f1 = np.arctan(x)

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,color='r',linewidth=1)
# plt.axis('equal')
ax.set_xlim(-4,4)
ax.set_ylim(-4,4)

# De functie f(x) = arctan(x):
def f(x):
    y = np.arctan(x)
    return y

# De afgeleide dfdx(x) = 1/(x*x + 1):
def dfdx(x):
    z = 1/(x*x + 1)
    return z

# >>> Varieer het startpunt x0:
# x0 = 1.25
x0 = 1.391745200270735
# x0 = 1.5
# <<<

y0 = f(x0)
z0 = dfdx(x0)

# Het plotten van het startpunt:
plt.plot(x0,0,marker='o',markersize=3,color='g')
plt.plot(x0,y0,marker='o',markersize=3,color='r')
plt.plot([x0,x0],[0,y0],linestyle='dotted',color='g',linewidth=1)

for i in range(4):
    if i > 0:
        plt.plot(x1,y1,marker='o',markersize=3,color='r')
        plt.plot([x1,x1],[0,y1],linestyle='dotted',color='g',linewidth=1)
# Als z0 = 0, dan is x1 niet gedefinieerd:
    if z0 == 0:
        print('De berekening breekt af, omdat de afgeleide nul is.')
        break
# De volgende benadering x1:
    x1 = x0 - (y0/z0)
    y1 = f(x1)
    z1 = dfdx(x1)
    plt.plot(x1,0,marker='o',markersize=3,color='g')
    plt.plot([x0,x1],[y0,0],color='g',linewidth=1)
# Als y1 = 0, dan is x1 het nulpunt:
    if y1 == 0:
        break
# Anders wordt x0 aangepast:
    x0 = x1
    y0 = y1
    z0 = z1

# >>> Kies hier de uitvoer-modus:
# Uitvoer op het scherm:
# plt.show()
# Uitvoer in een png-bestand:
plt.savefig('opgave3i.png')
# <<<

Opgave 3ii

Download opgave3ii.py

# BISECTIEMETHODE
# uitvoer in getallen

import numpy as np

# De functie f(x) = 2x - (x^2 + 1) arctan(x):
def f(x):
    y = 2*x - (x*x + 1)*np.arctan(x)
    return y

# De startpunten x0 en x1:
x0 = 1
x1 = 2

y0 = f(x0)
y1 = f(x1)
print('startpunten: (x0,y0) = (',x0,',',y0,'), (x1,y1) = (',x1,',',y1,')')

print('iteratie, x2, y2:')
for i in range(100):
# De volgende benadering x2 ligt midden tussen x0 en x1:
    x2 = (x0 + x1)/2
    y2 = f(x2)
    print(i+1,x2,y2)
# Als y2 = 0, dan is x2 het nulpunt:
    if y2 == 0:
        break
# Anders wordt x0 of x1 aangepast:
    if np.sign(y0) == np.sign(y2):
        x0 = x2
        y0 = y2
    else:
        x1 = x2

Opgave 4

Download opgave4_getallen.py

# NEWTON'S METHODE
# uitvoer in getallen

import numpy as np

# De functie f(x) = x^3 - 3x:
def f(x):
    y = x*x*x - 3*x
    return y

# De afgeleide dfdx(x) = 3x^2 - 3:
def dfdx(x):
    z = 3*x*x - 3
    return z

# >>> Varieer het startpunt x0:
x0 = np.sqrt(0.6)
# x0 = np.sqrt(0.6) + 0.0005
# x0 = np.sqrt(0.6) + 0.001
# <<<

y0 = f(x0)
z0 = dfdx(x0)
print('startpunt: (x0,y0) = (',x0,',',y0,')')

print('iteratie, x1, y1:')
for i in range(20):
# Als z0 = 0, dan is x1 niet gedefinieerd:
    if z0 == 0:
        print('De berekening breekt af, omdat de afgeleide nul is.')
        break
# De volgende benadering x1:
    x1 = x0 - (y0/z0)
    y1 = f(x1)
    z1 = dfdx(x1)
    print(i+1,x1,y1)
# Als y1 = 0, dan is x1 het nulpunt:
    if y1 == 0:
        break
# Anders wordt x0 aangepast:
    x0 = x1
    y0 = y1
    z0 = z1

Download opgave4_grafiek.py

# NEWTON'S METHODE
# uitvoer in grafiek

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math

# Het plotten van de grafiek van de functie f(x):

matplotlib.use('TkAgg')

X0 = -2
X1 = 2
PN = 601
x = np.linspace(X0,X1,PN)

# De functie f(x) = x^3 - 3x:
Titel = 'f(x) = x^3 - 3x'
f1 = x*x*x - 3*x

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,color='r',linewidth=1)
plt.axis('equal')

# De functie f(x) = x^3 - 3x:
def f(x):
    y = x*x*x - 3*x
    return y

# De afgeleide dfdx(x) = 3x^2 - 3:
def dfdx(x):
    z = 3*x*x - 3
    return z

# >>> Varieer het startpunt x0:
# x0 = 0.6
x0 = np.sqrt(0.6)
# x0 = 1.4
# <<<

y0 = f(x0)
z0 = dfdx(x0)

# Het plotten van het startpunt:
plt.plot(x0,0,marker='o',markersize=3,color='g')
plt.plot(x0,y0,marker='o',markersize=3,color='r')
plt.plot([x0,x0],[0,y0],linestyle='dotted',color='g',linewidth=1)

for i in range(10):
    if i > 0:
        plt.plot(x1,y1,marker='o',markersize=3,color='r')
        plt.plot([x1,x1],[0,y1],linestyle='dotted',color='g',linewidth=1)
# Als z0 = 0, dan is x1 niet gedefinieerd:
    if z0 == 0:
        print('De berekening breekt af, omdat de afgeleide nul is.')
        break
# De volgende benadering x1:
    x1 = x0 - (y0/z0)
    y1 = f(x1)
    z1 = dfdx(x1)
    plt.plot(x1,0,marker='o',markersize=3,color='g')
    plt.plot([x0,x1],[y0,0],color='g',linewidth=1)
# Als y1 = 0, dan is x1 het nulpunt:
    if y1 == 0:
        break
# Anders wordt x0 aangepast:
    x0 = x1
    y0 = y1
    z0 = z1

# >>> Kies hier de uitvoer-modus:
# Uitvoer op het scherm:
# plt.show()
# Uitvoer in een png-bestand:
plt.savefig('opgave4.png')
# <<<