You are on page 1of 13

4/29/2010

Special Matrices, Cells,


Structures , Functions
Chapter 2 & 3

Contents

• Special Matrices
• Polynomial Operations Using Arrays
• Cell Arrays
• Structure Arrays
• Functions
• Summary
• Problems

1
4/29/2010

Special Matrices
ones Creates array of all ones zeros Creates array of all ones

Syntax Syntax
Y = ones(n) Y = zeros(n)
Y = ones(m,n) Y = zeros(m,n)
Y =ones([m n]) Y =zeros([m n])
Y = ones(m,n,p,...) Y = zeros(m,n,p,...)
Y= ones([m n p ...]) Y= zeros([m n p ...])
Y = ones(size(A)) Y = zeros(size(A))
ones(m, n,...,classname) zeros(m, n,...,classname)
ones([m,n,...],classname) zeros([m,n,...],classname)

Description Description
Y = ones(n) returns an n-by-n matrix of 1s. Y = zeros(n) returns an n-by-n matrix of 0s.
An error message appears if n is not a An error message appears if n is not a
scalar. ......... See Matlab docs for details scalar. ......... See Matlab docs for details
Example Example
x = ones(2,3,'int8'); x = zeros(2,3,'int8');

Special Matrices (ctd)


rand
Uniformly distributed pseudorandom numbers
eye Creates Identity matrix Syntax
Y = rand(n)
Syntax Y = rand(m,n)
Y = eye(n) Y =rand([m n])
Y = eye(m,n) Y = rand(size(A))
Y =eye([m n]) rand(m, n,...,classname)
Y = eye(size(A)) rand([m,n,...],classname)
eye(m, n,...,classname)
eye([m,n,...],classname) Description
Y = rand returns a pseudorandom,
Description scalar value drawn from a uniform distribution
Y = eye(n) returns the n-by-n identity matrix. on the unit interval. ......... See Matlab docs
Y = eye(m,n) or eye([m,n]) returns an m-by-n Example
matrix with 1's on the diagonal and 0's Generate a 3-by-4 pseudorandom matrix:
elsewhere. ......... See Matlab docs for details R = rand(3,4)
R=
Example 0.8147 0.9134 0.2785 0.9649
x = eye(2,3,'int8'); 0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706

2
4/29/2010

More examples on rand More examples on rand


Example 2
Make a random choice between two Example 4
equally probable alternatives: Generate a uniform distribution of random
if rand < .5 numbers on a specified interval [a,b].
'heads' To do this, multiply the output of rand by (b-a),
else then add a. For example, to generate a 5-by-5
'tails' array of uniformly distributed random numbers
end on the interval [10,50],
Example 3
Generate uniform integers on the set 1:n. a = 10; b = 50;
n = 75; f = ceil(n.*rand(100,1)); x = a + (b-a) * rand(5)
f(1:7)
ans = x=
72 19.1591 49.8454 10.1854 25.9913 17.2739
37 46.5335 13.1270 40.9964 20.3948 20.5521
16.0951 27.7071 42.6921 42.0027 15.8216
61
43.0327 14.2661 44.7478 27.2566 15.4427
11 31.5337 48.4759 13.3774 46.4259 44.7717
32
69
60

Polynomial functions
conv deconv
Deconvolution and polynomial division
Convolution and polynomial multiplication
Syntax
[q,r] = deconv(v,u)
Syntax
Description
w = conv(u,v)
[q,r] = deconv(v,u) deconvolves vector u out
of vector v, using long division. The quotient
Description
is returned in vector q and the remainder in
w = conv(u,v) convolves vectors u and v.
vector r such that v = conv(u,q)+r .If u and v
Algebraically, convolution is the same
are vectors of polynomial coefficients,
operation as multiplying the polynomials
convolving them is equivalent to multiplying
whose coefficients are the elements of u
the two polynomials, and deconvolution is
and v. ......... See Matlab docs for details
polynomial division. The result of dividing v
by u isquotient q and remainder r.
Example
z=conv([ 1 1], [1 1]) Example if z =[ 1 0 -1 ]
z= [x,r] = deconv(z,[1 1])
1 2 1 x=
z=conv([ 1 1], [1 -1]) 1 -1
z= r=
1 0 -1 0 0 0

3
4/29/2010

