Top Banner
Introduction To Matlab Prepared by : 1 Prepared by: Eng. Amr Ezz Eldin Rashed Assistant lecturer TAIF university,KSA TEL:ksa 0554404723
351
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Matlab

Introduction To Matlab

Prepared by :

1

Prepared by:

Eng. Amr Ezz Eldin Rashed Assistant lecturer

TAIF university,KSA

TEL:ksa 0554404723

Page 2: Introduction to Matlab

Outlines(First Level) _24 hour

What is Matlab

Basic commands

Vectors and matrices

Statistics for vector and matrix

Control loops(if,for,switch,break)

2D plotting , mathematics(int ,diff ,limit,..)

Exam 2

Page 3: Introduction to Matlab

Outlines(Second Level)_24hour

3D plotting, animation

Dialog box

Simulink

Graphical user interface

Image and sound processing

البرامج الجاهزة

Project

EXAM 3

Page 4: Introduction to Matlab

Outlines(Third Level)_24 hour

Introduction to Image Processing

Point Processing ,Spatial Filtering

Neighborhood Processing

The Fourier Transform

Image Restoration

Image Segmentation

project 4

Page 5: Introduction to Matlab

What is Matlab?

What is possible in Matlab? graphic examples

How Matlab works? matrix, vector & scalar

syntax & important operators

basic commands & plot commands

creating a m-file

Statistics in Matlab some basics & example

Algebraic operations in Matlab

Useful links & other tutorials

5

Introduction to MATLAB

Page 6: Introduction to Matlab

What is MATLAB

The name MATLAB stands for Matrix laboratory(or

mathematical laboratory).

MATLAB is an interactive system whose basic data element

is an array that does not require dimensioning.

It‟s both a computer programming language, and a software

environment for using that language effectively.

Typical

6

Page 7: Introduction to Matlab

What is MATLAB

Matlab is a commercial "Matrix Laboratory" package which operates as an

interactive programming environment.

Matlab is available for PC's, Macintosh and UNIX systems.

Matlab is well adapted to numerical experiments.

Matlab program and script files (m-files) always have filenames ending with

".m";

The programming language is exceptionally straightforward since almost every

data object is assumed to be an array.

Graphical output (figure) is available to supplement numerical results.

Online help is available from the Matlab prompt (a double arrow) by typing help

7

Page 8: Introduction to Matlab

Typical Uses

Math and computation

Algorithm development

Modeling, simulation, and prototyping

Data analysis, exploration, and

visualization

Scientific and engineering graphics

Application development including GUI

8

Page 9: Introduction to Matlab

Why use Matlab?

Advantages:

Handles vector and matrices very nice

Quick plotting and analysis

EXTENSIVE documentation (type „help‟)

Lots of nice functions: FFT, fuzzy logic, neural nets,

numerical integration, OpenGL (!?)

Drawbacks:

Slow compared to C or Java

9

Page 10: Introduction to Matlab

Matlab History

In the 1970‟s, Cleve Moler “Professor of Math & Computer

Science, Chief Author of MatLab and one of the Founders of

Mathworks.Inc” participated in developing (EISPACK) and

(LINPACK). Those were collection of Fortran subroutines for

solving linear equations and Eigen value problems.

Later, when teaching courses in mathematics, Moler wanted

his students to be able to use LINPACK and EISPACK

without requiring knowledge of Fortran, so he developed the

first MATLAB in 1977 as an interactive system to access

LINPACK and EISPACK.

10

Page 11: Introduction to Matlab

MatLab History

The first Matlab was written in 2000 lines of Fortran, with Matrices as the only data type, 80 functions, no .m files and no toolboxes.

Jack Little, one of Moler‟s students saw Matlab potentials in Control systems & Signal Processing. They founded together Mathworks, Inc. in 1980

Mathworks is now responsible for development, sale, and support for MATLAB

MATLAB was rewritten in C with more functionality (such as plotting

routines), and now it contains more than 80,000 functions.

11

Page 12: Introduction to Matlab

The Basic Matlab System

It consists of 5 main parts:

– Development Environment.

– MATLAB Mathematical Function Library.

– MATLAB Language.

– Graphics.

– MATLAB External Interfaces

And finally the MatLab Toolboxes

– Toolboxes are comprehensive collections of MATLAB functions (M-

files) that extend the MATLAB environment to solve particular classes

of problems.

– Areas in which toolboxes are available include signal processing,

control systems, neural networks, communications, wavelets, Data

Acquisition, simulation, and many others.

12

Page 13: Introduction to Matlab

MATLAB Development

Environment & Basic Math

Functions

13

Page 14: Introduction to Matlab

Development Environment

• Run the matlab

14

Page 15: Introduction to Matlab

Development Environment

15

Command Window

History

Work Space

Variables

stores here

All of ur previous

commands

stores here

All commands,

programs runs from

here

Page 16: Introduction to Matlab

Variables

MATLAB variable names must begin with a

letter, which may be followed by any combination

of letters, digits, and underscores. MATLAB

distinguishes between uppercase and lowercase

characters, so A and a are not the same

variable(case sensitive).

When naming a variable, make sure you are not

using a name that is already used as a function

name,begin with character .

Page 17: Introduction to Matlab

Special Values

Function description

Ans Most recent answer (variable). If you do not assign an output variable to an

expression, MATLAB automatically stores the result in ans.

pi 3.1415926535897...

inf Infinity. Calculations like n/0, where n is any nonzero real value, result in

inf.

I,J The imaginary unit √-1

NaN,nan Not-a-Number, an invalid numeric value. Expressions like 0/0 and inf/inf

