% CVICENI - 3

%-------------------------------------------------------------------------%
% a) Matice a vektory - pokracovani
clc;
clear all;

u = [1,2,3];
v = [4;5;6];
w = [-1,2,5];

% ruzne varianty vypoctu skalarnich soucinu
S = dot(u,v)
S = u*v
S = u*w'
S = sum(u.*w)

% vektorovy soucin
z = cross(u,v)

% overeni zda je vysledek vektoroveho soucinu z kolmy na u,v
is_prependicular = dot(z,u)==0 & dot(z,v)==0;
is_prependicular

%-------------------------------------------------------------------------%
% b) prace s polem
clc;
clear all;

A = [45, 55, 30, 74, 19, 69, 18, 37, 63, 78];
mean_A   = mean(A)
Min_A    = min(A)
Max_A    = max(A)
Sum_A    = sum(A)
length_A = length(A)

[Min_A, index] = min(A)
[Max_A, index] = max(A)

%-------------------------------------------------------------------------%
% c) for cyklus
for i=1:10
    disp(i);
end

% funkce faktorial (pomoci for cyklu)
F = faktorial(6)
% funkce faktorial z Matlab knihovny
F = factorial(6)

% Mocneni po prvcich pomoci for cyklu:
A = rand(3,5)
A3 = zeros(3,5) % zde budeme ukladat vysledek

for i=1:size(A,1) % cyklus pres radky
    for j=1:size(A,2) % cyklus pres sloupce
        A3(i,j) = A(i,j)^3; % do matice A3 prirazujeme prvky A(i,j)^3
    end
end

% overeni spravnosti
rozdil = A.^3 - A3


% soucet prvku vektoru (for cyklus)
X = [1,2,3,4,5,6,7,8,9];
soucet = 0;
for i=1:length(X)
    soucet = soucet + X(i);
end
prumer = soucet / length(X)
fprintf('Soucet je: %.1f\n',soucet);
fprintf('Prumer je: %.1f\n',prumer);

%-------------------------------------------------------------------------%
% d) uvod - grafy

x = linspace(-pi,pi,1000);
y = exp(abs(x)) .* sin(100*x);

f1 = figure(1);
plot(x,y)

title(['Line Plot of function: y(x) = exp(|x|) * sin(100x) ' ...
    'between -\pi and \pi']);
xlabel('-\pi < x < \pi') 
ylabel('y')


x = linspace(-pi,pi,1000);
length(x)
y1 = sin(1*x);
y2 = sin(2*x);
y3 = sin(3*x);

f2 = figure(2);
plot(x,y1, x,y2, x,y3)
grid on;
legend({'y = sin(x)','y = sin(2x)', 'y = sin(3x)'},'Location','southwest')


% polynom p(x) = a1 + a2*x + a3*x^2
x = linspace(-1,1,500);
a = [2,5,6];

p = a(1) + a(2)*x + a(3)*x.^2;
f3 = figure(3)
plot(x,p)
grid on;


clc;
clear all;


% polynom N-teho stupne p(x) = a1 + a2*x + a3*x^2 + a3*x^3 + ...
N = input('Nacti stupen polynomu N = ')
for i=1:N
    fprintf('Nacti koeficient a_%.0f\n',i)
    a(i) = input('')
end

D = 500; % pocet deleni intervalu
x = linspace(-1,1,D);
p = zeros(1,D);
for i=1:N
    p = p + a(i) * x.^(i-1)
end

f4 = figure(4)
plot(x,p)
grid on;
% ylim([-2 2]);


% tayloruv polynom fce f(x) = cos(x) v bode x0 = 0, stupne N:
% UKOL
% f4 = figure(4)
% plot(x,T, x,sin(x))
% grid on;
% ylim([-2 2]);


%%%%%%% FUNKCE %%%%%%
function F = faktorial(N)
    if(N < 0)
        disp('Nelze spocitat pro zaporne hodnoty!');
        return;
    elseif(N==0)
        F = 1;
    else
        F = 1;
        for i=1:N
            F = F*i;
        end
    end
end