How to do vector loop equation?

21 views (last 30 days)
Mhadi Alessa
Mhadi Alessa on 14 Oct 2019
Edited: Karim on 26 Oct 2022
'input';
for R1=8
R2=5;
R3=7;
R4=6;
end
for angle_2=25
omega_2=24;
alpha_2=-4;
end
'syms, angle_3, angle_4';
[angle_3]='solve(R2*cos(angle2)+R3*cos(angle3)-R4*cos(angle4)-R1=0';
[angle_4] = 'solve(R2*sin(angle2)+R3*sin(angle3)-R4*sin(angle4)=0';
[omega_3] = 'solve(-R2*(omega2)*sin(angle2)-R3*(omega3)*sin(angle3)+R4*(omega4)*sin(angle4)=0';
[omega_4]= 'solve(R2*(omega2)*cos(angle2)+R3*(omega3)*cos(angle3)-R4*(omega4)*cos(angle4)=0';
[alpha_3] = 'solve(-R2*(alpha2*sin(angle2)+(omega2)^2*cos(angle2))-R3*(alpha3*sin(angle3)+(omega3)^2*cos(angle3))+R4*(alpha4*sin(angle4)+(omega4)^2*cos(angle4))=0';
[alpha_4] = 'solve(R2*(alpha2*cos(angle2)-(omega2)^2*sin(angle2))+R3*(alpha3*cos(angle3)-(omega3)^2*sin(angle3))-R4*(alpha4*cos(angle4)-(omega4)^2*sin(angle4))=0';
angle_R3=angle3;
angle_R4=angle4;
R3_angular_velocity=omega_3;
R4_angular_velocity=omega_4;
R3_angular_acceleation=alpha_3;
R4_angular_acceleation=alpha_4;
  4 Comments
Jan
Jan on 26 Oct 2022
@Mhadi Alessa: The code looks very strange:
'input'; % Why? Do you just want to display a message? Then:
disp('input')
for R1=8 % What is the purpose to run a loop over one element?
% easier without FOR:
R1 = 8;
R2=5;
R3=7;
R4=6;
%for angle_2=25 see above
angle_2=25;
omega_2=24;
alpha_2=-4;
% end
% See above: 'syms, angle_3, angle_4';
% Why do you use square brackets here?
[angle_3]='solve(R2*cos(angle2)+R3*cos(angle3)-R4*cos(angle4)-R1=0';
% ^ ^ ?
% Setting angle_3 to a char vector looks strange also.
% Do you mean:
angle_3 = solve('R2*cos(angle2)+R3*cos(angle3)-R4*cos(angle4)-R1 = 0');
Currently the complete code does neither look like meaningful Matlab code nor does it compute anything.

Sign in to comment.

Answers (1)

Karim
Karim on 26 Oct 2022
Edited: Karim on 26 Oct 2022
The syntax for the solve function works a bit different, see below for a demonstration.
Notice that if you have a set of two equations with two unkowns, you can pass both of them directly into the solve function.
R1 = 8;
R2 = 5;
R3 = 7;
R4 = 6;
angle_2 = 25;
omega_2 = 24;
alpha_2 = -4;
syms angle_3 angle_4 omega_3 omega_4 alpha_3 alpha_4
% set up equations
Angle_eqs = [R2*cos(angle_2)+R3*cos(angle_3)-R4*cos(angle_4)-R1 == 0;
R2*sin(angle_2)+R3*sin(angle_3)-R4*sin(angle_4) == 0];
Omega_eqs = [-R2*(omega_2)*sin(angle_2)-R3*(omega_3)*sin(angle_3)+R4*(omega_4)*sin(angle_4) == 0;
R2*(omega_2)*cos(angle_2)+R3*(omega_3)*cos(angle_3)-R4*(omega_4)*cos(angle_4) == 0];
Alpha_Eqs = [-R2*(angle_2*sin(angle_2)+(omega_2)^2*cos(angle_2))-R3*(alpha_3*sin(angle_3)+(omega_3)^2*cos(angle_3))+R4*(alpha_4*sin(angle_4)+(omega_4)^2*cos(angle_4)) == 0;
R2*(angle_2*cos(angle_2)-(omega_2)^2*sin(angle_2))+R3*(alpha_3*cos(angle_3)-(omega_3)^2*sin(angle_3))-R4*(alpha_4*cos(angle_4)-(omega_4)^2*sin(angle_4)) == 0];
% solve the equations
Angle = solve(Angle_eqs,[angle_3 angle_4]);
angle_3 = eval(Angle.angle_3);
angle_4 = eval(Angle.angle_4);
% this provides two possible solutions. Below i simply pick the first slution,
% However you need to check if this is the one you want
angle_3 = angle_3(1);
angle_4 = angle_4(1);
Omega = solve(Omega_eqs,[omega_3 omega_4]);
omega_3 = eval(Omega.omega_3);
omega_4 = eval(Omega.omega_4);
Alpha = solve(Alpha_Eqs,[alpha_3 alpha_4]);
alpha_3 = eval(Alpha.alpha_3);
alpha_4 = eval(Alpha.alpha_4);
% print results to the display
fprintf("Angle 3 = %.3f\nAngle 4 = %.3f\n",angle_3,angle_4)
Angle 3 = -0.809 Angle 4 = -1.268
fprintf("Omega 3 = %.3f\nOmega 4 = %.3f\n",omega_3,omega_4)
Omega 3 = -35.070 Omega 4 = -28.245
fprintf("Alpha 3 = %.3f\nAlpha 4 = %.3f\n",alpha_3,alpha_4)
Alpha 3 = -1372.136 Alpha 4 = -2497.489
EDIT: i believe this resembles a four bar mechanism, if so we can plot the configuration in the following way:
x = [0;
R2*cos(angle_2);
R2*cos(angle_2)+R3*cos(angle_3)
R2*cos(angle_2)+R3*cos(angle_3)-R4*cos(angle_4)];
y = [0;
R2*sin(angle_2)
R2*sin(angle_2)+R3*sin(angle_3)
R2*sin(angle_2)+R3*sin(angle_3)-R4*sin(angle_4)];
figure
plot(x,y,'LineWidth',1.5,'Marker','o','MarkerFaceColor','red')
grid on

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by