result in a NaN, as do arithmetic operations involving a NaN. n/0, where n

is complex, also returns NaN.

Page 18: Introduction to Matlab

Special Values

Description Function

Beep sound beep

Maximum real number that can be used realmax

minimum real number that can be used

realmin

Specifies the accuracy of floating point

Precision .

الخطوة بن عددن أو أصغر عدد مكن تعرفه

eps

18

Page 19: Introduction to Matlab

Operators

Arithmetic

– numeric computations, e.g., 2^10

Relational

– quantitative comparison of operands

– e.g., a < b

Logical

– AND, OR, NOT

–return Boolean variable, 1 (TRUE) or 0 (FALSE)

Page 20: Introduction to Matlab

Arithmetic operators

example symbol Operation

3+22 + Addition

54.6-16.5 _ subtraction

3.14*6 * Multiplication

10/100

100\10 / or \ Division

2^8 ^ power

20

Page 21: Introduction to Matlab

Relational Operators

Description

Relational Operator

Less than

<

Less than or equal

<=

Greater than

>

Greater than or equal

>=

Equal to

==

Not equal to

~=

21

Page 22: Introduction to Matlab

Examples

Command Result

5>8 ans=0

A=5<10 A=1

Y=(6<10)+(7>8)+(5*3==60/4) Y=2

B=[15 6 9]; C=[8 20 9];

D=C>=B

D=[0 1 1]

B= =C ans=[0 0 1]

B>6 ans=[1 0 1]

Page 23: Introduction to Matlab

Logical Operators

Logical Operator Description

& Element by element AND

| Element by element OR

~ NOT

&& Scalar AND with short circuiting

|| Scalar OR with short circuiting

Page 24: Introduction to Matlab

Examples

Command Output

3 & 7 ans=1

A=5 | 0 A=1

~25 ans=0

(12 & 0) +(~0)+(0 | 5) ans=2

X=[9 3 0]; Y=[2 0 13] X & Y ans=1 0 0

Z=X | Y Z=1 1 1

Page 25: Introduction to Matlab

Operator Precedence

Precedence Operation

1 (highest) ( )

2 ^

3 ~

4 *, /

5 +, -

6 Relational operations >,<, …

7 &

8 |

Page 26: Introduction to Matlab

Complex functions Description Item

Define a complex number Complex(2,-3)

Absolute value ;|x| Abs(x)

Angle of complex number x Angle(x)

Complex conjugate of x Conj(x)

Imaginary part of a complex number x Imag(x)

Real part of complex number x Real(x)

26

Page 27: Introduction to Matlab

Example

27

Page 28: Introduction to Matlab

Relational and Logical Functions

Function Description

xor(x,y) Exclusive OR

any(x) True if any element is non zero

all(x) True if all elements are non zero

Isequal(x,y) True if arrays are numerically equal

Isfloatpt True for a floating point number

isprime True for a prime number

Page 29: Introduction to Matlab

Example

29

Page 30: Introduction to Matlab

Information About System

30

Page 31: Introduction to Matlab

Information About System

31

Page 32: Introduction to Matlab

Calendar

32

Page 33: Introduction to Matlab

Information About System

33

Page 34: Introduction to Matlab

System and file commands Description Item

Clears command window clc

Remove variables from memory Clear , clear all

Display documentation doc

Checks for existence of file or variable exist

Declares variables to be global global

Display help text in the command window Help

Display help text in the help browser

helpwin

Searches help entries for a keyword lookfor

Stops Matlab Quit or exit

List current variables who

Long display) )List current variables whos 34

Page 35: Introduction to Matlab

How to open file (mspaint.exe)

35

Page 36: Introduction to Matlab

Trigonometric functions(Radian)

Description Item

Inverse cosine Acos(x)

Inverse cotangent Acot(x)

Inverse cosecant Acsc(x)

Inverse secant Asec(x)

Inverse sine Asin(x)

Inverse tangent Atan(x)

Cosine Cos(x)

cotangent Cot(x)

cosecant Csc(x)

Sine Sin(x)

tangent Tan(x)

36

Page 37: Introduction to Matlab

Trigonometric functions(degree)

Description Item

Inverse cosine Acosd(x)

Inverse cotangent Acotd(x)

Inverse cosecant Acscd(x)

Inverse secant Asecd(x)

Inverse sine Asind(x)

Inverse tangent Atand(x)

Cosine Cosd(x)

cotangent Cotd(x)

cosecant Cscd(x)

Sine Sind(x)

tangent Tand(x)

37

Page 38: Introduction to Matlab

Hyperbolic function

Description Item

Inverse cosine Acosh(x)

Inverse cotangent Acoth(x)

Inverse cosecant Acsch(x)

Inverse secant Asech(x)

Inverse sine Asinh(x)

Inverse tangent Atanh(x)

Cosine Cosh(x)

cotangent Coth(x)

cosecant Csch(x)

Sine Sinh(x)

tangent Tanh(x)

38

Page 39: Introduction to Matlab

Mathematical functions item Description

factor التحلل ال العوامل االولة

Primes االولة االقل من ولد قائمة باالعدادx

isprime عدtrue اذا كان العدد اولا

Gcd المشترك االكبر القاسم

lcm المضاعف المشترك االصغر اجاد

Factorial(x) الجاد المضروب

Gamma(x) تابع جاما

Beta(x,y) بتا تابع 39

Page 40: Introduction to Matlab

Examples

40

Page 41: Introduction to Matlab

Example

41

Page 42: Introduction to Matlab

System and file Commands

item Description

cd Change current directory

Date Display current date

dir Lists all files in the current directory

mkdir Used to make new directory

pwd Present work directory

