Professional Documents
Culture Documents
Special Matrices, Cells, Structures, Functions: Chapter 2 & 3
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');
2
4/29/2010
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
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] } ;
5
4/29/2010
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.
Student 1 Student 2
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
• 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
-2أوامز انتخزين املختهفت
•ختزيٍ َافزج األوايش ( )Command Windowيرى تاصرخذاو األيش
>> diary filename.ext
حيس يفرح يهف تاإلصى ادلكرىب ويضعم مجيغ األوايش انرانيح و سد انربَايط ػهيها ىف ْزا ادلهف
إليقاف انرخزيٍ يرى تكراتح األيش
>> diary off
و إلػادج انرخزيٍ يشج أخشي تضرذو األيش
>> diary on
save
•ختزيٍ ادلرغرياخ و يرى تىاصغح األيش saveكانراىل
save filename
save filename keywords
كًا ميكٍ ختزيٍ ادلرغشاخ انىت ىف حيز انؼًم 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', ....
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;
11
4/29/2010
مثال
Files مثال
أسصى دانح يُحىن اجلية يٍ صفش إىل 2ط تاصرخذاو َ20قغح ػهً األقم و اخرش ػاليح دميزج
o,*,+نُقاط انثياَاخ ػهً سمسك يٍ تني انؼالياخ
Filename: example1.m
Selected functions
بعض انذوال املشهىرة و املفيذة
-1دانح إجياد ذقاعغ يؼادنح يغ خظ انصفش (أي حم ادلؼادنح) ")"fzero('function_name',number
تانضاتق كاٌ ميكُك إجياد ظزوس يؼادنح حذيح تاصرخذاو األيش ) ] roots( [polynomial vectorنكٍ ػُذيا ذكىٌ ادلؼادنح غري حذيح ( )non-polynomial
فإٌ األيش يضرهزو إجياد ذقاعؼها يغ انصفش.
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