Polynomial functions
polyval
poly Polynomial evaluation
Polynomial with specified roots Syntax
Syntax y = polyval(p,x)
p = poly(r) y = polyval(p,x,[],mu) ......
p = poly(A) Description
Description y = polyval(p,x) returns the value
p = poly(r) where r is a vector returns a row of a polynomial of degree n evaluated at x.
vector whose elements are the coefficients The input argument p is a vector of length
of the polynomial whose roots are the n+1 whose elements are the coefficients in
elements of r.......... See Matlab docs for p = descending powers of the polynomial to be
poly(A) ....will be used in 3rd yr control evaluated.
course!!
. ......... See Matlab docs for details
Example Example
p =[ 1 -6 -72 -27] The polynomial 3x2+2x+1 is evaluated at
r = roots(p) = 5, 7, 9 and 11.
r= x =[5 7 9 11]; p = [3 2 1];
12.1229 y= polyval(p, x)
-5.7345 y=
-0.3884 86 162 262 386
p1=poly(r) plot (x,y)

Cell Arrays

4
4/29/2010

Cell Arrays cells

 1 2  
A cell array is a generalization to the concept
of normal arrays in which each element is a    2 10
 4 5 
1 3 6
bin, or cell, which can contain an array or
100 
class .

Example,
Suppose you want to create a 2 x 2 cell array A,  Walden Pond June 13, 1997 
whose cells contain the location, the date, the  
air temperature (measured at 8 A.M. , 12  
noon, and 5 P.M.), and the water temperatures  55 56 57  
measured at the same time in three different  60 72 65 54 57 59 
points in a pond. The cell array looks like   
the following.  55 57 58 
A (1 , 1) = { ' Walden Pond ' } ;
A (1 , 2) = { ' June 13 , 1997 ' } ; Try commands
A (2 , 1) = { [60 , 72 , 65] } ; celldisp(A), iscell(A)
A (2 , 2) = { [55 , 57 , 56 ; 54 , 56 , 55 ; 52 , 55 , 53] } ;

Using Cell Arrays


A(1,1)
ans =
' Walden Pond '
>> whos ans
Name Size Bytes Class Attributes
ans 1x1 86 cell

>> A{1,1} ‫الخلية‬ ‫إلستدعاء محتويات‬


ans = { } ‫استخدم األقواس‬
Walden Pond
>> whos ans
Name Size Bytes Class Attributes
ans 1x13 26 char

5
4/29/2010

Using Cell Arrays


Suppose:
H = { [2 , 4 , 8], [6 , -8 , 3 ], [2 : 6], [9 , 2 , 5] } ;

J = [H{1} ; H{2} ; H{4}]

J=
2 4 8
6 -8 3
9 2 5

Structure Arrays

6
4/29/2010

Structure Arrays
Structure arrays are composed of structures. This class of arrays
enables you to store similar arrays together. The elements in structures
are accessed using filed name. This feature distinguishes them from
cell arrays, which are accessed using the standard array indexing
operations.

Structure array "student"

Student 1 Student 2

Name: Mohamed Ali Name: Salama Ali

SSN: 1297 SSN: 1298

Email: Mohd@server.com Email: salama@server.com

Tests: 76, 86, 90 Tests: 79,67, 95

Structure Arrays
Student.name = 'Mohamed Ali '; Student (2).name = 'Salama Ali' ;
Student.SSN = '1297'; Student (2).SSN = '1298' ;
Student.email ='Mohd@server.com'; Student (2).email = 'salama@server.com ' ;
Student.tests = [76, 86, 90]; Student(2).tests = [79,67, 95];

>> student =
1x2 struct array with fields:
name
SSN
email
tests
>> student(1)
ans =
name: 'Mohamed Ali '
SSN: '1297 '
email: ' Mohd@server.com'
tests: [76 86 90]

7
4/29/2010

Functions and Files

Functions and Files


• Working with Data Files

• Elementary Mathematical Functions ( sin, cos , tan, asin, acos, atan, log,
log10, exp, sqrt, sinh, cosh, tanh, asinh,acosh, atanh, .....) investigate
your self
• User-Defined Functions
• Advanced Function Programming

8
‫‪4/29/2010‬‬

‫‪Files & Functions‬‬


‫أ‪-‬انتعامم مع املههفاث‬

‫‪ -1‬أنىاع املهفاث انشائعت هى‪:‬‬


