# Matlab code wont run. Matrix dimensions must agree error

1 view (last 30 days)
Liam Wall on 24 Nov 2021
Answered: Kevin Holly on 24 Nov 2021
Matrix dimensions must agree.
Error in ME4112_Ex3 (line 27)
v_t = l * omega.*cos(theta);
MATLAB CODE
%Arm Length
l=409;
%Initial Angle
beta = ((18)*(pi/180));
%Time period
T=0.1;
t=0:0.01:1;
theta = (pi/2) + beta*sin((2*t - T)*pi/(2*T));
%Diplacement of Plunger
H_t = l*(sin(theta) - cos(beta));
%Maximum Plunger Displacement
h_max = l*(1 - cos(beta));
%Velocity of Plunger
v_t = l * omega.*cos(theta);
%Acceleration of Plunger
a_t = l*((alpha.*cos(theta)) - (omega.^2).*sin(theta));
%Maximum acceleraion of Plunger in mm/s^2
a_max = max(a_t);
%Maximum acceleraion of Plunger in m/s^2
A_max = (a_max*10^-3);
%Angular Velocity of arm AB
omega = (pi*beta/T)*cos((2*t - T)*pi/(2*T));
%Angular Acceleration of arm AB
alpha = -(pi*pi/(T*T))*beta*sin((2*t -T)*pi/(2*T));
fprintf('Maximum displacement of plunger is %4.2f.\n' ,h_max);
fprintf('Maximum acceleration of plunger is %4.2f.\n' ,A_max);
% Reequired Graphs
% Graph 1: Time(s) vs. Angular Velocity (rads/s)
figure(1)
plot(t,omega)
xlabel('Time (s)')
% Graph 2: Time(s) vs. Displacement (mm)
figure(2)
plot(t,H_t)
xlabel('Time (s)')
ylabel('Displacement (mm)')
% Graph 3: Time(s) vs. Velocity (m/s)
figure(3)
plot(t,v_t)
xlabel('Time (s)')
ylabel('Velocity (m/s)')
% Graph 4: Time(s) vs. Acceleration (m/s^2)
figure(4)
plot(t,a_t)
xlabel('Time (s)')
ylabel('Acceleration (m/s^2)')

Kevin Holly on 24 Nov 2021
I did not come across that issue when I defined omega as omega = (pi*beta/T)*cos((2*t - T)*pi/(2*T)); before running that line. You may have a different omega defined in your workspace.
%Arm Length
l=409;
%Initial Angle
beta = ((18)*(pi/180));
%Time period
T=0.1;
t=0:0.01:1;
theta = (pi/2) + beta*sin((2*t - T)*pi/(2*T));
%Angular Velocity of arm AB
omega = (pi*beta/T)*cos((2*t - T)*pi/(2*T));
%Diplacement of Plunger
H_t = l*(sin(theta) - cos(beta));
%Maximum Plunger Displacement
h_max = l*(1 - cos(beta));
%Velocity of Plunger
v_t = l * omega.*cos(theta)
v_t = 1×101
0.0000 367.1813 596.5728 599.6230 372.1168 0.0000 -372.1168 -599.6230 -596.5728 -367.1813 -0.0000 367.1813 596.5728 599.6230 372.1168 -0.0000 -372.1168 -599.6230 -596.5728 -367.1813 -0.0000 367.1813 596.5728 599.6230 372.1168 0.0000 -372.1168 -599.6230 -596.5728 -367.1813

R2018a

### Community Treasure Hunt

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

Start Hunting!