Use Matlab to plot x(t) for damped system

AI Thread Summary
The discussion focuses on plotting the response of a damped system in MATLAB with varying damping ratios. The user initially encounters matrix dimension errors when attempting to compute the system's response using standard multiplication instead of element-wise multiplication. After consulting with a professor, they learn to use the ".*" operator for element-wise operations, which resolves the errors. The final code successfully plots the damped response for different damping ratios by adjusting the damping ratio variable. Understanding MATLAB's matrix operations is crucial for effectively implementing mathematical models in programming.
Northbysouth
Messages
241
Reaction score
2

Homework Statement


Plot x(t) for a damped system of natural frequency w_n= 2 rad/s and initial conditions x_0= 1 mm and v_0 = o mm/s, for the following values of the damping ratio: z= 0.01, 0.2, 0.6, 0.1, 0.4 and 0.8

Homework Equations





The Attempt at a Solution



I began by defining the values of my constants. I realize that z is not a constant per se but I'm not particularly knowledgeable about matlab

w_n=2; %rad/s
x_0=1; %mm
v_0=0; %mm/s
z= 0.01; %this value varies, thereby changing the response of the plot

Next I defined the time interval as well as some intermediate variables and the solution to a damped system.

t=[0:0.1:2*3.41];
w_d=w_n*sqrt(1-z^2);
x=x_0*exp(z*w_n*t_i)*sin(w_d*t_i);

With the above code I'm told:

Error using *
Inner matrix dimensions must agree.

I spoke to my professor about this and he explained that my exp and sin portions of x were 1x63 and 1x63 matrices, which doesn't work out. I've got the following lines of code which he gave me, but I'm still confused.

x=zeros(63,1);
for i=1:63
x(i)=x_0*exp(z*w_n*t(i))*sin(w_d*t(i));
end
plot(t,x)

Trying to use this code as it is just repeats:

Error using *
Inner matrix dimensions must agree.

Any help would be greatly appreciated
 
Physics news on Phys.org
Error using *
Inner matrix dimensions must agree.

Matlab does everything as vectors - this allows for some shortcuts but also means you have to be mindful of the operations.

The "*" is a matrix inner-product operation.
I don't think that's what you want.

I suspect you need the ith term in the result to be the ith term in one vector multiplied by the ith term in another vector.
For that you use the ".*" (dot-star) operation instead.

i.e.
t=1:0.125:6;
v=sin(t);
u=6-t;

t,u, and v are 1x49 vectors.

If I now do y=u*v I get an error because the dimensions are wrong for the dot product.
If I do y=u*v' (making the dimensions right) I get 50.176 ... because that's the dot-product.
But that's not what I want: I want the amplitude of the sine function to decrease with t.

This means that I need ##y=(y_1,y_2,\cdots,y_{49}): y_i=u_iv_i##

To get that I have to do: y=u.*v.
 
Last edited:
Thank you so much. I'll admit I still don't fully understand. My final code was

w_n=2; %rad/s
x_0=1; %mm
v_0=0; %mm/s
z=0.8; %this value varies, thereby changing the response of the plot

t=[0:0.1:20];
w_d=w_n*sqrt(1-z^2);

x=x_0*exp(-z*w_n*t).*sin(w_d*t);

plot(t,x)

Which by changing z for each value produced the graphs I was looking for.
 
Well done.

It can take a while to wrap your head around how you do things in these math-script programs, but it's usually worth the effort. All the program is doing is storing your numbers in matrices - so that's what you have to get used to.

eg. You could have made z into a vector, then the output would be a matrix where each row is for a different z value. I've had to do this where I had about 50-100 such plots to do but you don't have anything like that kind of number so you may as well just do them one at a time like what you did ;)

Cheers.
 
Back
Top