what Lists all matlab files

Clock Display current clock and date 42

Page 43: Introduction to Matlab

Example

43

Page 44: Introduction to Matlab

Exponential functions item Description

exp التابع االس

log اللوغارتم الطبع

log10 10اللوغارتم لالساس

log2 2اللوغارتم لالساس

Sqrt(x) الجذر التربع

nthroot الجذر من المرتبةn

pow2 2^(x)

expm1 Exp(x)-1

log1p Log(x+1)

44

Page 45: Introduction to Matlab

Example

45

Page 46: Introduction to Matlab

Example

46

Page 47: Introduction to Matlab

Erf,erfc,expint,format

47

تابع اجاد الخطأ

تابع اجاد الخطأ المتمم

اجاد التكامل االس

رقم بفاصلة عائمة 15

Page 48: Introduction to Matlab

format

48

ارقام بفاصلة ثابتة 5

ارقام بفاصلة عائمة 5

رقم بفاصلة عائمة 15

الشكل الكسري

Page 49: Introduction to Matlab

الرياضيات في الماتالب

الجاد جذور المعادلة

X^4+2*x^3+4*x+5=0

>>roots([1 2 0 4 5])

والجاد المعادلة بمعرفة الحلول

>>poly([1 2 3 4])

49

Page 50: Introduction to Matlab

Cont.

50

Page 51: Introduction to Matlab

Integration

51

Page 52: Introduction to Matlab

Differentiation

52

Page 53: Introduction to Matlab

Laplace Transform

53

Page 54: Introduction to Matlab

Expand ,conv ,and deconv

54

Page 55: Introduction to Matlab

limit

55

Page 56: Introduction to Matlab

Vector defenition

56

Page 57: Introduction to Matlab

Vector definition

57

Page 58: Introduction to Matlab

Array addressing

58

Page 59: Introduction to Matlab

Array addressing

59

Page 60: Introduction to Matlab

Adding element to vector

60

Page 61: Introduction to Matlab

Vector statistics

61

Page 62: Introduction to Matlab

Max ,min ,

Page 63: Introduction to Matlab

Cont.

63

Page 64: Introduction to Matlab

Linspace , logspace

64

Page 65: Introduction to Matlab

Array Construction

65

Page 66: Introduction to Matlab

Array Construction

66

Page 67: Introduction to Matlab

Array orientation

>> c=[1;2;3;4;5]

c =

1

2

3

4

5

>> c‟ % transpose of c

ans =

1 2 3 4 5

Page 68: Introduction to Matlab

Array orientation

>> s=[1+2i 3-5i 3+4i]

s =

1.0000 + 2.0000i 3.0000 - 5.0000i 3.0000 + 4.0000i

>> f=s‘

f =

1.0000 - 2.0000i

3.0000 + 5.0000i

3.0000 - 4.0000i

>> g=s.‘ dot transpose operator g =

1.0000 + 2.0000i

3.0000 - 5.0000i

3.0000 + 4.0000i

Page 69: Introduction to Matlab

Matrix definition

69

Page 70: Introduction to Matlab

Math.

70

Page 71: Introduction to Matlab

Cont.

71

Page 72: Introduction to Matlab

Mean and Variance

Page 73: Introduction to Matlab

Determine,diagonal,inverse

73

Page 74: Introduction to Matlab

Maximum,minimum

74

Page 75: Introduction to Matlab

Find location of an element

75

Page 76: Introduction to Matlab

برنامج لحساب زاوة خط

X=[1 0 0;0 1 0;0 0 1];

[I,j]=find(x==1);

Length=max(i)-min(i);

Width=max(j)-min(j);

Ang=atand(length/width);

76

Page 77: Introduction to Matlab

Determines matrix elements

d=[ 1 2;4 5; 3 2]

d =

1 2

4 5

3 2

>> d(1,:) %row 1 and all columns

ans =

1 2

>> d(1,2) % row 1 and all column 2

ans =

2

>> d(:,2) %all row and l column 2

ans =

2

5

2

Page 78: Introduction to Matlab

Determines matrix elements

d =

1 2

4 5

3 2

>> d(2:3,:) ans =

4 5

3 2

Page 79: Introduction to Matlab

Add element to matrix

79

Page 80: Introduction to Matlab

Add element to matrix

80

Page 81: Introduction to Matlab

Special Matrix(zeros,ones,magic,eye)

>> f=zeros(3) zeros=false

f =

0 0 0

0 0 0

0 0 0

>> g=ones(4) ones=true

g =

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

Page 82: Introduction to Matlab

Special Matrix

>> v=zeros(2,3)

v =

0 0 0

0 0 0

>> h=3*ones(2,4)

h =

3 3 3 3

3 3 3 3

Page 83: Introduction to Matlab

Special Matrix

>> eye(3,3)

ans =

1 0 0

0 1 0

0 0 1

Page 84: Introduction to Matlab

True ,false

84

Page 85: Introduction to Matlab

Special matrix(rand,randn,randint)

85

Zero mean

unit variance

Random number

between 0 and 1

Page 86: Introduction to Matlab

Mean2,std2,randint

86

Page 87: Introduction to Matlab

Unique,intersect,setdiff

87

لحذف ما هو مكرر

وترتب النتجة

الحصول عل العناصر

المشتركة بن مصفوفتن

الحصول عل العناصر

وغر 1الموجودة ف

2موجودة ف

Page 88: Introduction to Matlab

Rot90,flipud,fliplr

Page 89: Introduction to Matlab

Matrix operations

89

Page 90: Introduction to Matlab

Example

90

Page 91: Introduction to Matlab

Example

91

Page 92: Introduction to Matlab

