This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
132 changed files
with
510 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Dec 15 17:11:45 2020 | ||
SEP FS20 Aufgabe 2 | ||
@author: knaa | ||
""" | ||
|
||
# a) | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def f(x): | ||
return x * np.exp(x) | ||
|
||
|
||
def df(x): | ||
return np.exp(x) * (x + 1) | ||
|
||
|
||
def K(x): | ||
return np.abs(x) * np.abs(df(x)) / np.abs(f(x)) | ||
|
||
|
||
# K = abs(x + 1) | ||
x = np.arange(-4, 2.05, 0.05) | ||
plt.plot(x, K(x), "-"), plt.xlabel("x"), plt.ylabel("K(x)") | ||
|
||
# b) | ||
""" | ||
Gut konditioniertes Problem fuer K <= 1 | ||
Forderung: |x+1| <= 1 | ||
Fuer x > -1: | ||
Forderung: 1 + x <= 1 | ||
=> x <= 0 2P | ||
Fuer x < -1: | ||
Forderung: -(1+x) <= 1 | ||
=> -1-x <= 1 | ||
=> -x <= 2 | ||
=> x >= -2 2P | ||
Gute Konditionierung fuer -2 <= x <= 0 1P | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Dec 15 17:37:39 2020 | ||
SEP FS20 Aufgabe 3 | ||
@author: knaa | ||
""" | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
N = 100 | ||
epsIncr = 1e-6 | ||
|
||
xL = 1 | ||
xR = 2 | ||
|
||
|
||
def F(x): | ||
return 1.0 / (np.cos(x + np.pi / 4) - 1) + 2 | ||
|
||
|
||
def dF(x): | ||
return np.sin(x + np.pi / 4) / (np.cos(x + np.pi / 4) - 1) ** 2 | ||
|
||
|
||
# Achtung: die Ableitung konnte damals mit Matlab berechnet werden. In Python sind wir noch nicht so weit. | ||
# Allerdings ist auch die Berechnung der Ableitung von Hand nicht wirklich schwer. | ||
|
||
# Aufgabe a) | ||
print("\nFixpunktiteration a)\n") | ||
|
||
xPlot = np.arange(0, np.pi + 0.01, 0.01) | ||
plt.figure(1) | ||
plt.plot( | ||
xPlot, | ||
xPlot, | ||
xPlot, | ||
F(xPlot), | ||
xPlot, | ||
np.abs(dF(xPlot)), | ||
[xL, xL], | ||
[0, np.pi], | ||
"k-.", | ||
[xR, xR], | ||
[0, np.pi], | ||
"k-.", | ||
) | ||
plt.legend(["y=x", "y=F(x)", "y=abs(F'(x))"]) | ||
plt.axis([0, np.pi, 0, np.pi]) | ||
|
||
# Aufgabe b) | ||
print("\nFixpunktiteration b)\n") | ||
|
||
fx = F(np.array([xL, xR])) | ||
fxMin = np.min(fx) | ||
fxMax = np.max(fx) | ||
dfx = dF(np.array([xL, xR])) | ||
lambd = np.max(np.abs(dfx)) | ||
|
||
print(lambd < 1) | ||
print(fxMin > xL) | ||
print(fxMax < xR) | ||
# Banach erfüllt, wenn 3 x logisch 1 | ||
|
||
# Aufgabe c) | ||
print("\nFixpunktiteration c)\n") | ||
lambd | ||
xj = 1.3376 | ||
xj1 = 1.3441 | ||
errEst = lambd / (1 - lambd) * (xj1 - xj) | ||
|
||
# Verbesserung von lambda (da Graph von dF monoton abnehmend) | ||
xj = 1.3376 | ||
xj1 = 1.3441 | ||
lambdaEst = dF(xj) | ||
errEst = lambdaEst / (1 - lambdaEst) * (xj1 - xj) | ||
|
||
# Aufgabe d) | ||
print("\nFixpunktiteration d)\n") | ||
|
||
|
||
def fixIt(f, x0, epsIncr, lambd): | ||
import numpy as np | ||
|
||
k = 0 | ||
notConverged = True | ||
N = 1000 | ||
|
||
x0 | ||
while notConverged and k < N: | ||
x1 = f(x0) | ||
incr = np.abs(x1 - x0) | ||
error = lambd / (1 - lambd) * incr | ||
notConverged = error > epsIncr | ||
k = k + 1 | ||
x0 = x1 | ||
n = k | ||
return (x1, n) | ||
|
||
|
||
x0 = 1 | ||
[xF, n] = fixIt(F, x0, epsIncr, lambd) | ||
print("xF=%.4E" % xF, "(n=%.4E Iterationen)\n" % n) | ||
|
||
# Aufgabe e) | ||
print("\nFixpunktiteration e)\n") | ||
|
||
|
||
def fA(x): | ||
return (x - 1) / (x - 2) - np.cos(x + np.pi / 4) | ||
|
||
|
||
def fB(x): | ||
return F(x) | ||
|
||
|
||
def fC(x): | ||
return x + (2 - 1 / (np.cos(x + np.pi / 4) - 1)) | ||
|
||
|
||
def fD(x): | ||
return np.cos(x + np.pi / 4) - 1 / (x - 2) - 1 | ||
|
||
|
||
print("A ist ", np.abs(fA(xF)) < 1e-6) | ||
print("B ist ", np.abs(fB(xF)) < 1e-6) | ||
print("C ist ", np.abs(fC(xF)) < 1e-6) | ||
print("D ist ", np.abs(fD(xF)) < 1e-6) | ||
# d.h. korrekt ist A) und D) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Dec 15 18:29:47 2020 | ||
SEP FS20 Aufgabe 4 | ||
@author: knaa | ||
""" | ||
|
||
import numpy as np | ||
|
||
# Aufgabe a) | ||
A = np.array([[15, 0, 1], [1, 3, 7], [0, 1, 6]]) | ||
y = np.array([[21, 67, 44]]).T | ||
|
||
|
||
""" | ||
Die 1. und 2. Zeile müssen vertauscht werden, da ansonsten ein | ||
Diagonal-Element 0 ist und die Diagonaldominanz nicht mehr gegeben ist ! | ||
""" | ||
|
||
D = np.diag(np.diag(A)) | ||
R = np.triu(A) - D | ||
L = np.tril(A) - D | ||
|
||
# Aufgabe b) | ||
|
||
x = np.array([[0, 0, 0]]).T | ||
|
||
for k in np.arange(1, 7): | ||
x = -np.linalg.inv(D + L) @ R @ x + np.linalg.inv(D + L) @ y | ||
print(x) | ||
|
||
|
||
# Aufgaben c) | ||
|
||
""" | ||
Bei grossen Gleichungssystemen ist der numerische Aufwand von exakten | ||
Lösungsverfahren zu gross. Iterative Verfahren sind bei solchen | ||
(insbesondere diagonal dominanten) Gleichungsystemen effizienter | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Dec 15 18:49:02 2020 | ||
SEP FS20 Aufgabe 5 | ||
@author: knaa | ||
""" | ||
|
||
""" | ||
a) Gleichungssystem | ||
A = | ||
240 120 80 | ||
60 180 170 | ||
60 90 500 | ||
b = | ||
3080 | ||
4070 | ||
5030 | ||
b) Gauss Schritte | ||
i=1,j=2 = | ||
240 120 80 3080 | ||
0 150 150 3300 | ||
60 90 500 5030 | ||
i=1,j=3 = | ||
240 120 80 3080 | ||
0 150 150 3300 | ||
0 60 480 4260 | ||
i=2,j=3 = | ||
240 120 80 3080 | ||
0 150 150 3300 | ||
0 0 420 2940 | ||
Rückwärts einsetzen ergiebt: x1 = 3, x2=15, x3=7 | ||
c) Relativer und Absoluter Fehler | ||
|| x - x~|| <= || A^-1 ||*||b-b~|| in inf norm == 0.0113*609 == 6.8875 | ||
|| x - x~|| / || x || <= ||A||*|| A^-1 ||*||b-b~||/||b|| in inf norm == | ||
650 * 0.0113*609/5030 == 0.89 = 89% | ||
d) cond(A) = ||A||*|| A^-1 || = 7.3512 | ||
""" |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# ------------------------------------------------------------ | ||
# SEP HS20 HM1 / LOESUNG AUFGABE 2 / adel | ||
# ------------------------------------------------------------ | ||
|
||
# TEILAUFGABE a) | ||
|
||
# f'(x)*x (2*x*sin(x)+x^2*cos(x))*x | ||
# K(x) = |---------| = |---------------------------| | ||
# f(x) x^2*sin(x) | ||
# | ||
# 2*sin(x)+x*cos(x) | ||
# = |------------------| | ||
# sin(x) 1 P | ||
# =================== | ||
|
||
# TEILAUFGABE b) | ||
|
||
# Naeherungsweise gilt | ||
# | ||
# |(f(x)-f(x0))/f(x0)| = K(x0)*|(x-x0)/x0| 0.5 P | ||
# | ||
# Aus |(f(x)-f(x0))/f(x0)| <= 0.1 folgt | ||
# | ||
# |x-x0| <= 0.1/K(x0)*x0 1 P | ||
|
||
import numpy as np | ||
|
||
|
||
def K(x): | ||
return np.abs((2 * np.sin(x) + x * np.cos(x)) / np.sin(x)) | ||
|
||
|
||
x0 = np.pi / 3 | ||
y0_err_rel = 0.1 | ||
x0_err_abs = y0_err_rel / K(x0) * x0 | ||
print(x0_err_abs) | ||
# 0.040205699009494354 = 0.04021 1.5 P | ||
# ======= | ||
|
||
# TEILAUFGABE c) | ||
|
||
print([K(10**-n) for n in range(1, 6)]) | ||
# [2.9966644423259243, | ||
# 2.999966666444442, | ||
# 2.999999666666645, | ||
# 2.9999999966666664, | ||
# 2.9999999999666667] 2 P | ||
|
||
# Fuer x -> 0 konvergiert K(x) -> 3. Dementsprechend | ||
# ist f(x) in x = 0 gut konditioniert. | ||
# ================================================== 1 P | ||
|
||
# TEILAUFGABE d) | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
x = np.arange(-2 * np.pi, 3 * np.pi, 10**-4) | ||
plt.semilogy(x, K(x)) | ||
plt.xlabel("x") | ||
plt.ylabel("K(x)") | ||
plt.grid() # 2 P | ||
|
||
# Der Plot zeigt, dass f(x) fuer x in der Umgebung von +/- pi, | ||
# +/- 2*pi, +/- 3*pi, usw. schlecht konditioniert ist. | ||
# =================================================== 1 P |
Oops, something went wrong.