Week 1 To 5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Ahmad Arafat Abu feilat

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 ?

A high-performance language for technical computing.


Typical uses of MATLAB:
 Mathematical computations.
 Algorithmic development.
 Model prototyping (prior to complex model development).
 Data analysis and exploration of data (visualization).
 Scientific and engineering graphics for presentation.
 Complex analysis using MATLAB toolboxes

MATLAB's Power of Computational Mathematics

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

 MATLAB is a technical language to ease scientific computations


 The name is derived from MATrix LABoratory
 It provides many of the attributes of spreadsheets and programming languages
 MATLAB is a case sensitive language (a variable named “c” is different than another one called “C”)
 In interactive mode MATLAB scripts are platform independent (good for cross platform portability
 MATLAB works with matrices
 Everything MATLAB understands is a matrix (from text to large cell arrays and structure arrays)

The MATLAB Environme 1.5

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

Different ways to find information:

1. help command : >> help

3
sqrt

2. help general : >>help general


>> demo - Run demonstrations.
>> ver - MATLAB, Simulink and toolbox version information.

3. Ask for certain command: help mean, sqrt...


>> help sin
>> help sqrt
>> help cos

4. Mathwork Website 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=???

Note : General Commands


- clc : erases the command windows only
- clear (all) : erases all variables
- clear a, b : erases only specified variables
5
Variable

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

Ex : If x=5 , y=10 , z=1.5 find g=x*y*z , t=(0.5*x-y)/x


Sol
>> x=5
>>y=10
>>z=1.5
>> g=x*x*z
>> t=(0.5*x-y)/x

Try : find o = (x^2)+sqrt(y)- 𝒆𝒙 & k=ln(x) 𝑒 exp(x)


Problem:

1- If a=10 , b=52 , c=12.


Find :
-J=a+√𝑏 + 𝑎
-k=
2- A car is travelling at the speed of 50 km/hour . How much distance would it
travel in 2 hr?
Hint: distance=speed/time

3-Square shape , Length = 5m , Width = 12m . find area if area = L*W

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

The angle of (sin,cos,tan,cot,sec,sinh,…) rad (pi/2,pi,…)


The angle of (sind,cosd,tand,cotd,...) Degree (180, 45,90,…)

>>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

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

Ex : If x=5 , y=10 , z=1.5 find g=x*y*z , t=(0.5*x-y)/x


Sol
>> x=5
>>y=10
>>z=1.5
>> g=x*x*z
>> t=(0.5*x-y)/x

Try : find o = (x^2)+sqrt(y)- 𝒆𝒙 & k=ln(x) 𝑒 exp(x)

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
XImaginary Part

>>i=2+3i

>>r=4.5-6i

>>e=5i

Ex :find w=i+r ,l=e*r


Sol :
>> w= i+r
>> l=e*r

Command :

real(x) real part of a complex number


imag(x) imaginary part of a complex number
abs(x) magnitude of the complex number
angle(x) angle of a complex number x

>>x=3+4i
>> real(x)
>> imag(x)
>> angle(x) ----- in rad **
>> abs(x)

Display format
Most common format :

format long format short format bank


format long e format short e format rat
9
>>format rat
>> x=290/7
>>format long e
>> x=290/7
>>format short
>> x=290/7
>>format short e
>> x=290/7

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

To find f(x) at x=10 :


>> f(10)

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].

Ex : find the roots of A , O & N


A=X^3+2X^2-x+6
O=X^2+X+2
N=4X^7-X^5+3X^2-7

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 :

Ex : Find the X1 ,X2 ,X3


X1+5*X2-3*X3=3
5*X1+5*X2-3*X3=7
X1+5*X2+10*X3=17

Sol:
MATLAB HELP

Try : Find x , y ,z
x-2y+3z=7
2x+y+z=4
-3x+2y-2z=-10

Try : Find x if a*x^2+b*x+c=0

12
SOLVE DIFF. EQU.

13
Integration

Int(function, variable) Integration the function with


respect to variable.

Ex : find the following : ,


Sol:
>>syms x
>>int(1/(x.*log(x)),x)
>> int(1/((x^2)-8),x)

: Ex find the following:


Sol:
>>syms x
>>int('(cos(0.5.*x)).^2,x,(3*pi/5),(pi/9))

Try:

c) d)

14
Differential

provides the diff command for computing


Symbolic derivatives. Help??

EX:find the first derivative of f=sin(x).


Sol:
>>syms x
>> f=sin(x)
>>diff(f)

Try: find the first derivative of .(o=2.*t-5 , l=sin(exp(x)) , j=t.^3 ,


r=x.*sqrt(exp(x)) , c=tan(w))

EX: Computing (1st , 2ed , 3ed ) Order Derivatives of y=x.^4-7


Sol:
>>syms x
>> y=x.^4-7
>> diff(y)
>> diff(y,2)
>> diff(y,3)

