Use Matlab to plot x(t) for damped system

In summary, the author attempted to solve a damped system, but ran into an error. He consulted with his professor, who explained that his exp and sin portions of x were 1x63 and 1x63 matrices, which didn't work out. He corrected the code and produced the graphs he was looking for.
  • #1
Northbysouth
249
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 
  • #5
.

I would recommend checking the dimensions of your variables and making sure they are compatible for the operations you are trying to perform. In this case, it seems like your t variable is a 1x63 matrix while your x variable is a 63x1 matrix, so they cannot be multiplied together.

I would also suggest using the "hold on" function in Matlab to plot multiple curves on the same graph, so you can easily compare the responses for different values of z.

Here is a possible solution for your problem:

%Define constants
w_n=2; %rad/s
x_0=1; %mm
v_0=0; %mm/s

%Define time interval
t=[0:0.1:2*3.41];

%Initialize x matrix with zeros
x=zeros(63,6);

%Loop through different values of z
for i=1:6
%Calculate damped natural frequency
w_d=w_n*sqrt(1-z(i)^2);
%Calculate x(t) for the current value of z
x(:,i)=x_0*exp(z(i)*w_n*t).*sin(w_d*t);
end

%Plot x(t) for different values of z
plot(t,x)
legend('z=0.01','z=0.2','z=0.6','z=0.1','z=0.4','z=0.8')
xlabel('Time (s)')
ylabel('Displacement (mm)')
title('Response of a Damped System for Different Values of z')
grid on
 

Related to Use Matlab to plot x(t) for damped system

1. What is a damped system?

A damped system is a physical system in which the amplitude of oscillations decreases over time due to the dissipation of energy. This can be caused by factors such as friction, air resistance, or electrical resistance.

2. How can I plot x(t) for a damped system using Matlab?

To plot x(t) for a damped system in Matlab, you will first need to define the equation of motion for the system. This will depend on the specific parameters and variables of your system. Once you have the equation, you can use the "plot" function in Matlab to generate a graph of x(t) over a specified time interval.

3. What is the purpose of plotting x(t) for a damped system?

Plotting x(t) for a damped system allows you to visualize the behavior of the system over time. It can help you understand how the amplitude of oscillations changes, and how the system responds to different damping factors or initial conditions.

4. How can I adjust the damping of a system in Matlab?

In Matlab, you can adjust the damping of a system by changing the parameters in the equation of motion. This can include the damping coefficient or the initial conditions of the system. By adjusting these parameters and plotting x(t) again, you can see how the damping affects the behavior of the system.

5. Can Matlab also calculate the natural frequency of a damped system?

Yes, Matlab has built-in functions that can calculate the natural frequency of a damped system. This can be useful in understanding the behavior of the system and predicting how it will respond to different damping factors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
844
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
905
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
970
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
1K
Back
Top