MATLAB: Hello, I keep getting a “Matrix dimensions must agree.” error message.I’m trying to create a graph that includes all the curves for various damping factors.

matrix dimensions must agree error message

Hello, I keep getting a "Matrix dimensions must agree." error message.I'm trying to create a graph that includes all the curves for various damping factors.
function [df, wd, wn, xMax] = vibrations() % Vibrations characteristic equation solver % All units are SI! % Use: [df,wd,wn,xMax] = vibrations(); % df=damping factor (also known as zeta) % wd=damping frequency, wn=natural frequency % xMax=Maximum displacement
clc; clear all; close all help vibrations m=input('Input mass: '); if m<=0 disp('Please specify a mass greater than 0.'); [df,wd,wn, xMax]=vibrations(); else c=input('Input dampening coefficient: '); k=input('Input spring constant: '); v0=input('Input initial velocity v(0): '); x0=input('Input initial position x(0): ');
s=[m c k];
fprintf('The Characteristic eq is %gs^2+%gs+%g=0 \n',m,c,k)
[R]=roots(s);
r1=R(1); r2=R(2);
df0=0.1;
df1=1.0;
dfi=0.1;
df=df0:dfi:df1;
%This sets up zeta for the plot
% fprintf('Roots of the Characteristic eq are=%.2f and %.2f\n',r1,r2); disp('Roots of the Characteristic eq are=') dispĀ® %wn/wd calculations wn=sqrt(k/m); fprintf('Natural frequency=%0.2f\n',wn); wd=wn*sqrt(1-df.^2); disp('Damping frequency=') disp(wd) tau=m/c; fprintf('Time Constant=%0.2f\n',tau); LD=(2*pi*df)/sqrt(1-df.^2); fprintf('Logarithmic Decrement=%0.2f\n',LD); r=real(r1); q=imag(r1); b=(1/q)*sqrt((q*x0)^2+(v0-r*x0)^2); p1=asin(x0/b); p2=acos((v0-r*x0)/(q*b)); %atan2 to use the correct quadrant ta=atan2((q*x0),(v0-r*x0)); fprintf('Equation below\n'); fprintf('x=%.2fe^(%0.2ft)*sin(%0.2ft+%0.2f)\n',b,r,q,ta); end %Checking the stability: if r1<0 && r2<0 fprintf('Motion is stable\n'); else fprintf('Motion is unstable\n'); end F0=input('Magnitude of F0: '); wf=input('Working frequency: '); r0=0; rf=input('Input final ratio for position over plot: '); ri=0.01; rw=r0:ri:rf; phi=atan((2*df*rf)/((rf^2)-1)); disp('Angle phi ='); disp(phi); Xss=F0/sqrt((k-m*wf*wf)^2+(c*wf)^2); M=Xss/F0; disp('Magnitude ratio M is ='); disp(M); rw2=rw.*rw; MD1=sqrt((1.-rw2).^2+(2.*df.*rw).^2); MD2=1./MD1; xw=F0*MD2/k; wp=wn*sqrt(1-2.*df.*df); disp('Peak frequency is ='); disp(wp); xp=F0/(k*2.*df*sqrt(1-df.*df)); disp('Peak response is ='); disp(xp);
%plot graph with amplitude limit lines if Underdamped.
if df<1
plot(rw,MD2,'',rw,xw,'m:',rw,-xw,'m:'); hold on
else
plot(rw,MD2); hold on
end
xlabel('r or w/wn')
ylabel('Dimensionless Magnitude Ratio')
grid on
%Maximum displacement
if c>=0
xMax=max(abs(xw));
fprintf('Maximum displacement = %.2f\n',xMax);
else
xMax=inf;
fprintf('System unstable: Maximum displacement = infinite\n');
end

Best Answer

First off, please post all your code in the {code} notation. This was a pain to unwrap :(
Your code on line 69,
MD1=sqrt((1.-rw2).^2+(2.*df.*rw).^2);
has rw, rw2, and df, which are hard-coded as 1001, 1001, and 10 element arrays respectively. You're trying to implement vector multiplication via your df.*rw, which necessitates vectors of the same length. I don't understand you application, but you'll need these vectors to be the same length (all 1001 or all 10 elements) to allow multiplication.