Sie sind auf Seite 1von 25

Matlab Tutorial

Introduction to MATLAB
Matrices and matrix arithmetic.
Control structures
Program development using M-files.
Matlab Graphics
Practical applications

Mechanical Engineering Department


Wichita State University

Introduction to MATLAB

Matlab means MATrix LABoratory program which was developed to solve linear equations
and eigenvalue problems. MATLAB offers a language for expressing problems and their
solutions both mathematically and visually. MATLAB is used in numeric computation,
algorithm development, data analysis, signal processing, engineering graphics and scientific
visualization.
Defining the variables
The general format for defining the variables is
x = 98
Valid
y_hc = 6 Table = 6576
Invalid
_y = 5
8sum = 767
2/4 = 0.5 -- right division
2\4 = 4/2 = 2 -- left division

Arithmetic Operations

Precedence of Arithmetic Expression


The Matlab will execute the expression according the following precedence.
( ) ----- 1
^ -------2 right to left
* / ----3 left to right
\ -------3 right to left
+ - ---4 left to right
For Example
c= 4*7*(4+7)/4-2
c = 75
(4+7) = 11 --------1
4*7*11 = 308 ----- 2 from left to right
308/4 = 77 ------- 3 from left to right
77 2 = 75 ------- 4 from left to right

Formatting Options and Mathematical Functions

Formatting options available in Matlab


format compact
x=3.6542465
x=
3.6542
format long
x=3.6542465
x=
3.65424650000000
format short e 3.6542e+1
format long e
3. 6542465e+1
Mathematical Functions
rem(8,3) = 2, return the remainder.
round(5.77) = 6, round toward nearest integer.
sqrt(9) = 3, sqrt(-9) = 0 + 3.0000i,
pi = 3.1416
sind(30) = 0.5

Matrices and Matrix Arithmetic

General format for Matrix


The general format for introducing the matrix is as follows. Semicolon is separate the each matrix rows.
b = [2 3 4 5; 4 5 6 7;4 5 7 5;4 8 2 0]
b=
2 3 4 5
4 5 6 7
4 5 7 5
4 8 2 0

Matrix Multiplication and Division


A=[3,-1,0;-1,6,-2;0,-2,10]
B=[2,3,4;2,3,1;2,5,8];
C = A*B
c=
4 6 11
6 5 -14
16 44 78

Matrix Arithmetic

Division
C=A\B
C=
0.8608
0.5823
0.3165

1.3165 1.5823
0.9494 0.7468
0.6899 0.9494

Cube of a Matrix
A^3
ans =
39
-68
38
-68
319
-402
38
-402
1104
Cube Root of a Matrix
A^1/3
ans =
1.0000 -0.3333
0
-0.3333 2.0000 -0.6667
0
-0.6667 3.3333

Solving Three Linear Matrix Equations

Solving Three Linear Matrix Equations


Let us consider the three equations and solve using Matlab
3x y + 0z =1 --------(1)
-x + 6y-2z = 5 --------(2)
0x -2y +10z = 26 -------(3)
>> A=[3,-1,0;-1,6,-2;0,-2,10]
A=
3 -1 0
-1 6 -2
0 -2 10
>> B = [1;5;26]
B=
1
5
26
>> XYZ = A\B
XYZ =
1.0000
2.0000
3.0000

Matrix Operations

Inverse matrix
To find inverse matrix for matrix(A)
>> inv(A)
ans =
0.3544 0.0633 0.0127
0.0633 0.1899 0.0380
0.0127 0.0380 0.1076
Transpose Matrix
A'
ans =
3 -1 0
-1 6 -2
0 -2 10
Determinant
>> b=[1 2 3;4 5 6;4 8 7];
>> c=det(b)
c=
15

Control Structures

Relational and Logical Operators


< less than
> greater than
<= less than or equal
>= greater than or equal
= = to check equality
~ = Not equal
^ NOT
& AND
! OR

If-else statement

If-else statement
a=3; b=3.7;
if a<b,
c=7;
else
c=5;
end;
c
c=
7

While Statement

While Statement
EDU i=0;
EDU while(i<5),
k=3*i,
i=i+1
end
k= 0
i= 1
k= 3
i= 2
k= 6
i= 3
k= 9
i= 4
k = 12

For Loop

For Loop
for i=2:2:10
i
end
i= 2
i= 4
i= 6
i= 8
i = 10

Matlab Graphics

Draw a plot of the following equation


Y(x) = sin(x) for 6<=x<= 6
x=-6:0.3:6;
y=sin(x);
plot(x,y)
grid
xlabel('x')
ylabel('sinx')
plot(x,y)
title('sin curve')
legend('y axis')
axis([-6 6 -.4 1.4]) ---- axis([xmin, xmax,
ymin, ymax])

