4 Bar

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

FOUR-BAR MECHANISM

PROGRAMMING

prepared by :- Hassan Essam Hassan


Under supervision of :- Dr/Yasser Abdelrhman

FEBRUARY 27, 2024


% Prompting user for input
L1 = input('Enter the length of link 1 = ');
L2 = input('Enter the length of link 2 = ');
L3 = input('Enter the length of link 3 = ');
L4 = input('Enter the length of link 4 = ');

theta1 = input('Enter theta1 in degrees = ');


theta2 = input('Enter theta2 in degrees = ');

% Converting theta2 to radians


theta2 = deg2rad(theta2);

% Calculating coefficients A, B, C
A = (2 * L1 * L4) - (2 * L2 * L4 * cos(theta2));
B = -2 * L2 * L4 * sin(theta2);
C = (L1^2) + (L2^2) + (L4^2) - (L3^2) - (2 * L1 * L2 *
cos(theta2));

% Solving quadratic equation


a = C - A;
b = 2 * B;
c = A + C;

u1 = (-b + sqrt(b^2 - 4 * a * c)) / (2 * a);


u2 = (-b - sqrt(b^2 - 4 * a * c)) / (2 * a);

% Calculating theta4 in radians


theta4_1 = 2 * atan(u1);
theta4_2 = 2 * atan(u2);

% Converting theta4 to degrees


theta4_1_deg = rad2deg(theta4_1);
theta4_2_deg = rad2deg(theta4_2);

% Calculating theta3 for both theta4 solutions in radians


theta3_1 = asin((L4 * sin(theta4_1) - L2 * sin(theta2)) / L3);
theta3_2 = asin((L4 * sin(theta4_2) - L2 * sin(theta2)) / L3);

% Converting theta3 to degrees


theta3_1_deg = rad2deg(theta3_1);
theta3_2_deg = rad2deg(theta3_2);

% Displaying results
fprintf('Theta 4_1: %.2f degrees\n', theta4_1_deg);
fprintf('Theta 4_2: %.2f degrees\n', theta4_2_deg);
fprintf('Theta 3_1: %.2f degrees\n', theta3_1_deg);
fprintf('Theta 3_2: %.2f degrees\n', theta3_2_deg);

You might also like