Professional Documents
Culture Documents
Inroduction To Matlab
Prepared by:
Eng. Ali Nasser
E-mail: eeng.ali@gmail.com
Eng.Ali Nasser
Prepared by :
1
? What is MATLAB
جاءت كلمة ماتالب MATLABمن األحرف األولى للعبارة Matrix
Laboratoryأي مختبر المصفوفاتٌ ،حث تتعامل لغة ماتالب مع الثوابت
والمتحوالت كمصفوفات رٌاضٌة ،وبنا ًء على ذلك العملٌات الرٌاضٌة
االفتراضٌة فً ماتالب هً عملٌات على مصفوفات .مثالًa * b :هً عملٌة
Eng.Ali Nasser ضرب مصفوفتٌن األولى aوالثانٌة b
قام بتأسٌس البرنامج شخصان ,األول هو كلٌف مولر والثانً جاك لٌتل
كلٌف مولرهو إستاذ الرٌاضٌات وعلوم الحاسب Computer Science In
the 1970’s
2
? What is MATLAB
برنامج الماتالب هو برنامج هندسً ٌقوم بعملٌات تحلٌل وتمثٌل
البٌانات من خالل معالجة تلك البٌانات
ٌستطٌع البرنامج العمل فً جمٌع المجاالت الهندسٌة مثل أنظمة
Eng.Ali Nasser
التحكم Control System,وفً مجال المٌكانٌكا
Mechanical Field,وكذلك محاكة اإللكترونٌات
Electronicsوصناعة السٌارات Automotive
Industry,وكذلك مجال الطٌران والدفاع الجوي
Aerospace and Defense,والكثٌر من التطبٌقات
الهندسٌة.
3
? What is MATLAB
وحتى أؤكد للجمٌع ذلك ,قامت شركة السٌارات المرموقة نٌسان
Nissanبتخفٌض وقت التطوٌر إلى %50عندما قامت
بإستبدال التصمٌم على األوراق Paper Model Based
Eng.Ali Nasser
Designإلى األداة المتطورة فً برنامج الماتالب وهو Model
Based Design,
4
Why MATLAB?
Data Acquisition 4
5
Why Matlab?
Graphing
A Comprehensive array of plotting options
6
Why MATLAB?
Programming
Eng.Ali Nasser
7
?Why MATLAB
SIMULINK برنامج المحاكة للماكٌنة وتشغٌله فى الصوره الواقعٌة
Eng.Ali Nasser
8
Why MATLAB?
Eng.Ali Nasser
Applications Using Matlab
مقدمة فً الماتالب-1
Eng.Ali Nasser
Signal Application Using Matlab تطبٌقات األشارة بإستخدام الماتالب4-
Digital Signal Processing Using Matlab عملٌات اإلشارة الرقمٌة بإستخدام الماتالب5-
Image Processing Applications Using Matlab تطبٌقات معالجة الصور بإستخدام الماتالب7-
Radio Frequency Applications Using Matlab تطبٌقات موجات الرادٌو بإستخدام الماتالب8-
10
Applications Using Matlab
9-التطبٌقات المٌكانٌكٌة بإستخدام الماتالب Mechanical Applications Using Matlab
Eng.Ali Nasser
12-التطبٌقات اإللكترونٌة بإستخدام الماتالب Electronics Applications Using Matlab
14-التطبٌقات المستخدمة فً صناعة السٌارات بإستخدام الماتالب Automotive Applications Using Matlab
15-التطبٌقات المستخدمة فً علوم الفضاء والدفاع الجوي بإستخدام الماتالب Aerospace and Defense
Applications Using Matlab
11
Our mission
بأذن هللا الكورس هٌغطى المواضٌع االتٌة:
-1مقدمة فً الماتالب
12
The Basic Matlab System
Eng.Ali Nasser
13
MATLAB Development
Eng.Ali Nasser
Environment & Basic Math
Functions
14
Development Environment
• Run the matlab
Eng.Ali Nasser
15
Development Environment
Variables
stores here
Eng.Ali Nasser
Work Space
All commands,
programs runs from
here
All of ur previous
commands Command Window
stores here
History
16
.بعض الرموز شائعة االستخدام فى البرنامج
Eng.Ali Nasser
Typing %
هذه العالمة توضع بعد نهاية جمله ما لتعطى commentعن االتى من معادالت ما او شىء
اخر
clc
وعنذ كتابت clcيتم مسح كل ما هى فى البرنامح
pi
Eng.Ali Nasser
3.1415926535897...
inf Infinity. Calculations like n/0, where n is any nonzero real value,
result in inf.
Addition + 3+22
subtractionEng.Ali Nasser
_ 54.6-16.5
Multiplication * 3.14*6
Division / or \ 10/100
100\10
power ^ 2^8
20
Example:
Eng.Ali Nasser
>> 3+[2*(3+4)^2*(1+3)^3]/[3*(1-3)^2*8]
ans =
68.3333
21
1.2 Complex functions
Item Description
22
Example
Eng.Ali Nasser
23
Example
Eng.Ali Nasser
24
1.3 Exponential functions
item Description
Eng.Ali Nasser
log10 10 اللوغارٌتم لالساس
pow2 2^(x)
expm1 Exp(x)-1
log1p Log(x+1)
25
Example
Eng.Ali Nasser
26
Example
Eng.Ali Nasser
27
1.4 Trigonometric functions(Radian)
Item Description
Acos(x) Inverse cosine
Acot(x) Inverse cotangent
Eng.Ali Nasser
Acsc(x) Inverse cosecant
Asec(x) Inverse secant
Asin(x) Inverse sine
Atan(x) Inverse tangent
Cos(x) Cosine
Cot(x) cotangent
Csc(x) cosecant
Sin(x) Sine
Tan(x) tangent
28
Trigonometric functions(degree)
Item Description
Acosd(x) Inverse cosine
Acotd(x) Inverse cotangent
Eng.Ali Nasser
Acscd(x) Inverse cosecant
Asecd(x) Inverse secant
Asind(x) Inverse sine
Atand(x) Inverse tangent
Cosd(x) Cosine
Cotd(x) cotangent
Cscd(x) cosecant
Sind(x) Sine
Tand(x) tangent
29
Hyperbolic function
Item Description
Acosh(x) Inverse cosine
Acoth(x) Inverse cotangent
Eng.Ali Nasser
Acsch(x) Inverse cosecant
Asech(x) Inverse secant
Asinh(x) Inverse sine
Atanh(x) Inverse tangent
Cosh(x) Cosine
Coth(x) cotangent
Csch(x) cosecant
Sinh(x) Sine
Tanh(x) tangent
30
Example
>> y=sin(pi/2)
y= 1
>> y=2*cos(pi)
y = -2
Eng.Ali Nasser
>> y=asin(1)
y = 1.5708
>> y*180/pi
ans = 90
>> y=atan(1)
y = 0.7854
>> y*180/pi
ans = 45 31
1.5 Mathematical functions
item Description
factor
Primes
Eng.Ali Nasser
التحليل الي العوامل االولية
32
Examples
Eng.Ali Nasser
33
1.6 Array addressing
6 Eng.Ali Nasser
>>x(1:3) % display elements from 1 to 3
ans =
1 3 5
Array Addressing
Eng.Ali Nasser
>> x(2:2:6) %display elements from 2
to 6 step = 2
ans =
3 6 0
Array Construction
a=
1 2 Eng.Ali
3 4 5 Nasser
b=
1 3 5 7 9
Array Construction
>> c=[b a]
c=
1
Eng.Ali Nasser
3 5 7 9 1 2 3 4 5
>> sort(c)
ans =
1 1 2 3 3 4 5 5 7 9
Standard deviation and variance
>> a=[1:9]
a=
1 2 3 4 5 6 7 8 9
>> std(a)
ans =
2.7386Eng.Ali Nasser
>> var(a)
ans =
7.5000
median(a)
ans =
5
Max ,min ,prod,length and sum
>> a=[1:9]
a=
1 2 3 4 5 6 7 8 9
>> max(a)
Eng.Ali Nasser
ans = 9
>> min(a)
ans = 1
>> prod(a)
ans =
362880
>> a=[1 2 4 6];
>> length(a)
ans = 4
>> sum(a)
ans = 45
1.7 المصفوفات
X+3Y-4Z=0 4
-2Y+8Z=0
Eng.Ali Nasser
42
Eng.Ali Nasser
43
Eng.Ali Nasser
44
Eng.Ali Nasser
45
Eng.Ali Nasser
46
Eng.Ali Nasser
47
Eng.Ali Nasser
48
Solve Equations by Matrix
Eng.Ali Nasser
49
1.8
Eng.Ali Nasser
50
4
Eng.Ali Nasser
51
Eng.Ali Nasser
52
Eng.Ali Nasser
53
Eng.Ali Nasser
54
Eng.Ali Nasser
55
Eng.Ali Nasser
56
Multiplication
Eng.Ali Nasser
57
1.9 Differentiations
Eng.Ali Nasser
>> syms x
>> y =3*x^2+2*x+1;
>> diff(y,x)
= 6*x + 2
58
تفاضل بتثبت متغٌر
diff (U,v): Returns the derivative of the expression U
with respect to the variable v
Eng.Ali Nasser
59
Evaluate the derivative at x = a
To evaluate the derivative dy/dx at x = a enter:
Eng.Ali Nasser
60
1.10The integration function
4 The int function is used to integrate a symbolic expression.
As with the diff function, there are many ways to use the
int function.
Eng.Ali Nasser
4 Using (y = 3x) as an example
>> syms x
>> y=int(3*x);
ans =(3*x^2)/2
61
4 2-int (W,a,b): returns the integral of the expression W with
respect to the default
independent variable evaluated over the interval [a b].For
Eng.Ali Nasser
example
>> syms x
>> int(x^3,3,5)
ans =136 62
4 3-int (W,u,a,b): returns the integral of the expression W
with respect to the variable u evaluated over the interval
[a,b].For example
Eng.Ali Nasser
>> syms x y
>> int (x^2*y^2,y,-1,2)
ans =
3*x^2 63
1.11 Limit
64
Example
Eng.Ali Nasser
>> syms x
>> limit(sin(3*x)/x)
ans =3
1-by default, MATLAB finds the limit as x goes to zero. If you want
to find the
previous limit when x goes to (e.g. 1) you may enter the following
code:
Eng.Ali Nasser
66
Session(2)
2.1 Basic Graphics
1- If x is a vector, the command plot(x) will produce a linear
plot of the elements in the vector x as a function of the
index of the elements in x.
Eng.Ali Nasser
If x = [0 0.9 1.1 1.9 2 3.5 4]
4 Then plot(x) produces the graph shown in Figure
67
2-If x and y are vectors of the same length, then the command plot(x,y)
plots the elements of x versus the elements of y.
Eng.Ali Nasser
68
Eng.Ali Nasser
69
After Editing
Eng.Ali Nasser
70
Eng.Ali Nasser
71
Example:
X= 0:10
Eng.Ali Nasser
72
Eng.Ali Nasser
73
Eng.Ali Nasser
74
Eng.Ali Nasser
75
4
Eng.Ali Nasser
76
Eng.Ali Nasser
77
2.2 The hold command
It creates a plot that needs two or more plot commands. You can use the hold
command to plot two curves on a single graph
Eng.Ali Nasser
78
2.3 Subplot
هو امر يستخدم لتقسيم شاشة ال figureورسم اكتر من رسمه بجانب بعض او تحت او فوق بعض
)Subplot(m,n,p
عدد الصفوف =m
عدد االعمده =n
رقم الرسمة داخل P=m&n
Eng.Ali Nasser
79
Subplots
t=0:0.1:6;
w1=sin(t).*exp(t-1);
w2=tan(2*t);
subplot(2,2,1)
Eng.Ali Nasser
plot(t,w1)
title('w1')
subplot(2,2,2)
plot(t,w2)
title('w2')
subplot(2,2,3)
plot(t,w1+w2)
title('w1+w2')
subplot(2,2,4)
plot(t,w1-w2)
title('w1-w2')
80
Example
x=-10:.01:10; y4=y2-y1;
y1=sin(x); subplot(3,2,4)
subplot(3,2,1) plot(x,y1,x,y4)
plot(x,y1) xlabel('x')
xlabel('x') ylabel('y')
ylabel('y') title('cos(x)-sin(x)')
Eng.Ali Nasser
title('sin(x)') y5=sin(x);
subplot(3,2,2) y6=cos(x);
y2=cos(x); subplot(3,2,5)
plot(x,y2,'r') plot(x,y5,'r',x,y6,'k')
xlabel('x') xlabel('x')
ylabel('y') ylabel('y')
title('cos(x)') title('sin(x) and cos(x)')
subplot(3,2,3) subplot(3,2,6)
plot(x,y1+y2,'k') dc=5;
xlabel('x') plot(x,dc,'r')
ylabel('y') xlabel('x')
title('sin(x)+cos(x)') ylabel('y')
title('DC component')
81
4
Eng.Ali Nasser
82
Eng.Ali Nasser
83
2.4 The fill command
4
>> t= 0:pi/40: 4*pi;
>> y=sin(t);
>> fill(t,y,'b')
Eng.Ali Nasser
84
2.5 Discontinuous graphs
Eng.Ali Nasser
85
4
Eng.Ali Nasser
86
2.6 Example
Eng.Ali Nasser
87
Eng.Ali Nasser
88
2.7 Plots With Special Graphics
Eng.Ali Nasser
89
Pie Chart
Grades A B C D E
Students 11 18 26 9 5
Class Grades
Eng.Ali Nasser
grd=[11 18 26 9 5]; 7%
pie(grd) 16%
title('Class Grades') 13%
E
A
D
data relativeٌجعل ال
وٌتم كتابة على الرسم من B
26%
editig ploter<view C
38%90
2.8 Import Data
EXAMPLE
the experimental data obtained in an electric machines laboratory
which represents the relation between the speed and the generated
voltage for a DC Shunt Wound Generator, where N is the generator
speed, in rpm and U is the generated voltage, in Volt.
Eng.Ali Nasser
Speed&volt
4 YOU must save data in text file using any text editor (notepad) and then save
data in “.txt or .dat” command,in this example save “Data.txt”
91
Import Data
180
4
load data.txt 160
x=data(:,1); 140
y=data(:,2);
volt (v)
120
plot(x,y)
Eng.Ali Nasser
100
80
60
40
500 1000 1500 2000
speed (rpm)
Should be saved In
C:\Users\Eng.Ali\Documents\MATLAB\data.txt
92
2.9 Statistical Analysis
For 2 days the diameter of a turned shaft at center lathe is recorded
below, it is required to
Days Reading
x(:,1)=[19,12]; 1 19 15 16 10 10 12
Eng.Ali Nasser
x(:,2)=[15,19]; 2 12 19 10 16 15 11
x(:,3)=[16,10];
x(:,4)=[10,16]; رقم العمود لنفس المتغير:, 14
x(:,5)=[10,15];
12
x(:,6)=[12,11];
a=mean(x,2) mean & madian اليحاد 10
var(x,0,2) 2
y=1:2; 0
1 2 93
bar(y,a)
Random Number
Random number
between 0 and 1
Eng.Ali Nasser
Mean=0
Stand.Devision=1
94
Example
Generate values from the uniform distribution on
the interval [a, b].
r = a + (b-a).*rand(100,1);
Eng.Ali Nasser
r=-5+(15*rand(1,10))
صف واحد
بداية الفترة
ومجموع الفترة
95
Example
Generate a bell-curve histogram from Gaussian data
x = -4:0.1:4; 450
y = randn(1,10000); 400
Eng.Ali Nasser
hist(y,x) 350
300
250
Assume:
200
Mean=0& st,dev=1
150
Y=mean+st*randn(1,no.) 50
0
-5 -4 -3 -2 -1 0 1 2 3 4 5
96
Gaussian or Normal distribution
The Gaussian distribution is specified by two parameters:
(1) the population mean μ,
(2) the population standard deviation σ, with the distribution function
given by
Eng.Ali Nasser
0.5
فعلياx هنا قومت بقياس قيم ل
mean=0 وبفرض ان
0.4
St=1 0.3
x=linspace(-3,3,100); 0.2
z=exp(-(x.^2))/(sqrt(2*pi)); 0.1
plot(x,z)
0
-4 -3 -2 -1 0 1 2 3 4
97
التقرٌب
>> floor(1.9)
down
ans = 1
up
>> ceil (1.9)
Eng.Ali Nasser
ans = 2
>> round(3.9)
ans= 4 Normal
>> round(3.4)
ans = 3
98
مرة طٌب عاٌز50 فلنفرض ان معاٌا عملة وهقوم بقلبه
اعرف كم مرة كتابة وكم مرة ملك
هو عدد االحتماالت2
وعرفناhead=1
Tail=0 n = 50; % number of flips
coin = floor(2*rand(1,n)) % vector of n flips: 0: tails, 1:heads
Eng.Ali Nasser
straight line curve, are chosen to minimize
4 the average of the squared distances between the line and
the measured data values.
Example:
100
4
120
Eng.Ali Nasser
100
80 Y=mx+c=20.8x+3.8
Temp (F)
60
40
20
0
-1 0 1 2 3 4 5 6 101
Time (s)
Curve Fitting
x = 0:0.1:1;
y = [-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];
a2 = polyfit(x,y,2)
a10 = polyfit(x,y,10) 16
2nd and 10th Degree Polynomial Curve Fit
xi = linspace(0,1,101);
Eng.Ali Nasser
14
yi2 = polyval(a2,xi);
yi10 = polyval(a10,xi); 12
plot(x,y,'o',xi,yi2,'--',xi,yi10) 10
xlabel('x'), ylabel('y')
8
title('2nd and 10th Degree Polynomial Curve Fit')
y
2
Measured
0 2nd Degree
10th Degree
-2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
x
1021
Spline
x=[0 5 20 8 25 35];
y=[0 15 20 25 30 40];
curve توصٌل النقط بافضل
45
xx=linspace(0,35,100);
40
yy=spline(x,y,xx);
Eng.Ali Nasser
plot(x,y,'o',xx,yy) 35
30
25
20
15
10
0
0 5 10 15 20 25 30 35
103
Free Drawing
4 axes('position',[0 0 1 1])
postion ادخل على ال
4 [x,y] = ginput;
Eng.Ali Nasser
4 n = length(x); mouse ادخل النقط بال
4 s = (1:n);
4 t = (1:.05:n); لتوصٌل خط بٌن النقط
4 u = spline(s,x,t);
4 v = spline(s,y,t);
4 clf reset
4 plot(x,y,'.',u,v,'-'); 104
2.11 Interpolation
4
Eng.Ali Nasser
x = (0:5);
y(:,1) = [0,20,60,68,77,110];
y(:,2) = [0,25,62,67,82,103];
y(:,3) = [0,52,90,91,93,96];
interp1(x,y,2.6)
ans =
64.8000 65.0000 90.6000
105
Assignment(2)
Eng.Ali Nasser
106
Session(3)
Programing with GUI
Graphic User Interface البرمجة بأستخدام الواجهات الرسومية
To access this feature in MATLAB select file → New →GUI
Eng.Ali Nasser
107
Eng.Ali Nasser
108
User Graphical Interface
Eng.Ali Nasser
109
Example(1) step by step
سنقوم بعمل برنامج يقوم بجمع اوطرح رقمين
Eng.Ali Nasser
110
Eng.Ali Nasser
111
ادخل اسم لٌعرف من سٌتخدم البرنامج ماذا ٌفعلstatic text
Eng.Ali Nasser
112
stringوغٌر االسم منDouble click
Eng.Ali Nasser
113
على االزرارpanel اعمل
Eng.Ali Nasser
114
Eng.Ali Nasser
115
m-file اضغط وسٌفتح معاك
Eng.Ali Nasser
116
f اضغط على رمز
Eng.Ali Nasser
117
sum_button اضغط على
Tag String ًواكتب تحت الكالم االخضر االت
• x1=str2double(get(handles.No1,'String'));
• x2=str2double(get(handles.No2,'String'));
• y=x1+x2;
Eng.Ali Nasser
• set(handles.Result,'string',num2str(y));
118
• x1=str2double(get(handles.No1,'String'));
• x2=str2double(get(handles.No2,'String'));
• y=x1+x2;
• set(handles.Result,'string',num2str(y));
السطر التانى
Eng.Ali Nasser
Eng.Ali Nasser
120
Example(2)
Eng.Ali Nasser
121
Eng.Ali Nasser
122
Control Statements
Relational Operators Description 4
<=
>
Eng.Ali Nasser Less than or equal
Greater than
== Equal to
~= Not equal to
123
Examples True or False as 01
Command Result
5>8 ans=0
A=5<10 A=1
Eng.Ali Nasser
Y=(6<10)+(7>8)+(5*3==60/4) Y=2
B= =C ans=[0 0 1]
B>6 ans=[1 0 1]
Eng.Ali Nasser
125
3.2 Logical Operators
Eng.Ali
| Nasser
At least one is true (nonzero) OR
A=5 | 0 A=1
Eng.Ali
~25 Nasserans=0
~0 ans =1
Z=X | Y Z=1 1 1
Control Statements
1-For loops
for x = array
(commands)
end Eng.Ali Nasser
The (commands) are executed once for every
column in array
128
Example
1-find the squares of all integers starting from 1 to 5
Eng.Ali Nasser
for i = 1:5
x = i^2
end
The output of this code: x = 1,4,9,16,25.
129
Flow Control
MATLAB has several flow control constructs:
if, else, and elseif
switch and case
for
While
Eng.Ali Nasser
continue
Break
try – catch
return
For Loop
for x = array
(commands)
Eng.Ali Nasser
end
4 The (commands) are executed once for
every column in array
4 At each iteration, x is assigned to the next
column of the array
Example
4 For i=1:10
4i
4 end
Eng.Ali Nasser
4 ٌنفذ طالما i≤10
132
Example
4 for i=1:10
4 disp(i)
4 end
Eng.Ali Nasser
بجانب كل قٌمةI ال ٌعرض كلمة4
133
♣♣Example
Eng.Ali Nasser
4 for i=1:10
4 disp(i)
4 end
134
Factorial
4 n=5;
4 f=1;
Eng.Ali Nasser
4 for i=2: n
4 f=f*i;
4 end
4 disp(f)
135
1:5 ◄إلٌجاد مجموع األعداد من
4 n=5
4 s=0
Eng.Ali Nasser
4 for i=1: n
4 s=s+i;
4 end
4 disp(s)
136
1:5 ◄إلٌجاد مجموع مربعات األعداد من
4 n=5
4 s=0
Eng.Ali Nasser
4 for i=1: n
4 s=s+i^2;
4 end
4 disp(s)
137
1:5 ◄ إلٌجاد جذور األعداد من
4 n=5;
4 for i=1: n
Eng.Ali Nasser
4 sqrt (i)
4 end
4 disp(sqrt(i))
138
Example
4 n=5
4 disp(‘number square root’)
4 disp(‘
Eng.Ali Nasser ‘)
4 for i=1: n
4 AA=sqrt(I);
4 disp([ I AA ])
4 لعرض مجموعة من المتغٌرات
4 End 139
Example
4 N=10
4 12/1! + 22/2!+………..+n2/n!
140
Example
n=5;
fact=1;
Eng.Ali Nasser
Sum=0;
For I=1:n
fact=fact*I;
T=(I^2)/fact;
sum=sum+ T;
End
sum 4 14
1
Example
◄ملحوظة 4
لعمل إشارة أحد الحدود موجب واآلخر سالب 4
142
Example
n=input (‘enter any integer’); 4
Sum =0; 4
Eng.Ali Nasser
For i=1: n 4
Sum=sum+i 4
End 4
Disp(sum); 4
143
Example
4 What is your name?
4 How old are you?
144
Example
4 Nam=input (‘what is your name?’ , ’s’);
4 character ٌحتوي علىstring
Eng.Ali Nasser
4 Age=input (‘how old are you?’);
4 Disp(nam)
4 Disp(age)
145
Note
4ملحوظة:
4لكً ٌترك سطر نستخدم عالمة \
4ولكً ٌطبع backslashنستخدم عالمة \\
Eng.Ali Nasser
4ولطبع name ,ageبجانب بعضهم
Disp ([x, y]); 4
⤾ 4إما أن ٌكون x, y
أي ٌجب أن ٌكونا من نفس النوعString or num
;)])Disp ([nam, num2str (age
146
Examples
for n=1:10
x(n)=sin(n*pi/10);
End
Eng.Ali Nasser
Example
5 اطبع مجموع األعداد التى تقبل القسمة على
4 sum=0;
4
Eng.Ali Nasser
For i=0:5:100
4 sum=sum+I;
4 end
4 Disp(sum)
148
Nested Loops
for n=1:5
for m=5:-1:1
Eng.Ali Nasser
A(n,m)=n^2+m^2;
A=
end
end 2 5 10 17 26
5 8 13 20 29
10 13 18 25 34
17 20 25 32 41
26 29 34 41 50
While Loops
while expression
(commands)
end
Eng.Ali Nasser
4 Commands are executed as long as all
elements in expression are true.
4 Usually evaluation of expression gives
scalar
In case of array all elements must be true –
Example
x =3
while x<=15
x=2*x;
Eng.Ali Nasser
end
x =24
Be careful and try to avoid infinite loops!
To stop the execution of an infinite loop use Ctrl+C
If-End Structure
if expression
(commands)
Eng.Ali Nasser
elseif <logical expression>
<commands>
if height>170
disp(’tall’)
else elseif height<150
<commands> disp(’small’)
end else
disp(’average’)
end
Example
X=input ('enter any value'); 4
If (x>0) disp ('positive'); 4
End 4
Eng.Ali Nasser
IF (x<0) disp ('negative'); 4
End 4
end بmatlab ال ٌغلق برنامج ال4
155
Example
iF (x>0) disp ('positive'); 4
else if (x<0) disp ('negative'); 4
else 4
Eng.Ali Nasser
disp ('zero'); 4
end 4
156
Example
x,y أدخل قٌمة4
x,y ام ال ونطبعy اكبر منx ونحدد هل4
Eng.Ali Nasser
4 X is greater than y
4 X is lower than y
4 X is equal to y
157
Example
Eng.Ali Nasser
158
Example
159
برنامج الٌجاد جذور معادلة تربٌعٌة
4 Disp.→this program is used to solve the quadratic eqn
4 Disp→ Ax2+Bx+c=0
4
4
Eng.Ali Nasser
b=input→enter the value of B:___
4 D=B2- 4AC
0 Non zero
• X1=x2=-b/2a X1= (-B+sqrt (d))/ (2*a))
• X2= (-B-sqrt (d) )/ (2*a))
160
Program
Eng.Ali Nasser
161
Example
4 X=floor (rand*6) +1;
4 If (x==1) disp ('that is 1');
4 Else if (x==2) disp ('that is 2');
Eng.Ali Nasser
4 Else if (x==3) disp ('that is 3');
4 Else if (x==4) disp ('that is 4');
4 Else if (x==5) disp ('that is 5');
4 Else disp ('that is 6');
4 Or else if (x==6) disp ('that is 6'); end 162
Example
apples=10;
cost=apples*25;
Eng.Ali Nasser
if apples > 5
cost=(1 -20/100)*cost % 20% discount
end
Switch-Case Construction
switch expression
case test_expression1
(commands1)
Eng.Ali Nasser
case {test_expression2, test_expression3}
(commands2)
otherwise
(commands 3)
end
Example
method = 'Bilinear';
switch (method)
Eng.Ali Nasser
case 'linear'
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
otherwise
disp('Unknown method.')
end
Method is Unknown method
multiple cases فى حالةswitch باستخدام
4 X=floor (rand*6)+1;
4 Switch x → expression or variable ٌمكن أن ٌكون
4 Case 1
4 Disp ('that is 1');
4 Case 2
4 Disp ('that is 2');
4
4
4
Case 3
Eng.Ali Nasser
Disp ('that is 3');
Case 4
4 Disp ('that is 4');
4 Case 5
4 Disp ('that is 5');
4 Case 6
4 Disp ('that is 6');
4 Or
4 Otherwise
4 Disp ('that is 6'); End 166
Program
Eng.Ali Nasser
167
Example
لحساب أكثر من حالة مع بعض4
4 X=floor (rand*6)+1;
4 1-if (x==1 | x==2 | x==3)
Eng.Ali Nasser
4 2-if (1<=x<=3)
4 3-case {1,2,3}
4 disp ('from 1 to 3')
4 case {4,5}
4 disp ('4 or 5')
4 case 6 / otherwise
4 disp (that is 6');
4 end 168
Program
Eng.Ali Nasser
169
Program
Eng.Ali Nasser
170
password برنامج لعمل
Eng.Ali Nasser
171
Another solution
Eng.Ali Nasser
172
Another solution
Eng.Ali Nasser
173
while
Eng.Ali Nasser
174
fprintf
Eng.Ali Nasser
175
لحساب الوقت المستغرق لتنفٌذ برنامج
Eng.Ali Nasser
176
program
4 Enter the no. of student=n
4 Vector (x)
Eng.Ali Nasser
4 Average = mean (x)
4 The first = max (x) الطالب األول
4 The last = min (x) الطالب األخٌر
177
program
Eng.Ali Nasser
178
Example
4 A=[2 3;0 4];
4 B=[-5 7;10 2];
Eng.Ali Nasser
4 Display
4 1-add a to b
4 2-max value of a
4 3-Diag of b
4 4-exit
179
program
Eng.Ali Nasser
180
program
Eng.Ali Nasser
181
Another solution
Eng.Ali Nasser
182
الدخال عناصر مصفوفة
4 Enter the number of raws
4 Enter the number of column
Eng.Ali Nasser
4 Enter the values
183
program
Eng.Ali Nasser
184
Matlab simulink
Eng.Ali Nasser
185
simulink
Eng.Ali Nasser
186
simulink
Eng.Ali Nasser
187
Example
Eng.Ali Nasser
188
simulink
Eng.Ali Nasser
189
Eng.Ali Nasser
190
Eng.Ali Nasser
191
Eng.Ali Nasser
192
Example 2
Eng.Ali Nasser
193
Eng.Ali Nasser
194
Eng.Ali Nasser
195
Eng.Ali Nasser
196
Eng.Ali Nasser
197
Eng.Ali Nasser
198
Eng.Ali Nasser
199
Eng.Ali Nasser
200
Simulink Power Window Controller Hybrid System Model
Eng.Ali Nasser
201
Filtered QPSK vs. MSK
Eng.Ali Nasser
202
Eng.Ali Nasser
203
AM DSB_SC modulation
Eng.Ali Nasser
204
Scope
Eng.Ali Nasser
205
Graphical User Interface
Eng.Ali Nasser
206
Start Guide
Eng.Ali Nasser
207
Example
Eng.Ali Nasser
208
Property Inspector
Eng.Ali Nasser
209
Figure
Eng.Ali Nasser
210
Call backs
Eng.Ali Nasser
211
Call Backs
Eng.Ali Nasser
212
Example 2
Eng.Ali Nasser
213
Example
Eng.Ali Nasser
214
Open file
Eng.Ali Nasser
215
Pop up menu
Eng.Ali Nasser
216
Edit button
Eng.Ali Nasser
217
Push button
Eng.Ali Nasser
218