0% found this document useful (0 votes)
152 views3 pages

Using Matlab: Page 1 of 3 Spring Semester 2012

Matlab is software designed to help engineers carry out calculations with minimum programming. It consists of the Workspace, where commands are typed to define and manipulate variables; Scripts, which are files containing lists of commands; Functions, which are like scripts but do not place variables in the workspace; and Figures, which display plots in separate windows. The document provides examples of basic Matlab syntax and commands for arithmetic operations, plotting functions, and manipulating vectors and matrices. It also describes how the provided Matlab functions can be used to analyze the free and forced vibration response of a single-degree-of-freedom mechanical system.

Uploaded by

Alexand Meliala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views3 pages

Using Matlab: Page 1 of 3 Spring Semester 2012

Matlab is software designed to help engineers carry out calculations with minimum programming. It consists of the Workspace, where commands are typed to define and manipulate variables; Scripts, which are files containing lists of commands; Functions, which are like scripts but do not place variables in the workspace; and Figures, which display plots in separate windows. The document provides examples of basic Matlab syntax and commands for arithmetic operations, plotting functions, and manipulating vectors and matrices. It also describes how the provided Matlab functions can be used to analyze the free and forced vibration response of a single-degree-of-freedom mechanical system.

Uploaded by

Alexand Meliala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Using Matlab

Matlab is software designed to help engineers carry out calculations with minimum programming effort. The
Matlab environment typically consists of the Workspace, Scripts, Functions and Figures.

Workspace: This is the main Matlab window that appears when you start the software. Within the workspace,
you can type various commands. Every Matlab action can be specified by a command in the Workspace
although there are sometimes menu-driven simplifications.
As an example, a command to generate a row vector called “x” containing the numbers 1, 2, 3, and 4 would be
x = [1 2 3 4];
Note that the semicolon only stops Matlab echoing the answer back to you. The vector “x” is now a variable in
the workspace and will stay there until you exit the program, clear it or overwrite it. Some basic Matlab syntax
is described below.
whos list variables in the workspace (whos a* lists all variable beginning with “a”)
clear clear all variables from the workspace (clear a* clears only those starting with “a”)
clc clear writing off the workspace screen (nothing is lost from memory)
save zzz save workspace variables in Matlab format to the current directory in a file called “zzz”
arithmetic the usual symbols for arithmetic functions apply. Use * to multiply, / to divide and ^
for “power of”.
abs(x) absolute value of x (i.e. magnitude of a complex number)
Matlab is designed to handle vectors, matrices and arrays. When performing arithmetical operations of these,
several conventions apply
 Matrices or vectors add and subtract element-by-element
 The multiply and divide symbols define matrix operations. To get element-by-element operations use
.* for multiplication, ./ for division and .^ for power.
 To identify elements within a matrix use the indices e.g. b = a(3,[2 4]) defines the variable b as a
row vector [a(3,2) a(3,4)]. In this context, the colon means “all”, therefore a(:,3) would be the
third column of matrix a.

T
To get the transpose B = A , type the following command B = A'

Scripts: For repetitive tasks, it is sometimes more convenient to put a list of commands together into a
program called a Script. This is a file in ASCII format with its name ending with .m.
To run the script, type the name of the file (without the ending) into the workspace: e.g. to run a script called
antelope.m type antelope in the workspace. Note that the script has to be in the Matlab current directory (or
in the workpath), for it to run.

Functions: For more complex tasks that may involve intermediate steps, it is usually undesirable to fill up the
workspace with many variables. A Function in Matlab is a Script that does not keep variables in the workspace.
Instead, it has defined inputs and outputs. You can call the function from the workspace in the same way as a
Script, however in order to get any output to the workspace, you will have to define its inputs and outputs. For
example the function “rand.m” is a random number generator. To run it, in the workspace you might type
a = rand(5,2). This will assign the variable “a” as a 5x2 matrix of random numbers.
With a function, you do not need to use the same variable names for input or output. The inputs can be
numeric values or variable names, the outputs can be any variable names you choose – but be careful as any
variables already in the workspace will be overwritten.
In addition to any functions you may create, Matlab has extensive libraries of functions. These include basic
graph-plotting commands such as plot, semilogy and loglog. Typing doc and the function name in the

Page 1 of 3 Spring Semester 2012


MEC201: Dynamics of Structures and Machines

workspace will give you information on how to use the function e.g. doc plot will explain how to use the plot
command. Information on any functions provided as part of this exercise will be accessible in the same way.

