Professional Documents
Culture Documents
Week 1 To 5
Week 1 To 5
Week 1 To 5
E-mail:
ahmadarafat2016@gmail.com
Tel:0797612096
MATLAB COURSE
Introduction
MATLAB, which stands for Matrix Laboratory, is a very powerful program for performing
numerical and symbolic calculations, and is widely used in science and engineering, as well as in
mathematics.
What is MATLAB ?
Following are some commonly used mathematical calculations where it is used most commonly:
Dealing with Matrices and Arrays
2-D and 3-D Plotting and graphics
Linear Algebra
Algebraic Equations
Non-linear Functions
Statistics
Data Analysis
Calculus and Differential Equations
Numerical Calculations
Integration
Transforms
Curve Fitting
Various other special functions.
Simulink®, with the ability to build models with up to 1000 blocks (the
professional version allows an unlimited number of blocks)
1
Basics of the Technical Language
2
Workspace:
Displays all the defined variables
Command Window:
To execute commands in the MATLAB environment
Command History:
Displays record of the commands used
Matlab Help
3
sqrt
4
Mathematical operation
>> 5+ 3
ans=
8
>> 5*4
=ans
20
>> 8/7
=ans
1.1429
>>5^3
=ans
125
>> ((5*3)-4)*2
=ans
22
>> 25^(0.5) or 25^(1/2) or sqrt(25)
=ans
5
Try : (10025100^1.52828228)*1.000541=???
To assign a value to a variable in MATLAB simply type the name of the variable,
followed by theassignment operator, = , followed by the value.
>> A=5
=ans
A=5
>> a=7
=ans
a=7
6
Trigonometric functions
Trigonometric functions
X : angle in rad X : angle in degree
sin(X)=… asin(…)=X sind(X)=… asind(…)=X
cos(X)=… acos(…)=X cosd(X)=… acosd(…)=X
tan(X)=… atan(…)=X tand(X)=… atand(…)=X
cot(X)=… acot(…)=X cotd(X)=… acotd(…)=X
sec(X)=… asec(…)=X secd(X)=… asecd(…)=X
csc(X)=… acsc(…)=X cscd(X)=… acscd(…)=X
>>sin(pi/2)
=ans
1
>>sind(90)
=ans
1
Ahmad Arafat Abu feilat
>>atan(1) E-mail: ahmadarafat2016@gmail.com
Tel:0797612096
=ans
0.7845 (rad)
>>atand(1)
=ans
45 (degree)
Problem:
1- Find :
csc(pi/3)
tan(2*pi/7)
atand(inf)
acotd(0)
sin(60)
7
logarithms
ln(x)log(x) log(x)log10(x)
>>log(10)
=ans
624/271
>>log10(10)
=ans
1
>>log(1)
=ans
0
Variable
>> a=7
=ans
a=7
8
Complex number
Entering complex numbers from the keyboard has to be done carefully. The
symbol "i" identifies the imaginary part and has to be typed immediately after
the numerical value of the imaginary part: for example, 2 + 3i .
Z=R+jX
R Real Part
XImaginary Part
>>i=2+3i
>>r=4.5-6i
>>e=5i
Command :
>>x=3+4i
>> real(x)
>> imag(x)
>> angle(x) ----- in rad **
>> abs(x)
Display format
Most common format :
Problem :
1-find the magnitude and angle of R if R=Z+T
Where X=2+j3 , T=-4-j3
Defined Functions
Use Anonymous Functions to define your own functions.
>> f = @(x) f(x): means constructs an inline function with variable x If no variable
exists, 'x‘ is used.
>> f=@(x) x*2
Problem :
Find f(5) , f(-20) if f(x)=𝑥 +6x+20
Roots
The roots of a polynomial are the values of the argument for which the value
of the polynomial is equal to zero. For example, the roots of the polynomial f
=x^2-2x-3 are x=[1 3].
Sol :
>>A=[1 2 -1 6]
>>roots(A) 10
>>O=[1 1 2]
>>roots(O)
>>N=[4 0 -1 0 0 3 0 -7]
>>roots(N)
Hint
Problem
Q1: find the roots of (X^2+X-5)
Q2: find the angle and the absolute of the complex number p=5+6i
Q3:find the sine of 160
Q4:find the tan inverse (1) in degree.
Q5: if o=10 u=4 y=7 , find r if r=(exp(-o)/sqrt(u))/pi
Solve Equations
Matlab help
11
To determine the unknown variables :
Sol:
MATLAB HELP
Try : Find x , y ,z
x-2y+3z=7
2x+y+z=4
-3x+2y-2z=-10
12
SOLVE DIFF. EQU.
13
Integration
Try:
c) d)
14
Differential
1)f2=(exp(x).*sin(x)./cos(x)).^(2.*x))
2)f2=sin(5.*h).*(cos(10.*h)).^10
15
Limit
Command:
limit(expr,x,a) ,limit(expr,a) ,limit(expr) ,limit(expr,x,a,'left') limit(expr,x,a,'right')
help??
Try:
16
Matrix
Enter a matrix
A=[ROW1 ; ROW 2 ; ROW3]
A=[ 6 10 ; 5 3; 0 2]
Sol:
>>P=[1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ]
>>Q=[-1 -2; 3 4 ; 5 6;-7 8]
Try:
17
Matrix operation
Ex:Find C=A+B
%%
clc
clear all
A=[2 1 ; 3 2 ;-2 2]
B=[1 1 ; 4 2 ; -2 1]
C=A+B
Hint :
18
Determine the value of Matrix
Hint:
Ex :
Sol:
%%
clc
clear all
O=[1 2 3 4 1 ; 0 -1 2 4 2 ; 0 0 4 0 0 ; -3 -6 -9 -12 4 ; 0 0 1 1 1]
det(O)
Try : find A
Commands
*linspace(a,b,n) , creates a vector of n elements linearly spaced from a to b.
Ex:
A=linspace(0,10,5)
19
*rand(a,b) , creates a matrix randomly where no. of rows = a, col =b between 0 to 1.
Ex:
D= rand(5,10),
* randi([a,b],c,d), creates a matrix randomly where no. of rows = a, col =b between two
number c , d. help ??
Ex:
E= randi([-5,30],10,1)
*ones(a,b), creates a matrix from ones where no. of rows = a, col =b.
Ex:
F= ones(10,5)
*zeros(a,b), creates a matrix from zeros where no. of rows = a, col =b.
Ex:
H= zeros(10,5)
*A', Transpose of A.
Ex:
V=1:5
K=V’
Ex: create the Matrix H start from 5 to 50 with step =5 and the Matrix
O start from 0 to 100 with log space by 10 elements, and find the
size & lengh of O & H.
Sol :
%%
clc
clear all
H=5:5:50
O=logspace(0,100,10)
size (O) ; length(O)
size (H) ; length(H)
Try: create the Matrix S start from 0 to 100 with step =2 and the
Matrix T start from 0 to 100 with log space by 51 elements, and find
the size & length of T & S.
Sol : --
20
Try: Create the random matrix with 3 rows 5 col. .and find
Transpose of matrix , inverse matrix , the value of matrix .
Sol:
A1
A2
A3
A4 A5
sol
%%
Clc
Clear all
A=[ 1 2 – 1 0; 0 5 3 0 ; - 2 0 0 4 ; 0 6 -4 -3]
A1=A([2,3],[1,2])
A2=A(1,4)
A3=A(3,[2 ,3,4])
A4=A(:,1) or A4=A([1,2,3,4],1)
A5=A(4,3)
Ex: Create ones matrix (A) with 5 rows 10 col. And set the value of
A(3,4) to -4.
Sol :
%%
Clc
Clear all 21
A=ones(5,10)
A(3,4)=-4
22
Sol:
-----------------------------------------------------------------------------------------------------------------------------------
Time (s) 1 6 11 16 21 26
Distance (m) 10 20 30 50 60 72
Speed (m/s)
23
Laplace and Fourier
The time function f is then formed and the inverse fourier command
is f = ifourier (F) .
Try:
25
Ex: plot the velocity with respect to time as a following table .
Time 1 2 3 4 5 6 7 8 9
velocity 10 22 33 44 58 65 66 67 67
Sol:
%%
clc
clear all
time=[1 2 3 4 5 6 7 8 9]
velocity=[ 10 22 33 44 58 65 66 67 67 ]
plot(time,velocity)
26
Commads
Sol:
clc
clear all
t=0:0.01:100
y=t.*sin(t)
plot(t,y)
title('example')
xlabel('time(s)')
ylabel('function')
legend('t.*sin(t)')
gtext('ahmad abu feilat ')
27
𝒕𝒔𝒊𝒏(𝒕)
Try : plot R if R= from 20 to 21 .
𝒄𝒐𝒔(𝒕)
28
𝟐𝒕
EX : plot R if R= from 5 to 50 , with green color and square point.
𝒄𝒐𝒔(𝒕)
Sol:
t=5:1:50
y=2./cos(t)
plot(t,y,'gs')
title('try')
xlabel('time(s)')
ylabel('function')
legend('2./ cos(t)')
grid
Ex:plot sine and cosine for one cycle on the same graph .
sol:
x=0:0.1:2*pi
y1=sin(x)
y2=cos(x)
plot(x,y1,x,y2)
xlabel('Time')
ylabel('Y')
title('MUtah ')
29
Ex: determine the speed of car and plot the relationship between
speed & distance with distance .
Time (s) 1 6 11 16 21 26
Distance (m) 10 20 30 50 60 72
Speed (m/s)
Sol:
clc
clear all
time=[1 6 11 16 21 26 ]
distance=[10 20 30 50 60 72 ]
speed=distance./time
plot(time,distance,time,speed)
grid
xlabel('time')
ylabel('magnitude')
title(speed & distance)
legend('distance','speed')
gtext('distance')
gtext('speed')
30
Q1:Two car :
Car 1 : Vo=10m/s , a=4m/s^2
Car2 : Vp=15 m/s ,a=1.75m/s^2
Draw the relationship between velocity and time of car1 & car2 for 20 s on
the same graph
Hint: V=Vo+at
Sol:
( ∗ )
Q2:If W= ,draw W vs t at m1= 0.1 , m2= 0.5 , m3=0.7 for t=0:0.01:10
(∜ )
on the same graph.
Sol:
31
logarithmic scales
semilogx(int,fun) plot data as logarithmic scales for the x-axis.
semilogy(int,fun) plot data as logarithmic scales for the y-axis.
Loglog(int,fun) plot data as logarithmic scales for the y & x axis.
Ex: Create a plot with a logarithmic scale for the x-axis and a linear
scale for the y-axis, where y=(t.^2).*cos(t) from 0 to 100
Sol:
t=0:0.01:100
y=(t.^2).*cos(t)
semilogx(t,y)
xlabel('time(s)')
ylabel('function')
grid
Ex: Create a plot with a logarithmic scale for the y-axis and a linear
scale for the x-axis, where y=(t.^2).*cos(t) from 0 to 100
Sol:
t=0:0.01:100
y=(t.^2).*cos(t)
semilogy(t,y)
xlabel('time(s)')
ylabel('function')
grid
32
Ex: Create a plot with a logarithmic scale for the y-axis and x-axis
where y=cos(t).* sin(t) from 0 to 100.
Sol
t=0:0.01:100
y=cos(t).* sin(t)
loglog(t,y)
xlabel('time(s)')
ylabel('function')
grid
plotyy
Plotyy(int1,fun1,int2,fun2) 2-D line plots with y-axes on both left
and right side
EX:Plot two data sets on one graph using two y-axes, from 0 to 20
y1 = 200.*exp(-0.05.*x).*sin(x)
y2 = 0.8.*exp(-0.5.*x).*sin(10*x)
Sol :
x = 0:0.01:20
y1 = 200.*exp(-0.05.*x).*sin(x)
y2 = 0.8.*exp(-0.5.*x).*sin(10*x)
plotyy(x,y1,x,y2)
EX:Plot two data sets on one graph using two y-axes, from 0 to 2*pi
y1 = sin(x)
y2 = x*sin(x)
33
bar & stem graph
bar(x,y) draws the bars at the locations specified by x.
stem(x,y) draws the stem at the locations specified by x.
sol:
%%
clc
clear all
years=1980:5:2020
people=[1.4 2.4 3.5 3.9 4.2 5.6 6.6 7.2 8.3]
bar(years,people)
or
%%
clc
clear all
years=1980:5:2020
people=[1.4 2.4 3.5 3.9 4.2 5.6 6.6 7.2 8.3]
stem(years,people)
stem bar
34
Try: create bar graph between years & pollution according to a
following table : Years Co2 Pollution =
co2 * 0.03
2010 150
2012 170
2014 200
2016 250
Pie chart
pie(X) draws a pie chart using the data in X. Each slice of the pie chart
represents an element in X.
Sol:
X=[33 50 17]
Pie(X)
35
EX : plot pie chart
Sol:
Y2010=[1400 1500 1600]
Y2015=[7000 8000 9000]
Y2020=[6000 9000 12000]
Y2025=[10000 5000 8000]
sum=Y2010+ Y2015+ Y2020+ Y2025
pie(sum)
gtext('jordan')
gtext('eygpt')
gtext('oman')
36
subplot
To create axes in tiled positions
subplot(m,n,p) divides the current figure into an m-
by-n grid and creates an axes for a
subplot in the position specified
Sol:
x=0:0.1:2*pi
y1=sin(x)
y2=sin(2.*x)
y3=sin(4.*x)
y4=sin(8.*x)
subplot(2,2,1)
plot(x,y1)
subplot(2,2,2)
plot(x,y2)
subplot(2,2,3)
plot(x,y3)
subplot(2,2,4)
plot(x,y4)
TRY:
37
3D plot
There are three requirements for plotting :
1-Time interval (x-axis & y-axis).
2-function (for example z).
3-command (plot command)
Sol:
%%
clc
clear all
[x,y]=meshgrid(0:0.1:10) Time interval (x&y-axis).
z=sin(x) function (z-axis).
plot3(x,y,z) command (plot command).
or
[x,y]=meshgrid(0:0.1:10)
z=sin(x)
mesh(x,y,z)
or
[x,y]=meshgrid(0:0.1:10)
z=sin(x)
surf(x,y,z)
38
𝒙 𝟐 𝒚 𝟐
Ex : plot f=𝒔𝒊𝒏𝒄( + )
𝝅 𝝅
Sol:
%%
[X,Y] = meshgrid(-10:0.25:10);
f = sinc(sqrt((X/pi).^2+(Y/pi).^2));
mesh(X,Y,f);
xlabel('x')
ylabel('y')
zlabel('sinc(R) ')
legend('sinc')
Try:
Z=sin(x).*cos(y)
Sol:
[X,Y] = meshgrid(0:0.1:12)
f1 =sin(X)
f2=cos(X)
mesh(X,Y,f1)
hold on
mesh(X,Y,f2)
39
Exersice :
Q1:
Q2:
Sol:
40
Sol:
Q4:Draw the effect of the area of conductor with constant length and
constant resistivity (l=0.2𝑚 , Ꝭ=1Ω.m) .
Ꝭ𝑳
Hint: R=
𝑨
Sol:
41