Bar Chart

x=[1,2,3,5,6,8,10];
y=[22,33,44,37,56,38,45];
bar(x,y)

stem(x,y)

Three Dimensional Plots

data = [ 2.6,4,5.6;
4.5,6,7;
3.7,5.9,9.8;
4.6,6.9,7.8];
plot3(data(:,1),data(:,2),data(:,3))
grid
>> xlabel('x'),ylabel('y'),zlabel('z')

Working with M-file editor

Working with M-file editor


1.

2.
3.

4.

First step is to set the path of the working directory for the Matlab. For that, first
save the filename.m in a particular folder. Then set the path to that particular
folder.
First click on the set path menu on the command window
Then click on the browse menu and set the path towards to the folder in which
the m-file program has been saved.
Then click on path and add the folder path.

Functions in Matlab

Matlab program demonstrating the usage of function as a separate M-file


M- file saved as quadra.m
% Find the roots of Quadratic equations.
disp('enter the coefficient for the equation ax^2 + bx +c \n');
a=input('enter coeff a ');
b= input('enter coeff b');
c= input('enter coeff c');
discri = discriminant(a, b, c);
r1 = -b/(2*a) - sqrt(discri)/(2*a);
r2 = -b/(2*a) + sqrt(discri)/(2*a);
fprintf('The roots are %5.3f, %5.3f\n',r1,r2);
end
*****************************************************************
Function M-file saved as discriminant.m
% Finding the discriminant for the equation
function [dis] = discriminant(a, b, c)
dis = b*b - 4*a*c;

Arrays in Matlab

Arrays can be formed using zeros command.


This following program demonstrate the
array programming in Matlab
%temperature
tempC =zeros(2);
tempF = zeros(2);
tempC(1)= -50;
tempF(1) = 9*tempC(1)/5 +32;
tempC(2)= 100;
tempF(2)= 9*tempC(2)/5 + 32;
plot(tempC, tempF);
grid;
xlabel('Tem(celsius)');
ylabel('temp(fah)');
title('fah vs cel');

Free Vibration Response of Undamped SDOF system

The free vibration response of an undamped single


degree of freedom (SDOF) oscillator is given by
y(t) = y(0)cos(wt) + v(0)/wsin(wt)
% Free Vibration Responses of Undamped SDOF
System.
% array storage
rows = 501;
values = zeros(rows,3);
%declarations
m=10;
stiff =100;
w = sqrt(stiff/m);
dt = 0.02;
disp_0 = 10;
vel_0 = 10;
%calculating the displacement and velocity.
for i=1:rows
time = (i-1)*dt
values(i,1) = time;
values(i,2) = disp_0*cos(w*time)+
vel_0/w*sin(w*time);
values(i,3) = -disp_0*w*sin(w*time)+
vel_0/w*cos(w*time);
end

Free Vibration Response of Undamped SDOF system Conti

%plotting graph between displacement and velocity versus time.


plot(values(:,1), values(:,2));
hold;
plot(values(:,1), values(:,3));
grid;
xlabel('Time in Sec');
ylabel('Displacement and Velocity');
title('Free vibration responses');

Integration

Integration
x=0:3*pi;
y=sqrt(4*cos(2*x).^2 + sin(x).^2 + 1);
% Integrate y with respect to x
trapz(x,y)
ans =
16.6199

Double Integration

xmin = pi;
xmax = 2*pi;
ymin = 0;
ymax = pi;
result = dblquad(@integrnd,xmin,xmax,ymin,ymax)
Save as dou.m

function out = integrnd(x,y)


out = y*sin(x) + x*cos(y);
Save as integrnd

Differentiation

syms x y
y= sin(x); dy = diff(y,'x')
dy =
cos(x)
y= (sin(x))^2
diff(y)
y=
sin(x)^2
ans =
2*sin(x)*cos(x)

Second Derivative

syms x y
y= (sin(x))^2+cos(x)
diff(y,x,2)
y=
sin(x)^2+cos(x)
ans =
2*cos(x)^2-2*sin(x)^2-cos(x)

Partial Derivative of y with respect to x

syms x y z
y= (sin(z))^2+cos(x)
diff(y,'x')
y=
sin(z)^2+cos(x)
ans =
-sin(x)

References:
[1] Engineering Programming C, MATLAB, JAVA by Mark Austin and David Chancogne, John Wiley &
sons Inc, ISBN 0-471-00116-3
[2] Computer Applications Lab manual, Mechanical Engineering Dept