MATLAB: Inner matrix dimensions must agree

electrical engineeringinner matrix must agree

This is my first time using Matlab, so I apologize if this is a noob sort of question that gets asked a thousand times.
Here is my m file:
A1 = 4;
A2 = -2;
B1 = 3;
B2 = 1;
T1 = .1;
T2 = .02;
omega = 40 * pi;
t = linspace(0,5*T1);
overdamped = A1*exp(-t./T1)+A2*exp(-t./T2);
critdamped = A1*exp(-t./T1)+(A2/T1)*t.*exp(-t./T1);
underdamped = exp(-t./T1)*(B1*cos(omega*t.)+B2*sin(omega*t.));
plot (t,overdamped)
I am trying to eventually have m file graph overdamped, critdamped, and underdamped on the same plot. I was trying to just get the first one to graph, which I can if I comment out the "critdamped = " and "underdamped = " lines, but if those are in there it throws the error "inner matrix dimensions must agree."
I'm having trouble seeing any errors – to me it seems like all the variables I am working with in those equations are 1×1, where x and t are the only vectors with dimension greater than 1.

Best Answer

That is so excepting when you get to
underdamped = exp(-t./T1)*(B1*cos(omega*t.)+B2*sin(omega*t.));
you're trying to multiply the damping by the waveform all of which are vectors. You're missing the "dot" on the .* to do element-wise multiplication of those two. "+" works without a ".+" operator as the operation is element-wise anyway and there isn't a secondary definition as matrix multiply overloads the "*" operator so have to have two different ones.
underdamped = exp(-t./T1).*(B1*cos(omega*t)+B2*sin(omega*t));
I corrected the extraneous 'dot' after the t in the omega*t terms, too, btw...