0% found this document useful (0 votes)
22 views

Introduction To Matlab (Published .Mat File)

This document provides an introduction and overview of key MATLAB functions and capabilities. It discusses: 1) Vectors and matrices, including how to initialize, perform operations on, and save/load data for them. 2) Relational and logical operators that can be used to compare values and write conditional logic. 3) Plotting functions in MATLAB, including how to customize plots, add legends/labels, and create multi-plot figures.

Uploaded by

Youness AITOUNY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Introduction To Matlab (Published .Mat File)

This document provides an introduction and overview of key MATLAB functions and capabilities. It discusses: 1) Vectors and matrices, including how to initialize, perform operations on, and save/load data for them. 2) Relational and logical operators that can be used to compare values and write conditional logic. 3) Plotting functions in MATLAB, including how to customize plots, add legends/labels, and create multi-plot figures.

Uploaded by

Youness AITOUNY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

Contents

Inroduction to MATLAB
Vectors
Matrices
Saving and loading data
Relational and logical operators
Plotting with MATLAB
Check the rest of the codes as a homework
Polynomials
Symbolic toolbox in MATLAB

Inroduction to MATLAB

% lines which start with '%' are comments

% the characters in a line after '%' are not executed

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Error: File: E:\My Files\Courses\MEC709 - Fall 2010\Lab 1\Tutorial.m Line: 159 Column: 2
The expression to the left of the equals sign is not a valid target for an assignment.

Vectors

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2+3 % summation

12/3 % divison

[1,2,3,4] % initizlization of vectors, i.e. arrays

[1 2 3 4]

a=[1 2 3 4] % assign values to an array 1 x 4

b=[1;2;3;4] % assign values to an array 4 x 1

% semicolon on the end of the line suppreses the output


b=[1;2;3;4]

length(b) % length of an array

max(b) % maximum element

min(b) % minimum

mean(b) % average value of an array

c=[3 4 65 2 8 1]

sort(c) % sort an array

help sort % help command

sort(c, 'descend') % sort in descending order

sqrt(23) % square root

d=5^3 % raise to the power of 3

pi

1 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

sqrt(a) % sqare root of an array

exp(a) % exponential

2*4 % multiplication

a*b % multiplication of arrays -> product of vectors a(1x4) * b(4x1)= (1x1)

b*a % b(4x1) * b(1x4)= (4x4)

s1=1:6 % define an array from 1 to 6 with step 1

s2=1:2:7 % define an array from 1 to 7 with step 2

s3=7:-1:2 % step -1

% Complex numbers
c1=2+3i

c2=3+5j

c3=2-3*i

isreal(c1) % check if the variable c1 is real

i1=real(c1) %real part

i2=imag(c1) %imaginary part

conj(c1) % conjugate of c1 -> i1-i2*i

abs(c1) %magnitude -> sqrt(i1^2+i2^2)

angle(c1) % phase angle atan(i2/i1)

home % bring the commanf line to the top of the command window

whos % lists all variables in the workspace

% Strings

t='ryerson' % define a tring variable with ' '

disp(t) % display the variable t

whos % notice that t is char variable -> character

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Matrices

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initializing matrices
A=[2 3 4;
2 4 5;
6 8 10]

B=[2,3,4;2,4,5;6,8,10]

C=[1 6 9; 2 5 1];

A*C % error: you can not multiply A(3x3) with C(3x2) matrix

2 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

D=C' % transpose of a matrix D(2x3)

A*D % note the sizes: A(3x3) and D(3x2)

A(2,3) % element in row 2 and column 3

A(:,1) % elements of the first column in A

A(1,:) % elements in the first row of A

A(1:2,2:3) % elements from rows 1 to 2 and from columns 2 to 3

A^2 % square of a matrix A*A

A.^2 % square of each element in the matrix

det(A) % determinant of a matrix

inv(A) % inverse of matrix

A*inv(A)

eye(5) %identity matrix

ones(6,3) % matrix of ones

zeros(2,4) % matrix of zeros

Saving and loading data

save mydata A D B % save the variables C, D and B in the file mydata

clear all % clear the variables from the workspace

load mydata % load the variables from the file mydata

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Relational and logical operators

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

3>1 % returns 1 if true,

2==3

2=3 % error: '=' is used to assign values to a variable

a2=3

A>4

A==3

max(A) % maximum of each column in A

max(max(A))

size(A) %size of a matrix

%for loops
for i=1:5
v(i)=i^2
end

3 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

% if loops
% swaps the values of x and y if x>y
x=5,y=4
if x>y
temp=x;
x=y
y=temp
end

% nested loops
for i=1:3
for j=1:3
if sqrt(A(i,j))>=2 & A(i,j)<5
D(i,j)=A(i,j);
else
D(i,j)=sqrt(A(i,j));
end
end
end
D

%while (egzample: find the smallest number supproted by the PC)


