clear all
close all
clc

C=0.5;  % m
W=700;   % N
K=2000;    % N/m
NP=361;
%% dados do exemplo
% C1=6*C;
% C2=3*C;
% C3=C;
% S0=3*C;
%% caso 1 - mola sem carga na horizontal; C1 = C2
% C1=3*C;
% C2=3*C;
% C3=C;
% S0=sqrt(C1^2+C2^2);
%% caso 2 - mola sem carga na horizontal; C1 diferente de C2
% C1=2*C;
% C2=3*C;
% C3=C;
% S0=sqrt(C1^2+C2^2);
%% caso 3 - mola sem carga na vertical; C1 diferente de C2
C1=2*C;
C2=3*C;
C3=C;
S0=C2-C1;

for i=1:NP
    A(i)=360*(i-1)/(NP-1)*pi/180;
    S(i)=sqrt((C1-C2*sin(A(i)))^2+(C2*cos(A(i)))^2);
    dSdA(i)=-C1*C2*cos(A(i))/sqrt(C1^2-2*C1*C2*sin(A(i))+C2^2);
    Mgrav(i)=(C2+C3)*W*cos(A(i));
    Mmola(i)=K*(S(i)-S0)*dSdA(i);
    M(i)=Mgrav(i)+Mmola(i);
    Fmola(i)=K*(S(i)-S0);
end
%%
figure(1)
%------------------------------------------------------------------------
subplot(2,2,[1 3])
plot(A*180/pi,Mgrav,'-r',A*180/pi,Mmola,'-b',A*180/pi,M,'-k','linewidth',1.5)
grid on
xlabel('A (^o)')                      
ylabel('M')  
xlim([0 360])
set(gca,'XTick',0:45:360)
title('Momento equilibrante')
legend('Gravidade','Mola','Total')
%------------------------------------------------------------------------
subplot(2,2,2)
plot(A*180/pi,S,A*180/pi,S0,'--r','linewidth',2)
grid on
xlabel('A (^o)') 
ylabel('S (m)')  
xlim([0 360])
ylim([0 inf])
set(gca,'XTick',0:45:360)
title('Comprimento da mola')
%------------------------------------------------------------------------
subplot(2,2,4)
plot(A*180/pi,Fmola,'linewidth',2)
grid on
xlabel('A (^o)') 
ylabel('F_{mola} (N)')  
xlim([0 360])
set(gca,'XTick',0:45:360)
title('Força na mola')
%%

% clear all
% close all
% clc
% 
% syms C1 C2 A
% 
% S=sqrt((C1-C2*sin(A))^2+(C2*cos(A))^2);
% Ks=simple(diff(S,A))
% pretty(Ks)