function [M,T,Tmax_rez,Sigma_x_max_rez,Sigma_red_max_rez]=napeti_redukovane(fi,F,a_rez,b_rez,deleni,E,poisson,n,rez)
% VYPOČTY K BAKALÁŘSKÉ PRÁCI VALENTA TOMÁŠ
%
% Redukovane napeti

[M,T] = VVU(fi,F);

[Tmax,Txy,Txz,a_rez,b_rez,deleni] = napeti_krut_obdelnik(a_rez,b_rez,deleni,E,poisson,M,n);

% ----------VÝPOČET----------
Jy = (a_rez*b_rez^3)/12; % kvadraticky moment y
Jz = (b_rez*a_rez^3)/12; % kvadraticky moment z

y = -a_rez/2:deleni:a_rez/2; % [m]
z = -b_rez/2:deleni:b_rez/2;  % [m]

Uy = (a_rez/2)*((b_rez^2/4)-z'.^2); 
Uy = Uy*ones(1,length(y)); % linearni moment y
Uz = (b_rez/2)*((a_rez^2/4)-y.^2); 
Uz = ones(length(z),1)*Uz; % linearni moment y


Sigma_x = zeros(length(z),length(y));
for i=1:length(z)
    for j=1:length(y)
        Sigma_x(i,j) = (M(rez,2)/Jy)*z(i)-(M(rez,3)/Jz)*y(j);
    end
end
Sigma_x = ((Sigma_x)/1e6) + (T((rez),1)/(a_rez*b_rez))/1e6;  % normalove napeti [Mpa]
[Y,Z]=meshgrid(y,z);

Sigma_z = 0;
Sigma_y = 0;

Txy = Txy + ((T((rez),2)*Uz)/(b_rez*Jz))/1e6; % smykove napeti
Txz = Txz + ((T((rez),3)*Uy)/(a_rez*Jy))/1e6; % smykove napeti
Tyz = 0;

T_rez = sqrt(Txy.^2+Txz.^2);
Tmax_rez = max(max(T_rez));

Sigma_x_max_rez = max(max(Sigma_x));

Sigma_red = sqrt(Sigma_x.^2 + Sigma_y.^2 + Sigma_z.^2 - Sigma_x.*Sigma_y - Sigma_y.*Sigma_z -...
   Sigma_z.*Sigma_x + 3.*Txz.^2 + 3.*Tyz.^2 + 3.*Txy.^2); % redukovane napeti

Sigma_red_max_rez = max(max(Sigma_red));
[Sigma_red_max_rez_row,Sigma_red_max_rez_col] = find(Sigma_red == Sigma_red_max_rez);

% ------VYKRESLOVANÍ-------
% if rez == 21 % zvolen zpetne dle rezu kde je nejvetsi redukovane napeti
% figure(5)
% subplot(1,2,1)
% fontsize = 15;
% surf(Y,Z,Sigma_red,'EdgeColor','interp');
% title('Redukovane napeti','FontSize',fontsize)
% xlabel('a [m]','FontSize',fontsize)
% ylabel('b [m]','FontSize',fontsize)
% zlabel('Sigma_r_e_d [MPa]','FontSize',fontsize)
% set(gca, 'DataAspectRatio', [diff(get(gca, 'XLim')) diff(get(gca, 'XLim')) diff(get(gca, 'ZLim'))])
% hold on
% 
% for k=1:size(Sigma_red_max_rez_row,1)
%     plot3(Y(Sigma_red_max_rez_row(k),Sigma_red_max_rez_col(k)),Z(Sigma_red_max_rez_row(k),Sigma_red_max_rez_col(k)),...
%         Sigma_red(Sigma_red_max_rez_row(k),Sigma_red_max_rez_col(k)),'ok','MarkerSize',15,'LineWidth',3)
% end
% view(0,90)
% 
% subplot(1,2,2)
% surf(Y,Z,Sigma_red,'EdgeColor','interp');
% %title('Redukovane napeti','FontSize',fontsize)
% xlabel('a [m]','FontSize',fontsize)
% ylabel('b [m]','FontSize',fontsize)
% zlabel('Sigma_r_e_d [MPa]','FontSize',fontsize)
% set(gca, 'DataAspectRatio', [diff(get(gca, 'XLim')) diff(get(gca, 'XLim')) diff(get(gca, 'ZLim'))])
% hold on
% 
% for k=1:size(Sigma_red_max_rez_row,1)
%     plot3(Y(Sigma_red_max_rez_row(k),Sigma_red_max_rez_col(k)),Z(Sigma_red_max_rez_row(k),Sigma_red_max_rez_col(k)),...
%         Sigma_red(Sigma_red_max_rez_row(k),Sigma_red_max_rez_col(k)),'ok','MarkerSize',15,'LineWidth',3)
% end
% view(0,0)
% colorbar

%print '-depsc' 'c:/Users/Tomáš Valenta/Škola/Vejška/_bachelory thesis/zprava/obrazky/18_redukovane_napeti_detail.eps'
%fprintf('Maximální redukované napětí v průřezu je: %f MPa\n',Sigma_red_max_rez)
% end

end