clear all;
close all;
clc;

Fo=1000; %[N] (osova)
Fa=1000; %[N] (bocni)
Fb=1000; %[N] (celni)
D=0.35; %[m]
K=0.68; %[m]

krok_alpha=5;
krok_beta=2;

ALPHA=[0:krok_alpha:360];
BETA=[0:krok_beta:90];

[X,Y]=meshgrid(ALPHA,BETA);

for i=1:length(ALPHA)
    for j=1:length(BETA)
        
alpha1=ALPHA(i); %[ï¿½] (uhel natoceni v ose C )
beta1=BETA(j); %[ï¿½] (uhel naklopeni v ose A)

m2=450; %[kg](hmotnost telesa 2)
m3=200; %[kg](hmotnost telesa 3)

G2=m2*9.81;
G3=m3*9.81;

alpha=alpha1*pi/180;
beta=beta1*pi/180;

%rozklad osove sily
Foz=Fo*cos(beta);
Foxy=Fo*sin(beta);
Fox=Foxy*cos(alpha);
Foy=Foxy*sin(alpha);

%rozklad kolme bocni sily
Fax=-Fa*cos(alpha);
Fay=-Fa*sin(alpha);
Faz=0;

%rozklad kolme celni sily (proti zepredu)
Fbz=Fb*cos(beta);
Fbxy=Fb*sin(beta);
Fby=Fbxy*cos(alpha);
Fbx=Fbxy*sin(alpha);

Fx=sqrt((Fox^2)+(Fax^2)+(Fbx^2));
Fy=sqrt((Foy^2)+(Fay^2)+(Fby^2));
Fz=sqrt((Foz^2)+(Faz^2)+(Fbz^2));


A= [
    1 0 0 -1 0 0 0 0 0 0 0 0;
    0 1 0 0 -1 0 0 0 0 0 0 0;
    0 0 1 0 0 -1 0 0 0 0 0 0;
    0 K 0 0 0 0 1 0 0 -1 0 0;
    K 0 0 0 0 0 0 1 0 0 -1 0;
    0 0 0 0 0 0 0 0 1 0 0 -1;
    0 0 0 1 0 0 0 0 0 0 0 0;
    0 0 0 0 1 0 0 0 0 0 0 0;
    0 0 0 0 0 1 0 0 0 0 0 0;
    0 0 0 0 0 0 0 0 0 1 0 0;
    0 0 0 0 0 0 0 0 0 0 1 0;
    0 0 0 0 0 0 0 0 0 0 0 1;
    ];

b= [0; 0; -G2; 0; 0; 0; Fx; Fy; Fz-G3; Fy*(D*cos(beta)); Fz*(D*sin(beta))+Fx*(D*cos(beta)); Fy*(D*sin(beta));];

x=inv(A)*b;

RAx=x(1,1);
RAy=x(2,1);
%disp('Zatizeni loziska v bode [A] v axialnim smeru:')
RAz=x(3,1); %Zatizeni loziska v bode [A] v axialnim smeru
RBx=x(4,1);
%disp('Zatizeni lozisek v bode [B] v axialnim smeru:')
RBy=x(5,1); % Zatizeni lozisek v bode [B] v axialnim smeru
RBz=x(6,1);
MAx=x(7,1);
MAy=x(8,1);
MAz=x(9,1);
MBx=x(10,1);
MBy=x(11,1);
MBz=x(12,1);

%disp('Vyslednice reakci na loziska v bode [B] v radialnim smeru:')
RBr=sqrt((RBx^2)+(RBz^2)); %Vyslednice reakci na loziska v bode [B] v radialnim smeru
%disp('Vyslednice reakci na lozisko v bode [A] v radialnim smeru:')
RAr=sqrt((RAx^2)+(RAy^2)); %Vyslednice reakci na lozisko v bode [A] v radialnim smeru


RAZ(i,j)=RAz;
RBY(i,j)=RBy;
RBR(i,j)=RBr;
RAR(i,j)=RAr;
    end
end


%% Maximalni hodnoty
MAX_RAZ=max(max(abs(RAZ(:,:))));
Poloha_max_RAZ=abs(RAZ)==MAX_RAZ;
figure(101)
plot3(X,Y,Poloha_max_RAZ','ro')
title('Maximum RAz')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Poloha MAXIMA')

MAX_RBY=max(max(abs(RBY(:,:))));
Poloha_max_RBY=abs(RBY)==MAX_RBY;
figure(102)
plot3(X,Y,Poloha_max_RBY','ro')
title('Maximum RBy')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Poloha MAXIMA')

MAX_RBR=max(max(abs(RBR(:,:))));
Poloha_max_RBR=abs(RBR)==MAX_RBR;
figure(103)
plot3(X,Y,Poloha_max_RBR','ro')
title('Maximum RBr')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Poloha MAXIMA')

MAX_RAR=max(max(abs(RAR(:,:))));
Poloha_max_RAR=abs(RAR)==MAX_RAR;
figure(104)
plot3(X,Y,Poloha_max_RAR','ro')
title('Maximum RAr')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Poloha MAXIMA')



%% Graf 1 - RAz
figure(1)
surf(X,Y,RAZ')
title('RAz')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Reakce [N]')

%% Graf 2 - RBy
figure(2)
surf(X,Y,RBY')
title('RBy')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Reakce [N]')

%% Graf 3 - RBr
figure(3)
surf(X,Y,RBR')
title('RBr')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Reakce [N]')

%% Graf 4 - RAr
figure(4)
surf(X,Y,RAR')
title('RAr')
xlabel('\alpha [°]')
ylabel('\beta [°]')
zlabel('Reakce [N]')

disp('Maximalni hodnota zatizeni na lozisko v bode [A] v radialnim smeru:')
disp(MAX_RAR);
disp('Maximalni hodnota zatizeni na loziska v bode [B] v radialnim smeru:')
disp(MAX_RBR);
disp('Maximalni hodnota zatizeni loziska v bode [A] v axialnim smeru:')
disp(MAX_RAZ);
disp('Maximalni hodnota zatizeni lozisek v bode [B] v axialnim smeru:')
disp(MAX_RBY);