Figures: Figures in Matlab appear as separate windows. The figure-plotting routines in Matlab are quite
extensive. Modification can be made either by typing commands in the workspace or through the user-
interface on the figure window itself. Some useful commands are …
plot(x,y) plot vector (or matrix) y against vector (or matrix) x. Note that
plot(x1,y1,x2,y2) can be used to generate two lines (or more) on the
same graph.
It is also possible to change the way lines look. For example, to get a line that
adds dots at the data points use plot(x,y, '.- '). You can specify colur
in the same way – consult the Help section in Matlab typing doc plot in the
workspace.
semilogy(x,y) same as plot but with log-scale on the y-axis.
loglog(x,y) same as plot with log-scale on both axes.
xlabel('zzz') add the label “zzz” to the x-axis, same format for ylabel
legend('aa','bb') adds a legend to the graph –here the first line is called aa and the second bb
clf clear everything off the figure.
grid on include a grid (and grid off to remove).
axis([a b c d]) x-axis ranges from a to b and y axis ranges from c to d
Matlab plots figures with very thin lines. It is advisable to make them thicker for reports. Use the interactive
figure editor or try the command set(get(gca,'Children'),'LineWidth',2).
You can save Matlab figures for later use in Matlab. You can also copy Matlab figures into other applications
such as MS Word. From the Edit menu on the figure window, the selection “copy options” allow you to change
to the format you desire.

Matlab use in MEC201


You will gain the most understanding in MEC201 by using the provided Matlab functions to understand the
behaviour of SDOF systems.
The SDOF system studied in the lab has the following nominal properties:
mass m = 4.5 kg
stiffness k = 55,000 N/m
damping c = 10 Ns/m
A number of Matlab functions have been specially written for this module. You can access these on MOLE (in
the Coursework folder). Listed below are some exercises that will help you learn to use them.

a) Fourier Transform:
Create a sinusoidal signal in the workspace and plot it to check that it looks sensible: define 500 points
between 0 and 2 seconds, then create the signal x  sin(1t )  0.5 sin( 2 t ) where 1 = 2 Hz and 2 = 10 Hz
t = linspace(0,4,500)';
x = sin(2*pi*2*t) + 0.5*sin(2*pi*10*t);
Plot the signal and add a grid
plot(t,x), grid on
Look at the forward and inverse Fourier Transforms of the signals. The forward transform is,
[f,X] = ft_forward(t,x);

Page 2 of 3 Spring Semester 2012


MEC201: Dynamics of Structures and Machines

Zoom in on the peaks to check magnitude and frequency.


axis([0 15 0 1.5])
Carry out the inverse transform and check that the forward-backward transforms are consistent.
[t1,x1] = ft_inverse(f,X);
plot(t,x,t1,x1,'.')

b) Free vibration response


The free vibration response of a SDOF system is given by

 x   n x0 
x(t )  e  n t  x0 cos d t   0 sind t 
 d 
This is coded into the function sdof_free_time.m. First look at the function input/output format.
doc sdof_free_time
Define the elemental properties for the SDOF system.
m = 4.5;, k = 55e3;, c = 10;

Start by defining  n  k / m and   c / c c (where c c  2 km ) and calculate response using default initial
conditions.
wnhz = sqrt(k/m)/2/pi;, zeta = c/2/sqrt(k*m);
[t,x] = sdof_free_time(wnhz,zeta);
Consider the effect of different parameters on the response. First, alter the initial conditions and compare
response traces (real amplitude and logarithmic scaling). Use the axis command to zoom into specific
areas.
[t1,x1] = sdof_free_time(wnhz,zeta,0,100);
plot(t,x,t1,x1)
semilogy(t,abs(x),t1,abs(x1))
axis([0 1.2 1e-1 1])
Next alter the damping level, compare results and generate an appropriate figure.
[t2,x2] = sdof_free_time(wnhz,5*zeta);
plot(t,x,t2,x2)
xlabel('time'),ylabel('amplitude'),legend('\zeta=0.01','\zeta=0.05')
set(get(gca,'Children'),'LineWidth',2)
c) Steady state forced vibration response
The command format in this case is,
[whz,X] = sdof_ssforced_mkc(m,k,c);
To obtain the responses of two different conditions try out the following commands.
[whz1,X1] = sdof_ssforced_mkc(m,k,c);
[whz2,X2] = sdof_ssforced_mkc(m,k,c*5);
To get logarithmic scale on the y-axis and include appropriate legends use the following commands.
semilogy(whz1,abs(X1),whz2,abs(X2))
xlabel('Frequency, Hz')
ylabel('Amplitude, m')
legend('original','increased damping')
set(get(gca,'Children'),'LineWidth',2)

Page 3 of 3 Spring Semester 2012

You might also like