Example

92

Page 93: Introduction to Matlab

Example

93

Page 94: Introduction to Matlab

Example

94

Page 95: Introduction to Matlab

Example

95

Page 96: Introduction to Matlab

Add element to matrix

96

Page 97: Introduction to Matlab

Add element to matrix

97

Page 98: Introduction to Matlab

Add element to matrix

98

Page 99: Introduction to Matlab

Reshape,repmat

99

Page 100: Introduction to Matlab

Approximation functions

Floor : rounds value towards negative

infinity

Ceil: rounds value towards positive infinity

Fix: rounds value towards zero

Round: normal approximation

100

Page 101: Introduction to Matlab

Approximation functions

101

Page 102: Introduction to Matlab

Approximation functions

102

Page 103: Introduction to Matlab

Approximation functions

103

Page 104: Introduction to Matlab

Number systems

104

Page 105: Introduction to Matlab

Cont.

105

Page 106: Introduction to Matlab

Character matrix

106

Page 107: Introduction to Matlab

Character matrix

107

Page 108: Introduction to Matlab

Character matrix

108

Page 109: Introduction to Matlab

Tic,toc

109

Page 110: Introduction to Matlab

Multidimensional matrix

110

Page 111: Introduction to Matlab

Cont.

111

Page 112: Introduction to Matlab

Cont.

112

Page 113: Introduction to Matlab

Cont.

113

Page 114: Introduction to Matlab

Introduction to signal and image processing

114

Page 115: Introduction to Matlab

Reading image