‫•يهفاخ انربايط و ًْ انىت ذُرهً تاإليرذاد ‪ m‬و حترىي ػهً أوايش انربَايط انزي صيرى ذُفيزِ تىاصغح‬
‫ال ‪matlab‬‬
‫•يهفاخ انثياَاخ ( ادلُغرياخ) وذُرهً تاإليرذاد ‪ mat‬وحترىي ػهً ادلرغرياخ خمزَح تغشيقح يضغىعح و‬
‫خاصح تال ‪matlab‬‬
‫• يهفاخ تياَاخ (يرغشاخ) ىف صىسج َصيح (‪ )text formatted‬وذأخز أي أيرذاد يُاصة و‬
‫غانثا يا يكىٌ (‪)txt‬‬
‫•يهفاخ ال ( ‪ )mdl‬وًْ ادلهفاخ اند حترىي ػهً منىرض تاصرخذاو يكىيُاخ ادلُظىيح ادلشصىيح‬
‫تىاظهح انرشغيم ‪Simulink‬‬

‫‪Files‬‬
‫‪-2‬أوامز انتخزين املختهفت‬
‫•ختزيٍ َافزج األوايش (‪ )Command Window‬يرى تاصرخذاو األيش‬
‫‪>> diary filename.ext‬‬
‫حيس يفرح يهف تاإلصى ادلكرىب ويضعم مجيغ األوايش انرانيح و سد انربَايط ػهيها ىف ْزا ادلهف‬
‫إليقاف انرخزيٍ يرى تكراتح األيش‬
‫‪>> diary off‬‬
‫و إلػادج انرخزيٍ يشج أخشي تضرذو األيش‬
‫‪>> diary on‬‬
‫‪save‬‬
‫•ختزيٍ ادلرغرياخ و يرى تىاصغح األيش ‪ save‬كانراىل‬
‫‪save filename‬‬
‫‪save filename keywords‬‬

‫‪load‬‬ ‫تيًُا يرى اصرشظاع انثياَاخ تاصرخذاو األيش ‪ load‬كانراىل‬


‫‪load filename‬‬
‫‪load filename.ext‬‬

‫كًا ميكٍ ختزيٍ ادلرغشاخ انىت ىف حيز انؼًم ‪ workspace‬ورنك تاصرخذاو أوايش انقائًح‬
‫‪File -> Save Worksapce As‬‬
‫• ميكٍ حتًيم تياَاخ ىف صىسج َصيح أيضا ػٍ عشيق اصرخذاو ال ‪ Import Wizard‬يٍ قائًح ادلهف‬
‫‪File -> Import Data‬‬

‫‪9‬‬
‫‪4/29/2010‬‬

