Rechnen mit Matrizen und Daten in Technik und Wissenschaft
Ausführen: Mit MATLAB (kommerziell) oder kostenlos mit GNU Octave: octave skript.m startet eine Datei skript.m; fast alle Beispiele hier laufen in beiden.
; wird das Ergebnis angezeigt; disp und fprintf zur Ausgabe% leitet Kommentare einmod/rem/floor/round für Ganzzahlenoctave # interaktive Konsole
octave --no-gui skript.m # Skript ausführen
octave --eval "disp(1+2)"[1 2 3] (Zeile), [1; 2; 3] (Spalte), 1:5, linspaceA(zeile, spalte), : = alle, end = letztes* ist Matrixprodukt, .* elementweiseA \ bsum, mean, max, sort, find, cumsum wirken spaltenweise auf Matrizenzeile = [1 2 3 4 5] % Zeilenvektor (Leerzeichen oder Komma)
spalte = [1; 2; 3] % Spaltenvektor (Semikolon)
von_bis = 1:5
schritt = 0:0.25:1
runter = 10:-3:0
linspace(0, 1, 5) % 5 gleichmäßig verteilte Punkte
logspace(0, 3, 4)
zeros(1, 3)
ones(2, 2)
numel(zeile)
length(spalte)
size(zeile)
zeile(1) % Indizes beginnen bei 1!
zeile(end)
zeile(2:4)
zeile([1 3 5])
zeile(zeile > 2) % logische Indizierung
zeile' % transponierenif/elseif/else, switch, for, while, break, continuex .^ 2, sum(x(x > 0))) statt Schleifenfunction [a, b] = name(x), nargin, varargin, ~ ignoriert Ausgaben@(x) ..., Handles @sin, arrayfun, cellfuntry/catch, error, assert1;, wenn sie Funktionen definierenx = 7;
if x > 10
disp('groß')
elseif x > 5
disp('mittel')
else
disp('klein')
end
if x > 5 && mod(x, 2) == 1 % && und || für einzelne Werte
disp('größer als 5 und ungerade')
end
wert = 'b';
switch wert
case 'a'
disp('A')
case {'b', 'c'}
disp('B oder C')
otherwise
disp('anderes')
end
a = [1 0 1];
b = [1 1 0];
a & b % elementweise
a | b
~a
xor(a, b)'...'; strcat, strsplit, strjoin, strrep, regexprep, sprintf{...} speichern Verschiedenes; c{i} Inhalt, c(i) Teil-Cellstruct mit Feldern (s.name), Struct-Arrays und dynamische Felder s.(name)containers.Map für Schlüssel/Wertcellfun/arrayfun wenden Funktionen elementweise ans = 'Hallo Welt';
length(s)
upper(s)
lower(s)
s(1:5)
s(end:-1:1)
[s ' und Octave']
strcat('a', 'b', 'c')
strrep(s, 'Welt', 'MATLAB')
strfind(s, 'l')
strsplit('a,b,c', ',')
strjoin({'x', 'y', 'z'}, '-')
strtrim(' getrimmt ')
strcmp('abc', 'abc')
strcmpi('ABC', 'abc')
num2str(3.14159)
num2str(42)
str2num('7 + 1')
str2double('2.5')
sprintf('%05.1f|%-5s|%d', 3.14159, 'ab', 42)
regexprep('a1b22', '\d+', '#')
regexp('Preis: 12 und 34', '\d+', 'match')
double('A')
char(72, 105)
char([72 105])
isspace('a b')
fliplr('abc')
strcmp({'a', 'b', 'a'}, 'a')
upper({'x', 'y'})
numel(sprintf('%d', 12345))polyfit/polyval für Kurvenanpassung, interp1 für Interpolationintegral, trapz, fzero, fminsearch für Integrale, Nullstellen, Optimierungode45 löst Differentialgleichungeneig, inv, det, qr, lu, svd, A \ bmean, std, corr, prctile, Zufallszahlen rand, randn, randip = [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) / 100plot, bar, hist, surf; hold on, subplot, xlabel, legend, print zum Speichernfopen/fprintf/fgetl/fclose, dlmwrite/dlmread, save/load, filereadtic/tocx = linspace(0, 2*pi, 200);
y1 = sin(x);
y2 = cos(x);
figure;
plot(x, y1, 'b-', 'LineWidth', 2);
hold on;
plot(x, y2, 'r--');
hold off;
grid on;
xlabel('x'); ylabel('y');
title('Sinus und Kosinus');
legend('sin(x)', 'cos(x)', 'Location', 'northeast');
axis([0 2*pi -1.2 1.2]);
print -dpng diagramm.pngconv2, fft, \, filter) werden komplexe Algorithmen kurzsqrt, norm, exakte Lösungen)1;
function [x, schritte] = newton(f, df, x0, tol)
x = x0;
schritte = 0;
while abs(f(x)) > tol && schritte < 50
x = x - f(x) / df(x);
schritte = schritte + 1;
end
end
f = @(x) x.^2 - 2;
df = @(x) 2 * x;
[x, n] = newton(f, df, 1, 1e-12);
fprintf('sqrt(2) = %.10f nach %d Schritten (Fehler %.1e)\n', x, n, abs(x - sqrt(2)));
[x2, n2] = newton(@(x) cos(x) - x, @(x) -sin(x) - 1, 0.5, 1e-12);
fprintf('Lösung von cos(x) = x: %.8f nach %d Schritten\n', x2, n2);.*) vs. Matrix-Operationen (*)end ist das letzte ElementA \ b löst Gleichungssysteme, polyfit, ode45, integral decken viel Numerik abv = [3 8 1 9 4];
v(v > 3) % logisches Indizieren
[~, idx] = sort(v, 'descend'); % Sortier-Index
v(idx(1:2)) % größte zwei
cumsum(v) ./ (1:numel(v)) % laufender Mittelwert
any(v == 9) % Existenzprüfung
x = linspace(0, 1, 5);
y = x .^ 2 - x; % vektorisierte Funktion
[wert, pos] = min(y)
isequal([1 2 3], 1:3)
mat2str([1 2; 3 4])
num2str([1 2 3])
ifelse = @(c, a, b) a * c + b * ~c;
ifelse(true, 10, 20)