Polynome und Kurvenanpassung
p = [1 -3 2]; % x^2 - 3x + 2
roots(p)'
polyval(p, 3)
polyder(p)
polyint(p)
conv([1 1], [1 -1]) % (x+1)(x-1)
x = 0:5;
y = [1.1 2.9 5.2 7.1 8.8 11.2];
koef = polyfit(x, y, 1) % Gerade y = a*x + b
y_fit = polyval(koef, x);
fehler = sqrt(mean((y - y_fit).^2))
round(polyval(koef, 10) * 100) / 100Ausgabe
ans = 2 1 ans = 2 ans = 2 -3 ans = 0.3333 -1.5000 2.0000 0 ans = 1 0 -1 koef = 2.0029 1.0429 fehler = 0.1499 ans = 21.070
Interpolation, Integration, Nullstellen
x = [0 1 2 3];
y = [0 1 4 9];
interp1(x, y, 1.5)
interp1(x, y, [0.5 2.5], 'linear')
interp1(x, y, 1.5, 'spline')
f = @(x) sin(x);
integral(f, 0, pi)
trapz(linspace(0, pi, 1001), sin(linspace(0, pi, 1001)))
quad(@(x) exp(-x.^2), -5, 5)
sqrt(pi)
fzero(@(x) cos(x) - x, [0 1])
x_min = fminbnd(@(x) (x - 2).^2 + 3, 0, 5)
[x_opt, wert] = fminsearch(@(v) (v(1) - 1)^2 + (v(2) + 2)^2, [0 0]);
round(x_opt * 1000) / 1000
round(wert * 1e6) / 1e6Ausgabe
ans = 2.5000 ans = 0.5000 6.5000 ans = 2.2500 ans = 2.0000 ans = 2.0000 ans = 1.7725 ans = 1.7725 ans = 0.7391 x_min = 2 ans = 1 -2 ans = 0
Differentialgleichungen
Numerische Lösung von y' = -2y mit Startwert y(0) = 1 (exakte Lösung: exp(-2t)):
[t, y] = ode45(@(t, y) -2 * y, [0 2], 1);
exakt = exp(-2 * t);
fehler = max(abs(y - exakt))
fehler < 1e-3
fprintf('y(2) = %.4f (exakt %.4f)\n', y(end), exp(-4));
% Eigenes Euler-Verfahren
h = 0.01;
ys = 1;
for k = 1:200
ys = ys + h * (-2 * ys);
end
fprintf('Euler: %.4f\n', ys);
% Räuber-Beute-System (Lotka-Volterra)
lv = @(t, z) [z(1) * (1 - 0.5 * z(2)); z(2) * (-0.75 + 0.25 * z(1))];
[t, z] = ode45(lv, [0 10], [2; 1]);
fprintf('Schritte > 20: %d, max Beute %.2f\n', numel(t) > 20, max(z(:, 1)));Ausgabe
fehler = 2.2228e-06 ans = 1 y(2) = 0.0183 (exakt 0.0183) Euler: 0.0176 Schritte > 20: 1, max Beute 6.11
Lineare Algebra
A = [4 -2; 1 1];
[V, D] = eig(A);
diag(D)'
A * V(:, 1) - D(1, 1) * V(:, 1) < 1e-10
norm(A)
cond(A)
[Q, R] = qr(A);
round(Q * R)
[L, U, P] = lu(A);
round(P' * L * U)
pinv([1 2; 2 4])
null([1 1; 1 1])'
svd([3 0; 0 4])'
expm(zeros(2))
b = [2; 3];
x = A \ b
A * xAusgabe
ans = 3 2 ans = 1 1 ans = 4.4966 ans = 3.3699 ans = 4 -2 1 1 ans = 4 -2 1 1 ans = 0.040000 0.080000 0.080000 0.160000 ans = 0.7071 -0.7071 ans = 4 3 ans = Diagonal Matrix 1 0 0 1 x = 1.3333 1.6667 ans = 2 3
Statistik und Zufall
rand('seed', 1);
randn('seed', 1);
x = [2 4 4 4 5 5 7 9];
mean(x), median(x), mode(x), std(x), var(x)
range(x)
prctile(x, [25 50 75])
histc(x, 0:2:10)
corr([1 2 3 4]', [2 4 5 9]')
corrcoef([1 2 3 4], [2 4 6 8])
cov([1 2 3 4], [1 2 3 4])
cumsum(x) ./ (1:numel(x))
numel(randperm(10))
all(randi(6, 1, 100) >= 1)
zufall_in_bereich = all(rand(1, 100) < 1)
1 / sqrt(2*pi) % Dichte der Standardnormalverteilung bei 0
0.5 * (1 + erf(1.96 / sqrt(2))) % Verteilungsfunktion bei 1.96
nchoosek(5, 2)
factorial(5)
primes(30)
isprime(97)
gcd(48, 18)
lcm(4, 6)Ausgabe
ans = 5
ans = 4.5000
ans = 4
ans = 2.1381
ans = 4.5714
ans = 7
ans =
4.0000 4.5000 6.0000
ans =
0 1 5 1 1 0
ans = 0.9648
ans =
1 1
1 1
ans = 1.6667
ans =
2.0000 3.0000 3.3333 3.5000 3.8000 4.0000 4.4286 5.0000
ans = 10
ans = 1
zufall_in_bereich = 1
ans = 0.3989
ans = 0.9750
ans = 10
ans = 120
ans =
2 3 5 7 11 13 17 19 23 29
ans = 1
ans = 6
ans = 12Merke
polyfit/polyvalfür Kurvenanpassung,interp1für Interpolationintegral,trapz,fzero,fminsearchfür Integrale, Nullstellen, Optimierungode45löst Differentialgleichungen- Lineare Algebra:
eig,inv,det,qr,lu,svd,A \ b - Statistik:
mean,std,corr,prctile, Zufallszahlenrand,randn,randi
Aufgabe
Passe ein Polynom zweiten Grades an die Punkte (0,1), (1,3), (2,9), (3,19) an und bestimme den Wert bei x = 4.