# Kamil Breguła - ONIMN - Lab 1 ## Laboratorium 1 - Wstęp/Przypomnienie Matlab’a.. ### Zadanie 1 ```matlab exp(-i * pi) exp(-i * pi + 1) log(exp(1)) log10(exp(1)) 2^(-1) ``` ### Zadanie 2 1. ```matlab reshape([1:100], 10, 10) ``` 2. ```matlab t = zeros(10, 10) for row=1:10 for col=1:10 t(row, col) = row * col end; end; t; ``` 3. ```matlab Z=zeros(21,21); for row=1:21 for col=1:21 Z(row, col)=sqrt((row - 11) ^ 2 + (col - 11)^2); end; end; Z; ``` ### Zadanie 3 1. ```matlab hold on cla x = -pi:0.05:pi A = sin(x) .^ 2 B = sin(x .* 2) .^ 2 C = sin(x) .^ 4 plot(x, A, 'r--') plot(x, B, 'gd:') plot(x, B, 'b-') hold off ``` 2. ```matlab hold on cla x = -pi:0.05:pi A = sin(x) .^ 2 B = sin(x .* 2) .^ 2 C = sin(x) .^ 4 plot(x, A, 'r--') plot(x, B, 'gd:') plot(x, B, 'b-') legend('sin(\alpha)^2', 'sin(2\alpha)^2','sin(\alpha)^4'); hold off ``` 3. ```matlab hold on cla x = -pi:0.05:pi A = sin(x) .^ 2 B = sin(x .* 2) .^ 2 C = sin(x) .^ 4 plot(x, A, 'r--') plot(x, B, 'gd:') plot(x, B, 'b-') legend('sin(\alpha)^2', 'sin(2\alpha)^2','sin(\alpha)^4'); grid on hold off ``` 4. ```matlab hold on cla x = -pi:0.05:pi A = sin(x) .^ 2 B = sin(x .* 2) .^ 2 C = sin(x) .^ 4 plot(x, A, 'r--') plot(x, B, 'gd:') plot(x, B, 'b-') legend('sin(\alpha)^2', 'sin(2\alpha)^2','sin(\alpha)^4'); grid on text(0, 0, '(0,0)') hold off ``` 5. ```matlab hold on cla x = -pi:0.05:pi A = sin(x) .^ 2 B = sin(x .* 2) .^ 2 C = sin(x) .^ 4 plot(x, A, 'r--') plot(x, B, 'gd:') plot(x, B, 'b-') legend('sin(\alpha)^2', 'sin(2\alpha)^2','sin(\alpha)^4'); grid on text(0, 0, '(0,0)') ylabel('Zmienna X') xlabel('Zmienna Y') hold off ``` 6. ```matlab hold on cla x = -pi:0.05:pi A = sin(x) .^ 2 B = sin(x .* 2) .^ 2 C = sin(x) .^ 4 plot(x, A, 'r--') plot(x, B, 'gd:') plot(x, B, 'b-') legend('sin(\alpha)^2', 'sin(2\alpha)^2','sin(\alpha)^4'); grid on text(0, 0, '(0,0)') ylabel('Zmienna X') xlabel('Zmienna Y') title('Wykresy') hold off ``` 7. ![](https://i.imgur.com/Jnx6i4Q.png) ### Zadanie 4 1. 2. ``` [X,Y] = meshgrid(-pi:0.2:pi); f = @(x, y) sin(x).*sin(y).*exp((-x).*(-y)-x.*y); Z = f(X, Y) tiledlayout(3, 3) nexttile contour3(X,Y,Z) title('contour3') nexttile mesh(X,Y,Z) title('mesh') nexttile meshc(X,Y,Z) title('meshc') nexttile meshz(X,Y,Z) title('meshz') nexttile surf(X,Y,Z) title('surf') nexttile surfc(X,Y,Z) title('surfc') nexttile surfl(X,Y,Z) title('surfl') nexttile waterfall(X,Y,Z) title('waterfall') nexttile imagesc(x,y,Z) title('imagesc') ``` ![](https://i.imgur.com/n9uESsa.png) 3. ``` [X,Y] = meshgrid(-pi:0.2:pi); f = @(x, y) sin(x).*sin(y).*exp((-x).*(-y)-x.*y); Z = f(X, Y) tiledlayout(2, 2) nexttile shading FLAT surf(X,Y,Z) title('FLAT') nexttile shading INTERP surf(X,Y,Z) title('INTERP') nexttile shading FACETED surf(X,Y,Z) title('FACETED') ``` ![](https://i.imgur.com/VDRdUXL.png) ### Zadanie 5 ```matlab function y = fibonacci(n, metoda) if metoda == 1 y = fib_petla(n) elseif metoda == 2 y = fib_rekurencja(n) elseif metoda == 3 y = fib_wzor(n) else error('Nieznana metoda') end ``` ```matlab function y = fib_petla(n) buff = zeros(1, n + 2); buff(1, 2) = 1; for i = 3:n + 1 buff(1, i) = buff(1, i - 1) + buff(1, i - 2); end y = buff(1, n + 1); ``` ```matlab function y = fib_wzor(n) y = 1 / sqrt(5) .* (((1 + sqrt(5)) / 2) .^ (n) - ((1 - sqrt(5)) / 2).^ n); ``` ```matlab function y = fib_rekurencja(n) if (n == 0) y = 0; elseif (n == 1) y = 1; else y = fib_rekurencja(n-1) + fib_rekurencja(n-2); end ``` ```matlab max_x = 10; result = zeros(3, max_x); retry = 400; for y=1:3 for x=1:max_x tic for i=1:retry fibonacci(x, y); end r = toc ./ retry; result(y, x) = r; end end cla clf hold on plot(1:max_x, result(1, :), 'rd-') plot(1:max_x, result(2, :), 'gd-') plot(1:max_x, result(3, :), 'bd-') legend('petla', 'rekurencja', 'wzor') hold off ``` ![](https://i.imgur.com/5q4t8Xc.png) Metoda rekurencyjnaa jest najwolniejsz. Metoda z pętlą i z wzorem jest złożoność zbliżoną do wartości stałej. Metoda z pętlą wykonuje się dłużej z powodu allokowania tablicy na potrzebne elementy. Metoda rekurencyjna jest najwolniejsza z powodu nakładu pracy wynikającego z obsługi rekurencji oraz wielokrotnie tej samej wartości. Metoda pętla jest optymalna w porównaania mętodą z pętlą, ponięważ metoda z pętlą każdy element oblicza tylko raz. Metoda z wzorem ma stałą złożonośc, ponieważ ilość obliczeń nie zależy od parametrów wejściowych. W pozostałych metodach liczba wykonywanych operacji zależy od parametrów wejściowych. ### Zadanie 6 1. ```matlab A = randn(10:10) ``` 2. ```matlab A = randn(10:10); result = []; tab = A(:) for i = 1:numel(array) el = tab(idx); if el > 0 result = [result; el]; end end ``` 3. ```matlab any(A(:) < 0) ``` 4. ```matlab [ floor(find(A==max(A(:))) / 10),... rem(find(A==max(A(:))), 10)... ] ``` 5. ```matlab (abs(A) < 0.25) .* A ```