>> x=imread('C:\Program

Files\MATLAB\R2007a\toolbox\images\imd

emos\cameraman.tif');

>>y=imread('cameraman.tif');

>> imshow(y)

115

Page 116: Introduction to Matlab

Image show

116

Page 117: Introduction to Matlab

Operations on image

117

Page 118: Introduction to Matlab

Operations on image

>> diag(y);

>> trace(y)

ans =

27029

>> imshow(flipud(y))

>>imresize(y,[128 128]);

118

Page 119: Introduction to Matlab

Figure(2)

119

Page 120: Introduction to Matlab

120

>>imshow(fliplr(y));

Page 121: Introduction to Matlab

Edge detection

121

Page 122: Introduction to Matlab

Cont.

122

Page 123: Introduction to Matlab

Cont.

123

Page 124: Introduction to Matlab

Color image,imwrite

124

Page 125: Introduction to Matlab

imwrite

imwrite(z,'amr.jpg')

imwrite(y,„d:\amr2.jpg')

125

Page 126: Introduction to Matlab

Reading and writing sound

>>[y,fs]=wavread('C:\WINDOWS\Media\chi

mes.wav');

>>Help auread

>> soundsc(y) or wavplay(y,fs)

>>size(y)

>>wavwrite(y,‟c:\aa.wav‟) or auwrite

نأخذ جزء من الصوت ونتعامل معه<<

126

Page 127: Introduction to Matlab

Operation on sound

127

Page 128: Introduction to Matlab

Video read

128

Page 129: Introduction to Matlab

Cont.

129

Page 130: Introduction to Matlab

Flow Control

MATLAB has several flow control constructs:

For loop.

If statement.

Switch and case.

While.

Continue.

Break.

Try – catch.

Return.

Page 131: Introduction to Matlab

For Loop

for x = array

(commands)

end

The (commands) are executed once for

every column in array

At each iteration, x is assigned to the next

column of the array

Page 132: Introduction to Matlab

Example

For i=1:10

i

end

i≤10 نفذ طالما

132

Page 133: Introduction to Matlab

Example

for i=1:10

disp(i)

end

بجانب كل قمةI ال عرض كلمة

133

Page 134: Introduction to Matlab

Example♣♣

disp(„the numbers from 1 to 10 are:‟)

for i=1:10

disp(i)

end

134

Page 135: Introduction to Matlab

Factorial

n=5;

f=1;

for i=2: n

f=f*i;

end

disp(f)

135

Page 136: Introduction to Matlab

1:5إلجاد مجموع األعداد من ◄

n=5

s=0

for i=1: n

s=s+i;

end

disp(s)

136

Page 137: Introduction to Matlab

1:5إلجاد مجموع مربعات األعداد من ◄

n=5

s=0

for i=1: n

s=s+i^2;

end

disp(s)

137

Page 138: Introduction to Matlab

1:5إلجاد جذور األعداد من ◄

n=5;

for i=1: n

disp(sqrt(i))

end

138

Page 139: Introduction to Matlab

Example

n=5

disp(„number square root‟)

disp(„ „)

for i=1: n

AA=sqrt(I);

disp([ I AA ])

لعرض مجموعة من المتغرات

End

139

Page 140: Introduction to Matlab

Example

N=10

12/1! + 22/2!+………..+n2/n!

اإلشارة ثابتة

12/1! -22/2!+…-……..+n2/n!

اإلشارة متغرة

140

Page 141: Introduction to Matlab

Example

n=5;

fact=1;

Sum=0;

For I=1:n

fact=fact*I;

T=(I^2)/fact;

sum=sum+ T;

End

Disp(sum) 141

Page 142: Introduction to Matlab

Example

ملحوظة◄

لعمل إشارة أحد الحدود موجب واآلخر سالب

(-1) i+1

Fact=-1

Fact= - fact *i

142

Page 143: Introduction to Matlab

Example

n=input („enter any integer‟);

Sum =0;

For i=1: n

Sum=sum+i

End

Disp(sum);

143

Page 144: Introduction to Matlab

Example

What is your name?

How old are you?

age, nameطبع

144

Page 145: Introduction to Matlab

Example

Nam=input („what is your name?‟ , ‟s‟);

character حتوي علىstring

Age=input („how old are you?‟);

Disp(nam)

Disp(age)

145

Page 146: Introduction to Matlab

Note

:ملحوظة

\لك ترك سطر نستخدم عالمة

\\نستخدم عالمة backslashولك طبع

بجانب بعضهم name ,ageولطبع

Disp ([x, y]);

x, yإما أن كون ⤾

String or numأي جب أن كونا من نفس النوع

Disp ([nam, num2str (age)]);

146

Page 147: Introduction to Matlab

Example

5اطبع مجموع األعداد التى تقبل القسمة على

sum=0;

For i=0:5:100

sum=sum+I;

end

Disp(sum)

147

Page 148: Introduction to Matlab

Nested Loops

for n=1:5

for m=5:-1:1

A(n,m)=n^2+m^2;

end

end

A =

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

Page 149: Introduction to Matlab

3x3 Mean filter

149

Page 150: Introduction to Matlab

While Loops

while expression

(commands)

end

Commands are executed as long as all elements in expression are true.

Usually evaluation of expression gives scalar

–In case of array all elements must be true

Page 151: Introduction to Matlab

Example

while x<=15

x=2*x;

end

Be careful and try to avoid infinite loops!

To stop the execution of an infinite loop use Ctrl+C

x =3

x =24

Page 152: Introduction to Matlab

If-End Structure

if expression

(commands)

end

The (commands) are evaluated if all

elements in expression are true (nonzero)

Page 153: Introduction to Matlab

If-Else-End Structure

if (expression)

(commands evaluated if true)

else

(commands evaluated if false)

end

Page 154: Introduction to Matlab

Flow control - selection

The if-elseif-else construction

if <logical expression>

<commands>

elseif <logical expression>

<commands>

else

<commands>

end

if height>170

disp(’tall’)

elseif height<150

disp(’small’)

else

disp(’average’)

end

Page 155: Introduction to Matlab

Example

X=input ('enter any value');

If (x>0) disp ('positive');

End

IF (x<0) disp ('negative');

End

endب matlab ال غلق برنامج ال

155

Page 156: Introduction to Matlab

Example

iF (x>0) disp ('positive');

else if (x<0) disp ('negative');

else

disp ('zero');

End

وجد دالة جاهزة تقوم بنفس الوظفة:ملحوظة

sign

156

Page 157: Introduction to Matlab

Example

x,yأدخل قمة

x,yام ال ونطبع yاكبر من x ونحدد هل

X is greater than y

X is lower than y

X is equal to y

157

Page 158: Introduction to Matlab

Example

158

Page 159: Introduction to Matlab

Example

5لطباعة األعداد التى ال تقبل القسمة على

For i=0:100

If (rem(I,5)~=0)

disp(i);

End

End

159

Page 160: Introduction to Matlab

برنامج الجاد جذور معادلة تربعة

Disp.→this program is used to solve the quadratic eqn

Disp→ Ax2+Bx+c=0

a=input→enter the value of A:___

b=input→enter the value of B:___

c=input→ enter the value of C:___

D=B2- 4AC

• X1=x2=-b/2a X1= (-B+sqrt (d))/ (2*a))

X2= (-B-sqrt (d) )/ (2*a))

160

0 Non zero

Page 161: Introduction to Matlab

Program

161

Page 162: Introduction to Matlab

Example

X=floor (rand*6) +1;

If (x==1) disp ('that is 1');

Else if (x==2) disp ('that is 2');

Else if (x==3) disp ('that is 3');

Else if (x==4) disp ('that is 4');

Else if (x==5) disp ('that is 5');

Else disp ('that is 6');

Or else if (x==6) disp ('that is 6'); end

162

Page 163: Introduction to Matlab

برنامج درجات الطالب

163

Page 164: Introduction to Matlab

Example

apples=10;

cost=apples*25;

if apples > 5

cost=(1 -20/100)*cost % 20% discount

end

Page 165: Introduction to Matlab

Switch-Case Construction

switch expression

case test_expression1

(commands1)

case {test_expression2, test_expression3}

(commands2)

otherwise

(commands 3)

end

Page 166: Introduction to Matlab

Example

method = 'Bilinear'; switch (method) case 'linear' disp('Method is linear') case 'cubic' disp('Method is cubic') otherwise disp('Unknown method.') end Method is Unknown method

Page 167: Introduction to Matlab

multiple casesفى حالة switchباستخدام

X=floor (rand*6)+1;

Switch x → expression or variable مكن أن كون

Case 1

Disp ('that is 1');

Case 2

Disp ('that is 2');

Case 3

Disp ('that is 3');

Case 4

Disp ('that is 4');

Case 5

Disp ('that is 5');

Case 6

Disp ('that is 6');

Or

Otherwise

Disp ('that is 6'); End

167

Page 168: Introduction to Matlab

Program

168

Page 169: Introduction to Matlab

Example

لحساب أكثر من حالة مع بعض

X=floor (rand*6)+1;

1-if (x==1 | x==2 | x==3)

2-if (1<=x<=3)

3-case {1,2,3}

disp ('from 1 to 3')

case {4,5}

disp ('4 or 5')

case 6 / otherwise

disp (that is 6');

end

169

Page 170: Introduction to Matlab

Program

170

Page 171: Introduction to Matlab

Program

171

Page 172: Introduction to Matlab

passwordبرنامج لعمل

172

Page 173: Introduction to Matlab

Another solution

173

Page 174: Introduction to Matlab

Another solution

174

Page 175: Introduction to Matlab

program

ف 3المطلوب ف البرنامج السابق جعل عدد المحاوالت

.حالة كلمة السر الخطأ وواحدة ف حالة الكلمة الصححة

175

Page 176: Introduction to Matlab

program

176

Page 177: Introduction to Matlab

while

177

Page 178: Introduction to Matlab

Fprintf,save,load,uisave

178

Page 179: Introduction to Matlab

لحساب الوقت المستغرق لتنفذ برنامج

179

Page 180: Introduction to Matlab

program

Enter the no. of student=n

Vector (x)

Average = mean (x)

The first = max (x) الطالب األول

The last = min (x) الطالب األخر

180

Page 181: Introduction to Matlab

program

181

Page 182: Introduction to Matlab

Example

A=[2 3;0 4];

B=[-5 7;10 2];

Display

1-add a to b

2-max value of a

3-Diag of b

4-exit

182

Page 183: Introduction to Matlab

program

183

Page 184: Introduction to Matlab

program

184

Page 185: Introduction to Matlab

Another solution

185

Page 186: Introduction to Matlab

الدخال عناصر مصفوفة

Enter the number of raws

Enter the number of column

Enter the values

186

Page 187: Introduction to Matlab

program

187

Page 188: Introduction to Matlab

Security program

0 5 0 1

2 3 4 6

0 10 15 20

1 8 0 2

3 5 0 1

2 3 4 6

188

Page 189: Introduction to Matlab

program

189

Page 190: Introduction to Matlab

Matlab simulink

190

Page 191: Introduction to Matlab

simulink

191

Page 192: Introduction to Matlab

simulink

192

Page 193: Introduction to Matlab

Example

193

Page 194: Introduction to Matlab

simulink

194

Page 195: Introduction to Matlab

Cont.

195

Page 196: Introduction to Matlab

196

Page 197: Introduction to Matlab

197

Page 198: Introduction to Matlab

Example 2

198

Page 199: Introduction to Matlab

Cont.

199

Page 200: Introduction to Matlab

Cont.

200

Page 201: Introduction to Matlab

Cont.

201

Page 202: Introduction to Matlab

Cont.

202

Page 203: Introduction to Matlab

Cont.

203

Page 204: Introduction to Matlab

Cont.

204

Page 205: Introduction to Matlab

Cont.

205

Page 206: Introduction to Matlab

Simulink Power Window Controller Hybrid System Model

206

Page 207: Introduction to Matlab

Filtered QPSK vs. MSK

207

Page 208: Introduction to Matlab

Cont.

208

Page 209: Introduction to Matlab

AM DSB_SC modulation

209

Page 210: Introduction to Matlab

Scope

210

Page 211: Introduction to Matlab

Image processing example

211

Page 212: Introduction to Matlab

o/p video viewer

212

Page 213: Introduction to Matlab

Simple power model

213

Page 214: Introduction to Matlab

Cont.

214

Page 215: Introduction to Matlab

Scop

215

Page 216: Introduction to Matlab

Simple models(simpower system)

216

Page 217: Introduction to Matlab

Power electronic models(simpower sys)

217

Page 218: Introduction to Matlab

Graphical User Interface

218

Page 219: Introduction to Matlab

Start Guide

219

Page 220: Introduction to Matlab

Example

220

Page 221: Introduction to Matlab

Property Inspector

221

Page 222: Introduction to Matlab

Figure

222

Page 223: Introduction to Matlab

Call backs

223

Page 224: Introduction to Matlab

Call Backs

224

Page 225: Introduction to Matlab

Project 2

225

Page 226: Introduction to Matlab

Example 2

226

Page 227: Introduction to Matlab

Example

227

Page 228: Introduction to Matlab

Open file

228

Page 229: Introduction to Matlab

Pop up menu

229

Page 230: Introduction to Matlab

Edit button

230

Page 231: Introduction to Matlab

Push button

231

Page 232: Introduction to Matlab

Program 3

232

Page 233: Introduction to Matlab

Am modulation

233

Page 234: Introduction to Matlab

Operation

234

Page 235: Introduction to Matlab

Pop up menu

235

Page 236: Introduction to Matlab

Carr_freq

236

Page 237: Introduction to Matlab

Sampling_freq

237

Page 238: Introduction to Matlab

Am_mod

238

Page 239: Introduction to Matlab

Import data

239

Page 240: Introduction to Matlab

Code

% --- Executes on button press in put.

function put_Callback(hObject, eventdata, handles)

% hObject handle to put (see GCBO)

% eventdata reserved - to be defined in a future version of

MATLAB

% handles structure with handles and user data (see

GUIDATA)

distor=200;

set(handles.edit1,'String',distor);

240

Page 241: Introduction to Matlab

Calculator program

241

Page 242: Introduction to Matlab

Calculator program2

242

Page 243: Introduction to Matlab

243

Page 244: Introduction to Matlab

244

Page 245: Introduction to Matlab

حل المعادلة التربعة

245

Page 246: Introduction to Matlab

Menu editor

246

Page 247: Introduction to Matlab

برنامج عرض صورة الوان وابض واسود

247

Page 248: Introduction to Matlab

برنامج كلمة المرور

248

Page 249: Introduction to Matlab

Visible property

249

Page 250: Introduction to Matlab

Code

250

Page 251: Introduction to Matlab

Open , save (menu items)

251

Page 252: Introduction to Matlab

البرامج الجاهزة

252

Page 253: Introduction to Matlab

Funtool

253

Page 254: Introduction to Matlab

تحدد مركز الهزة االرضة

254

Page 255: Introduction to Matlab

رسم اشارة وتصمم المرشح المرغوب به

255

Page 256: Introduction to Matlab

Analog modulation

256

Page 257: Introduction to Matlab

RLC demo

257

Page 258: Introduction to Matlab

Neural network tool

258

Page 259: Introduction to Matlab

nftool

259

Page 260: Introduction to Matlab

nprtool

260

Page 261: Introduction to Matlab

Wavelet transform

261

Page 262: Introduction to Matlab

sigdemo1

262

Page 263: Introduction to Matlab

Filter design tool

263

Page 264: Introduction to Matlab

xpsound

264

Page 265: Introduction to Matlab

querybuilder

265

Page 266: Introduction to Matlab

Gui to exe))deploytool