‫‪Files‬‬
‫‪-3‬انتحكم ىف إدخال انبياناث‬
‫يرى تاصرخذاو األيش ‪ disp‬و األيش ‪ input‬كًا يهً‬
‫)‪disp(A‬‬
‫)'‪disp('text‬‬
‫‪fprintf‬‬
‫)'‪x = input ('text‬‬
‫)'‪x = input('text','s‬‬
‫)‪k= menu('title', 'op1' op2', ....‬‬

‫‪ -4‬انتحكم ىف طزق إظهار انبياناث‬


‫و يرى يضرخذاو األيش ‪ format‬كانراىل ‪:‬‬
‫‪format‬‬ ‫‪short‬‬
‫‪format‬‬ ‫‪long‬‬
‫‪format‬‬ ‫‪short e‬‬
‫‪format‬‬ ‫‪long e‬‬
‫‪format‬‬ ‫‪bank‬‬
‫‪format‬‬ ‫‪+‬‬
‫‪format‬‬ ‫‪rat‬‬
‫‪format‬‬ ‫‪loose‬‬

‫‪M-Files Functions and Scripts‬‬

‫‪ -6‬أنىاع مهفاث ال ‪M-files‬‬


‫•يهف حيرىي ػهً ذضهضم يٍ أوايش ال ‪ matlab‬فقظ و يؼذ كربَايط قاتم نهرُفيذ يثاششج‬
‫‪Script file‬‬
‫• يهف يثذأ ترؼشتف دانح ‪ matlab‬مث أوايش يرُىػح نرُفيز ْذف يؼني و يثؼذ ْزا ادلهف مبصايح أيش‬
‫ظذيذ ‪ function‬ميكٍ ذُفيزِ يٍ تشَايط ال ‪matlab‬‬

‫‪10‬‬
4/29/2010

Matlab functions
matlab ‫كيفيت بناء دوال ال‬
‫ نتعزيف انذانت‬-1
‫ذىضغ انذانح ىف انشكم انراىل‬
function [ Ouptut variables] = function_name ( Input variables)
‫ يضًً ادلهف تُفش اصى انذانح‬
‫ميكٍ نهذانح إسظاع أكصش يٍ َريعح ترؼشيف ػذد يٍ يرغرياخ اخلشوض تني األقىاس ادلشتؼح‬

Examples
square.m ‫دانح حلضاب يضاحح ادلشتغ‬: 1 ‫مثال‬
function area = square (side)
area = side^2;

circle.m ‫ دانح حلضاب يضاحح و حميظ دائشج‬:2 ‫مثال‬


function [area, circumference] = circle (radius)
area = pi*radius ^2;
circumference = 2*pi*radius;

rectang.m ‫دانح حلضاب يضاحح و حميظ يضرغيم‬: 3 ‫مثال‬


function [Rarea, Rcircumference] = rectang (Rlength, Rwidth)
Rarea = Rlength*Rwidth;
Rcircumference = 2*(Rlength+Rwidth);

11
‫‪4/29/2010‬‬

‫مثال‬

‫‪Files‬‬ ‫مثال‬
‫أسصى دانح يُحىن اجلية يٍ صفش إىل ‪ 2‬ط تاصرخذاو ‪َ20‬قغح ػهً األقم و اخرش ػاليح دميزج‬
‫‪ o,*,+‬نُقاط انثياَاخ ػهً سمسك يٍ تني انؼالياخ‬
‫‪Filename: example1.m‬‬

‫;)'*'‪k= menu('Choose a marker', 'o','+',‬‬


‫;]'‪type=['o','*','+‬‬
‫;‪x=0:pi/20:2*pi‬‬
‫;)‪y=sin(x‬‬
‫))‪plot(x, y, x, y, type(k‬‬
‫‪grid‬‬

‫‪ -5‬أونىيت تنفيذ األوامز ىف بزنامج ال ‪matlab‬‬


‫ػُذ كراتح أصى ىف َافزج األوايش فإٌ تشَايط ال ‪ matlab‬يقىو انرأكذ يٍ يؼشفرّ ذلزا األصى عثقا ألونىياخ‬
‫انرانيح‬
‫•ْم ْزا األصى ْى يرغري ؟‬
‫•ْم ْزا األصى ْى أصى أيش ؟‬
‫•ْم ْزا األصى ْى أصى يهف قاتم نهرُفيذ () ىف اجملهذ احلاىل؟‬
‫•ْم ْزا األصى ْى أصى يهف قاتم نهرُفيذ () ىف اجملهذاخ ادلؼشفح صاتقا ىف ادلضاس ؟‬

‫‪Selected functions‬‬
‫بعض انذوال املشهىرة و املفيذة‬
‫‪ -1‬دانح إجياد ذقاعغ يؼادنح يغ خظ انصفش (أي حم ادلؼادنح) ")‪"fzero('function_name',number‬‬
‫تانضاتق كاٌ ميكُك إجياد ظزوس يؼادنح حذيح تاصرخذاو األيش ) ]‪ roots( [polynomial vector‬نكٍ ػُذيا ذكىٌ ادلؼادنح غري حذيح ( ‪)non-polynomial‬‬
‫فإٌ األيش يضرهزو إجياد ذقاعؼها يغ انصفش‪.‬‬

‫أوالً‪ :‬يرى ذؼشيف انذانح ادلشاد إجياد ذقاعؼها يغ انصفش‬


‫)‪function y=f1(x‬‬
‫;‪y=x+2*exp(-x)-3‬‬
‫شاَياً‪ :‬يرى اصرذػاء انذانح ( ‪ )fzero('function_name', number‬فرقىو تانثحس ػٍ أفشب ذقاعغ يغ انصفش ىف يُغقح انشقى ادلؼغً كانراىل‪:‬‬
‫)‪>> fzero('f1',3‬‬
‫= ‪ans‬‬
‫‪2.8887‬‬
‫)‪>> fzero('f1',-.53‬‬
‫= ‪ans‬‬
‫‪-0.5831‬‬

‫‪12‬‬
4/29/2010

Selected functions
"fplot(function_name, [min, max]) " ‫ ميكُك سصى انذانح تاصرخذاو‬-2
>> fplot('f1',[-1,5])
>> grid

-2
-1 0 1 2 3 4 5
‫ انىت حتذز ػُذْا قيًح صغشي نهذانح ىف يُغقح يؼيُح َضرخذو األيش‬x ‫ إلجياد قيًح ال‬-3
"fminbnd('function name', min, max)"
>> fminbnd('f1',-1,5)
ans =
0.6932
‫ انصغشي‬y ‫نهحصىل ػهىقيًح‬
>> feval('f1',fminbnd('f1',-1,5))
ans =
-1.3069
>> x=fminbnd('0.025*x.^5-0.0625*x.^4-0.333*x.^3+x.^2', -1,4)
x=
2.0438e-006

13

You might also like