alex timon
alex timon

Reputation: 1

MATLAB error: “Matrix dimensions must agree” when not dealing with matrices

I am trying to find the displacement x(t) as shown but i keep on getting the error

Error using  + 
Matrix dimensions must agree. 

My code is as shown below and its for an over damped vibration system

for i = 1 : 100; 
   t(i)= i/40;
   x(i) = (C1*exp(-s+(((s^2)-1)^.5)*Wn*t)) + (C2*exp(-s-(((s^2)-1)^.5)*Wn*t));
end 

I looked up this problem earlier and I saw a similar problem where a (.) was missing in front of the operator and tried this on my code but still couldn't get it to work. I don't know if I placed them in the wrong places or not but I am still stuck.

Could anyone please show me where I went wrong?

Upvotes: 0

Views: 73

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

This is very likely because you use t inside the expression for x, and not t(i). (Assuming all other variables are scalars.

Try:

for ii = 1 : 100; 
   t(ii)= ii/40
   x(ii) = (C1*exp(-s+(((s^2)-1)^0.5)*Wn*t(ii)))+ (C2*exp(-s-(((s^2)-1)^0.5)*Wn*t(ii)))
end

A better solution would be to vectorize this:

t = (1:100)./40;
x = (C1.*exp(-s+(((s^2)-1)^.5)*Wn.*t))+ (C2.*exp(-s-(((s^2)-1)^.5)*Wn.*t))

Upvotes: 4

Related Questions