266

Page 267: Introduction to Matlab

phone

267

Page 268: Introduction to Matlab

nndtoc

268

Page 269: Introduction to Matlab

nctool

269

Page 270: Introduction to Matlab

Dialog box

270

Page 271: Introduction to Matlab

helpdlg

271

Page 272: Introduction to Matlab

Input dialoge

272

Page 273: Introduction to Matlab

Message box

273

Page 274: Introduction to Matlab

Color question

274

Page 275: Introduction to Matlab

Warning dialog

275

Page 276: Introduction to Matlab

Wait bar

276

Page 277: Introduction to Matlab

List dialog

d = dir;

str = {d.name};

[s,v] = listdlg('PromptString','Select a file:',...

'SelectionMode','single',...'ListString',str)

277

Page 278: Introduction to Matlab

List dialog

smpl_length={'20','25','30','100','200','500'};

entry2=listdlg('name','input for sample

lengths','promptstring',...

'enter sample length

values','liststring',smpl_length);

278

Page 279: Introduction to Matlab

Cont.

279

Page 280: Introduction to Matlab

Uigetfile

280

Page 281: Introduction to Matlab

uisave

281

Page 282: Introduction to Matlab

uiopen('figure')

282

Page 283: Introduction to Matlab

[file,path] = uiputfile('animinit.m','Save file name');

283

Page 284: Introduction to Matlab

Graphics

284

Page 285: Introduction to Matlab

Sine wave

285

0 1 2 3 4 5 6 7-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 286: Introduction to Matlab

Program 2

286

Page 287: Introduction to Matlab

Figure

287

0 0.5 1 1.5 2 2.5 3 3.5 40

1

2

3

4

5

6

Page 288: Introduction to Matlab

Program 3

288

Page 289: Introduction to Matlab

figure

289

0 0.5 1 1.5 2 2.5 3 3.5 40

1

2

3

4

5

6aaaaaaa

time

dist

ance

Page 290: Introduction to Matlab

Program 4

290

Page 291: Introduction to Matlab

figure

291

0 0.5 1 1.5 2 2.5 3 3.5 40

1

2

3

4

5

6aaaaaaa

time

dist

ance

xxxx

move with mouse

Page 292: Introduction to Matlab

Program 5

292

Page 293: Introduction to Matlab

figure

293

1 2 3 4 5 6 7 8 9 100

100

200

300

400

500

600

700

800

Page 294: Introduction to Matlab

Program 6

294

Page 295: Introduction to Matlab

Figure

295

0 1 2 3 4 5 6 7-5

0

5

10

15

20

25

30

35

40

Page 296: Introduction to Matlab

Program 6

296

Page 297: Introduction to Matlab

الرسم بإستخدام االحداثات الدائرة

297

Page 298: Introduction to Matlab

figure

298

0.1

0.2

0.3

0.4

0.5

30

210

60

240

90

270

120

300

150

330

180 0

Page 299: Introduction to Matlab

Program 7

299

Page 300: Introduction to Matlab

Program 8

300

Page 301: Introduction to Matlab

Figuer

301

1 2 3 4 5 6 7 8 9 100

10

20

30

40

50

60

70

80

90

100

Page 302: Introduction to Matlab

Program 9

302

Page 303: Introduction to Matlab

figure

303

1 2 3 4 5 6 7 8 9 100

50

100

1 2 3 4 5 6 7 8 9 100

0.5

1

Page 304: Introduction to Matlab

Program 10

304

Page 305: Introduction to Matlab

Program 11

305

Page 306: Introduction to Matlab

Program 12

306

Page 307: Introduction to Matlab

Figure

307

0 2 4 6 8-1

-0.5

0

0.5

1

0 2 4 6 8-1

-0.5

0

0.5

1

0 2 4 6 8-1

-0.5

0

0.5

1

-5 0 5 10-1

-0.5

0

0.5

1

Page 308: Introduction to Matlab

Program

308

Page 309: Introduction to Matlab

Figure

309

0 2 4 6 8-1

-0.5

0

0.5

1number one

0 2 4 6 8-1

-0.5

0

0.5

1number two

0 2 4 6 8-1

-0.5

0

0.5

1number three

-5 0 5 10-1

-0.5

0

0.5

1number four

Page 310: Introduction to Matlab

ezplot

310

-6 -4 -2 0 2 4 6

-1

-0.5

0

0.5

1

x

sin(x)

Page 311: Introduction to Matlab

cont

311

-6 -4 -2 0 2 4 6

0

0.2

0.4

0.6

0.8

1

x

sin(x)2

Page 312: Introduction to Matlab

312

-5 0 5

0

20

40

60

80

100

120

140

160

x

4 x2

-5 0 5

-50

-40

-30

-20

-10

0

10

20

30

40

50

x

8 x

-5 0 57

7.2

7.4

7.6

7.8

8

8.2

8.4

8.6

8.8

9

x

8

-5 0 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

x

0

Page 313: Introduction to Matlab

Graphics 3d

313

Page 314: Introduction to Matlab

Figure

314

00.2

0.40.6

0.81

00.2

0.40.6

0.810

0.2

0.4

0.6

0.8

1

x axisy axis

z label

Page 315: Introduction to Matlab

program

[X,Y] = meshgrid(-2:.2:2, -2:.2:2);

Z = X .* exp(-X.^2 - Y.^2);

surf(X,Y,Z)

315

Page 316: Introduction to Matlab

figure

316

-2

-1

0

1

2

-2

-1

0

1

2-0.5

0

0.5

Page 317: Introduction to Matlab

program

[X,Y] = meshgrid(-2:.2:2, -2:.2:2);

Z = X .* exp(-X.^2 - Y.^2);

subplot(221)

mesh(Z)

subplot(222)

mesh(Z)

view(-37.5,70)

subplot(223)

mesh(Z)

view(-37.5,10)

subplot(224)

mesh(Z)

view(0,0)

317

Page 318: Introduction to Matlab

figure

318

020

40

0

20

40-0.5

0

0.5

0

20

40

0

10

20

30-0.5

00.5

0 20 400

2040

-0.5

0

0.5

0 10 20 30-0.5

0

0.5

Page 319: Introduction to Matlab

sphere

319

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

1-1

-0.5

0

0.5

1

Page 320: Introduction to Matlab

Sphere(40)

320

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

1-1

-0.5

0

0.5

1

Page 321: Introduction to Matlab

mesh

[x,y,z]=sphere(40);

Mesh(x,y,z)

321

Page 322: Introduction to Matlab

Figure

322

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

1-1

-0.5

0

0.5

1

Page 323: Introduction to Matlab

cylinder

323

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

10

0.2

0.4

0.6

0.8

1

Page 324: Introduction to Matlab

Cylinder(r,n)

324

-5

0

5

-5

0

50

0.2

0.4

0.6

0.8

1

Page 325: Introduction to Matlab

program

[x,y,z]=cylinder(5,30);

Mesh(x,y,z)

325

Page 326: Introduction to Matlab

figure

326

-5

0

5

-5

0

50

0.2

0.4

0.6

0.8

1

Page 327: Introduction to Matlab

program

t = 0:pi/50:10*pi;

plot3(sin(t),cos(t),t,‟r‟);

Grid;axis square

327

Page 328: Introduction to Matlab

figure

328

-1

-0.5

0

0.5

1

-1

-0.5

0

0.5

1

0

10

20

30

40

Page 329: Introduction to Matlab

Pie,pie3

Subplot(211)

pie([1 2 3 5],{'North','South','East','West'})

Subplot(212)

pie3([2 4 3 5],[0 1 1 0],{'North','South','East','West'})

329

Page 330: Introduction to Matlab

figure

330

North

South

East

West

East

West

South

North

Page 331: Introduction to Matlab

program

331

Page 332: Introduction to Matlab

figure

332

1 1.5 2 2.5 34

5

6

7

0 0.5 10

0.2

0.4

0.6

0.8

1

0 0.5 10

0.2

0.4

0.6

0.8

1

0 0.5 10

0.2

0.4

0.6

0.8

1

Page 333: Introduction to Matlab

program

333

rectangle('Position',[0.59,0.35,3.75,1.37],...

'Curvature',[0.8,0.4 ],...

'LineWidth',2,'LineStyle','--')

Page 334: Introduction to Matlab

figure

334

0.5 1 1.5 2 2.5 3 3.5 4 4.50.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

Page 335: Introduction to Matlab

program

rectangle('Position',[0.59,0.35,3.75,1.37],...

'Curvature',[0 0],...

'LineWidth',2,'LineStyle','--')

335

Page 336: Introduction to Matlab

figure

336

0.5 1 1.5 2 2.5 3 3.5 4 4.50.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

Page 337: Introduction to Matlab

program

clf;close all;clear all;

rectangle('Position',[0.59,0.35,3.75,1.37],...

'Curvature',[1 1],...

'LineWidth',2,'LineStyle','--')

337

Page 338: Introduction to Matlab

Figure

338

0.5 1 1.5 2 2.5 3 3.5 4 4.50.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

Page 339: Introduction to Matlab

program

clf;close all;clear all;

rectangle('Position',[0.59,0.35,3.75,1.37],...

'Curvature',[1 1],...

'facecolor','g')

339

Page 340: Introduction to Matlab

figure

340

0.5 1 1.5 2 2.5 3 3.5 4 4.50.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

Page 341: Introduction to Matlab

program

341

Page 342: Introduction to Matlab

Figure

342

02

46

810

0

5

100

2

4

6

8

10

Page 343: Introduction to Matlab

program

343

Page 344: Introduction to Matlab

Figure

344

020

40

0

20

40-0.5

0

0.5

0

20

40

0

10

20

30-0.5

00.5

0 20 400

2040

-0.5

0

0.5

0 10 20 30-0.5

0

0.5

Page 345: Introduction to Matlab

program

345

Page 346: Introduction to Matlab

Figure

346

05

1015

2025

0

10

20

30-30

-20

-10

0

10

20

30

Page 347: Introduction to Matlab

Draw circles

347

Page 348: Introduction to Matlab

Result

348

Page 349: Introduction to Matlab

PROGRAM

المطلوب هو عمل برنامج لوضع االسم والدرجة والتقدر

كل واحدة ف مصفوفة وعرضهم

349

Page 350: Introduction to Matlab

350

Page 351: Introduction to Matlab

functions

Main program

for i=1:10

disp(i)

s(i)

end

Function s(i)

for k=1:i

fprintf(„*‟)

end

fprintf(„ „) 351