clear all; close all; clc;

% :::: Folded leaf spring computations ::::
% Created by:   Maarten Dekker
% Date:         2013-04-03
% Version:      #01

% Parameters
L1  = 20e-3;           % [m]
L2  = 40e-3;           % [m]
b  = 20e-3;            % [m]
t  = 0.5e-3;           % [m]
E  = 210e9;            % [N/m^2]
I  = 1/12*b*t^3;       % [m^4]

% Applied Force
Fx = 50;
Fy = 5;

%% Computations
C_av    = 15*E*I/2/L1^3
C_weak  = C_av*2/5
C_stiff = C_av*8/5
F       = sqrt(Fx^2+Fy^2);
 
% Mechanics
steps = 1e4; % datapoints per length
x     = 0:L2/steps:L2;
y     = 0:L1/steps:L1;
ph    = Fx.*L1.^2./2./E./I - Fy.*L1.*L2./E./I;
dx_y  = -Fx.*y.^3./6./E./I + (Fx.*L1-Fy.*L2).*y.^2./2./E./I;
dy_x  = -Fy.*x.^3./6./E./I + Fy.*L2.*x.^2./2./E./I - ph.*x;
t1    = [0,diff(dx_y)];
t2    = [t1(end),diff(dy_x)];
s     = sqrt(dx_y(end).^2+dy_x(end)^2);
th    = atan(Fy/Fx);

M1 = Fx.*L1-Fy.*L2-Fx.*y;
M2 = Fy.*L2-Fy.*x;


% subplot (2,2,[1 2]);
% initial shape; 
axis equal; hold on;
plot([0 0 L2]*1e3,[0 L1 L1]*1e3,'k:');
% force arrow:
plot([L2 L2+2*s*cos(th)]*1e3,[L1 L1+2*s*sin(th)]*1e3);

% deformed shape 1
plot((dx_y(end)+x)*1e3,(L1+dy_x)*1e3,'r','LineWidth',2);

% displacement:
plot([L2 L2+dx_y(end)]*1e3,[L1 L1+dy_x(end)]*1e3,'k')

% arrow parts
plot([L2+2*s*cos(th) L2+2*s*cos(th)-s/2*cos(th-pi/9) L2+2*s*cos(th)-s/2*cos(th+pi/9) L2+2*s*cos(th)]*1e3,[L1+2*s*sin(th) L1+2*s*sin(th)-s/2*sin(th-pi/9) L1+2*s*sin(th)-s/2*sin(th+pi/9) L1+2*s*sin(th)]*1e3)
% deformed shape:

plot(dx_y*1e3,y*1e3,'r','LineWidth',2); 

title('Displacement of folded plate with free end as a result of applied force')
xlabel('x [mm]'); ylabel('y [mm]')
legend(['Initial shape: L_1 = ',num2str(L1*1e3),'mm, L_2 = ',num2str(L2*1e3),'mm'],['Force = ',num2str(round(F*100)/100),' N (In direction of the arrow)'],'Deformed shape',['Displacement = ',num2str(round(1e3*s*100)/100),' mm'],4)

% subplot 223;
% plot(y,abs(M1));
% xlabel('y [m]'); ylabel('M [Nm]');title('Moment-line of vertical beam')
% 
% subplot 224;
% plot(x,abs(M2));
% xlabel('x [m]'); ylabel('M [Nm]');title('Moment-line of horizontal beam')
