Use Matlab to plot x(t) for damped system

Click For Summary

Discussion Overview

The discussion revolves around plotting the response of a damped system using Matlab, specifically focusing on the natural frequency and varying damping ratios. Participants share their attempts to implement the code and troubleshoot errors related to matrix operations in Matlab.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant outlines their initial approach to defining constants and setting up the time interval for plotting the damped system response.
  • The participant encounters an error related to matrix dimensions when attempting to compute the response using the exp and sin functions.
  • Another participant explains the difference between matrix multiplication and element-wise multiplication in Matlab, suggesting the use of the ".*" operator for element-wise operations.
  • A later reply shares a revised code that successfully plots the damped response for different damping ratios, indicating that changing the damping ratio produces the desired graphs.
  • One participant reflects on the learning curve associated with using Matlab for mathematical computations and suggests that creating a vector for damping ratios could streamline the process for larger datasets.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper matrix operations in Matlab, but there is no consensus on the best approach for handling multiple damping ratios simultaneously.

Contextual Notes

Some limitations include the initial misunderstanding of matrix operations in Matlab and the specific coding practices that may vary based on the complexity of the task.

Who May Find This Useful

Students learning to use Matlab for engineering or physics applications, particularly those dealing with differential equations and system responses.

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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K