v=1;
while v>0
vmin=v;
v=v/2;
end
vmin

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Plotting with MATLAB

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tt=[1 2.4 5 6.6 8] % define a time sequence


yy=[2 4 7 9 10] % define the values of the function
plot(yy,'x') % plots the array with 'x' markers
plot(tt,yy,'o') % plots the array with 'o' markers at times tt

% plots sin(x) with a dashed line and width 6


x=0:0.1:10
y=sin(x)
plot(x,y,'--','Linewidth',6)

plot(x,y,'yo') % plots with yelow color and 'o' markers

plot(x,y,'rx','Markersize',10) % red color and 'x' markers

plot(x,y,x,2*y) % plots y=sin(x) and 2*y=2*sin(x)


legend('old','new') % legend

% define axis limits, title and axis labels


plot(x,y)
axis([-2 20 -2 2]) % xlim([xmin xmax]), ylim([ymin ymax])
title('Plot of sin(x)','Fontsize',16)
xlabel('input')
ylabel('sin(x)')
grid

hold on, plot(x,x.^-2,'g--'),hold off; % adds another plot in the same figure

% create several plots in the same figure


subplot(2,2,1),plot(x,y)
subplot(2,2,2),plot(x,x.^3)
subplot(2,2,3),plot(x+4),gtext('display it here')

4 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

subplot(2,2,4),plot(x,exp(x))

% bar plots
x1=0:0.3:2
bar(x1,sin(x1))

% for three dimensional plots use plot3


x=0:0.3:20
y=sin(x);
z=cos(x);
plot3(x,y,z)
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Check the rest of the codes as a homework

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Polynomials

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

P1=[2 4 8 10] % assign 2s^3+4s^2+8s+10

P2=[1 0 0 1 0] % assign s^4+s

P1+P2 % error: P1 and P2 should have the same number of elements

P1=[0 2 4 8 10] % we added a zero to P1 to match the size of P2


P1+P2

P3=poly([-2 3]) % define a polunomial with zeros 2 and -3 -> (s+2)(s-3)=s^2-s-6

P4=conv([1 2],[1 -3]) % multiplication of polynomials -> (s+2) * (s-3)

roots(P4) % roots (or zeros) of a polinomial

polyval(P4,3) % evaluate a polynomial -> 3^2-1*3-6

polyval(P4,25) % evaluate -> 25^2-1*25-6

% division of polynomials
[Q,R]=deconv(P1,P3) % Q-quotient, R-remainder P1=P3*Q+R

% partial fraction expansion


% expand (s+4)/(s^2+5s+6)
[r,p,k]=residue([1 4],[1 5 6])

% transfer function in polynomial form


sys1=tf([1 5],[1 2 7])

% transfer function with zeros, poles, gain


sys2=zpk([1 0],[1 3 0 6],4)
% convert to polynomial form
tf(sys2)

% alternative way
s=tf('s')
sys3=4*(s-1)/(s^4-6*s^2+5)
zpk(sys3)

5 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

% solving sistems of linear equations


% for example solve the following system Ax=b:
% x1+x2+x3=1
% 2x1+3x2=4
% x1+x2-x3=2
A=[1 1 1;
2 3 0;
1 1 -1]
b=[1; 4; 2]
inv(A)*b
% or
A\b

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Symbolic toolbox in MATLAB

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all % clear the variables in the workspace

% define symbolic objects


syms a b c t x;

% solve the equation 2*x+3=0


solve(2*a+3)

solve(b^2-4*b+19) % another example

% find the solutions of a*x^2+b*x+c for a=3, b=2 and c=5


y=solve(a*x^2+b*x+c)
subs(y,{a,b,c},{3,2,5})

% differentiation -> find the derivative d/dx(x^3)


diff(x^3)

diff(x^3,3) % derivative of d/dx(x^3) for x=3

diff(sin(x)+(x^2/4)-tan(x/3))

% integration
int(x) % indefinite integral

% definite integral
int('x+2',-2,6) % integral of (x+2)dx from -2 to 6

% solving differential equations


dsolve('Dy=t*y') % example dy\dt=t*y

dsolve('Dy=t*y','y(0)=0.01') % solve dy\dt=t*y with initial condition y=0.01

% laplace transformation
syms t % define a symbolic variable
f4=exp(-2*t) % define a function e^(-2t)
F4=laplace(f4) % find the solution
pretty(F4) % display the solution in a 'pretty' way (fractions)
ezplot(f4) % plot the function f4

% another example
f5=exp(-t)-2*t*exp(-2*t)-2*exp(-2*t)
F5=laplace(f5)
pretty(F5)

ilaplace(F5) % find the inverse Laplase transform


pretty(ans)

6 of 7 6/12/2010 12:59 PM
Tutorial file:///E:/My%20Files/Courses/MEC709%20-%20Fall%202010/Lab%2...

Published with MATLAB® 7.7

7 of 7 6/12/2010 12:59 PM

You might also like