Try : find (1st , 2ed ,.., 5th ) Order Derivatives of:

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??

Ex: find the limit of :


Sol:
>>syms x
>>limit((1 - cos(x))./x.^2,0)

Try:

16
Matrix

Enter a matrix
A=[ROW1 ; ROW 2 ; ROW3]

A=[ 6 10 ; 5 3; 0 2]

Ex: Define the matrix P, Q in matlab:

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

ADD & SUB. & MULT. & DEV.

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

Try : For previous A ,B values Find


Y=A-B
Z=A.*B
T=A*B
L=A./B
U=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)

*logspace(a,b,n) , creates a vector of n elements logarithmically spaced from 10 a to 10 b.


Ex:
B=logspace(0,10,5)

*A=a:step:b, creates a vector of linearly spaced by step from a to b.


Ex:
C=0:1:10

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)

*size(A), determine the size of A.


Ex:
size(H)

*whos ,view all defined matrix.

*length (A) , determine the length of A.


Ex:
length (H)

*inv(A) , determine the inverse of A.


Ex:
length (A)

*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:

Ex :find A1,….A5 from A

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)

Try : Create a matrix from 0 to 50 with 70 elements .

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:

-----------------------------------------------------------------------------------------------------------------------------------

Ex1: If A=2xD, A1=2xD1 ,where D=5 , D1=[1 2 3] .find A & A1.

Ex2: If R=sin(time), time from 0 to 10 with step equal 2 .find R.

Ex3: determine the speed of car .

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 laplace transform


command is F = laplace(f) .

Ex : convert f(t) = t.^2 to s domain .


>>syms t
>> f = t.^2
>> laplace(f)
The time function f is then formed and the inverse laplace command
is f = ilaplace(F) .

Ex : convert X(s) = (s.^5)./6 to s domain .


>>syms s
>>X=(s.^5)./6
>> x = ilaplace(X)

Try: convert x(t) = 𝒆𝒕 𝒔𝒊𝒏(𝒕) − 𝒄𝒐𝒔(𝒕) to s domain .

The time function f is then formed and the fourier transform


command is F = fourier (f) .

Ex : convert f(t) = t.^2 to s domain .


>>syms t
>> f = t.^2
>> fourier(f)

The time function f is then formed and the inverse fourier command
is f = ifourier (F) .

Ex : convert X(w) = (w.^5)./6 to s domain .


>>syms w
>>X=(w.^5)./6
>> x = ifourier (X)

Try: convert x(t) = 𝒆𝒕 𝒔𝒊𝒏(𝒕) − 𝒄𝒐𝒔(𝒕) to s domain


24
2D Plotting
There are three requirements for plotting :
1-Time interval (x-axis).
2-function (y-axis).
3-command (plot command)

plot(x axis , y axis ) Plot the function .

For example , to plot the y=sin(x) from 0 to 10


%%
clc
clear all
x=0:0.01:10 Time interval (x-axis).
y=sin(x) function (y-axis).
plot(x,y) command (plot command).

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)

Try : plot the relationship between


number of people and years .

26
Commads

xlabel(' … ') Add text label to x-axis


ylabel(' …') Add text label to y-axis
title(' … ') Add title to the figure
grid Grid on the figure
Legend (' … ') Add legend to the figure
gtext(' … ') Add text to the figure

Ex : plot y if y=𝒕𝒔𝒊𝒏(𝒕) from 0 to 100 .

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 .
𝒄𝒐𝒔(𝒕)

Color & line style & point style

Plot(x,y,'rs--') ,Color (red)


point style (square)
line style (--)

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

Drawing Multiple Functions on the same graph

Plot(int1,fun1,int2,fun2,…) Drawing Multiple Functions on the


same graph.

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')

Try : plot the following figure

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:

Ahmad Arafat Abu feilat


E-mail:
ahmadarafat2016@gmail.com
Tel:0797612096

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.

Ex : create bar & stem graph of the following table :

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.

Ex: create a pie chart of the faculty of engineering

Sol:
X=[33 50 17]
Pie(X)

Ex: create a pie chart of the faculty of engineering to mark a


electrical department.
sol:
X=[33 50 17]
explode = [1 0 0]
pie(X,explode)
gtext('Electrical')

Try : plot pie chart

2015 2016 2017 2018 2019 years


55 88 78 20 10 Import (million)

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

Ex: plot y1=sin(x) , y2=sin(2.*x) , y3=sin(4.*x) , y4=sin(8.*x)

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)

Plot3(x axis , y axis , z-axis ) 3D Plot the function .


mesh(x axis , y axis , z-axis ) 3D Plot the function .
surf(x axis , y axis , z-axis ) 3D Plot the function .

For example , to plot the z=sin(x)


from 0 to 10

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)

Ex :plot sine and cosine

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

You might also like