Professional Documents
Culture Documents
The file roots is written in your current working directory. It can be displayed by Matlab command type roots.
Script M-Files
Group of Matlab commands placed in a text file with a text editor. Matlab can open and execute the commands exactly as if they were entered at the Matlab prompt. The term script indicates that Matlab reads from the script found in the file. Also called M-files, as the filenames must end with the extension .m, e.g. example1.m.
Script M-Files
Script M-Files
Script M-Files
Example : Create the file named qroots.m in your present working directory using a text editor: %qroots:Quadratic root finding script format compact; a=1; b=5; c=6; x = -b/(2*a); y=sqrt(b^2-4*a*c)/(2*a); s1 = x+y s2 = x-y To execute the script M-file, simply type the name of the script file qroots at the Matlab prompt: >> qroots a = 1 b = 5 c = 43 6 s1 = -2 s2 = -3
5. You can use the type command to display an M-file without opening it with a text editor. For example, to view the file rqroot.m, the command is type rqroot.
Commands to manage this search path: matlabpath: Display search path. addpath dir: Add directory dir to beginning of matlabpath. If you create a directory to store your script and function M-files, you will want to add this directory to the search path. rmpath dir: Remove directory dir from the matlabpath.
Complex.
a+bi
Complex calculus
i 2= -1 )
Strings and characters follow the same rules as other matrices, with each character counting for one element.
Name
myname
0 or 1
which means:
Example:
false or true
Example:
>>A=true A= 1 >> whos A Name Size Bytes Class A 1x1 1 logical array
Relational Operators
Decision making uses comparison of logical variables
Comparison Is equal to
Example: >> A=1; B=2; >> A==B >> A>B >> A<B >> A>=B >> A<=B >> A~=B ans = ans = ans = ans = ans = ans = 0 0 1 0 1 1
==
>
< >= <= ~=
Is greater than
Is smaller than Is greater or equal to Is smaller or equal to Is not equal to
For example, suppose that x = [6,3,9] and y = [14,2,9]. The following MATLAB session shows some examples. >>z = (x < y) z = 1 0 0 >>z = (x ~= y) z = 1 1 0 >>z = (x > 8) z = 0 0 1
The relational operators can be used for array addressing. For example, with x = [6,3,9] and y = [14,2,9], typing z = x(x<y) finds all the elements in x that are less than the corresponding elements in y. The result is z = 6.
4-21
The arithmetic operators +, -, *, /, and \ have precedence over the relational operators. Thus the statement z = 5 > 2 + 7
is equivalent to
z = 5 >(2+7) and returns the result z = 0. We can use parentheses to change the order of precedence; for example, z = (5 > 2) + 7 evaluates to z = 8.
4-22
Accessing Arrays Using Logical Arrays When a logical array is used to address another array, it extracts from that array the elements in the locations where the logical array has 1s. So typing A(B), where B is a logical array of the same size as A, returns the values of A at the indices where B is 1.
4-25
Logical Operators
Format of COMPOUND Logical Expressions:
operation and
A 0 0 1 1
B 0 1 0 1
C= A|B 0 1 1 1
~(A|B) 1 0 0 0
&
| xor ~
Truth Table A 0 0 1 1 B 0 1 0 1
or or (exclusive) not
C= A&B 0 0 0 1
~(A&B)
1 1 1 0
A
0 0 1 1
B
0 1 0 1
C= xor(A,B)
~xor(A,B) 1 0 0 1
0 1 1 0
Logical Variables
Examples: >> A=1; B=2; >> (A==B) & (A>B) >> (A<B) & (A==B) >> (A==B) | (A>B) >> (A<B) | (A==B) ans = 0 ans = 0 ans = ans = ans = ans = 0 1 1 0
ans = ans =
0 1
>> (A>0) & (B>A) ans = 1 >> (A>0) & (B>A)&(B<0) ans = 0
First
Second
Third
Fourth Fifth
4-29
Logical functions:
Logical function all(x) Definition Returns a scalar, which is 1 if all the elements in the vector x are nonzero and 0 otherwise. Returns a row vector having the same number of columns as the matrix A and containing ones and zeros, depending on whether or not the corresponding column of A has all nonzero elements. Returns a scalar, which is 1 if any of the elements in the vector x is nonzero and 0 otherwise. Returns a row vector having the same number of columns as A and containing ones and zeros, depending on whether or not the corresponding column of the matrix A contains any nonzero elements. Returns an array of the same dimension as A with ones where the elements of A are finite and zeros elsewhere.
all(A)
any(x)
any(A)
finite(A)
4-30
Definition Returns a 1 if A is a character array and 0 otherwise. Returns a 1 if A is an empty matrix and 0 otherwise. Returns an array of the same dimension as A, with ones where A has inf and zeros elsewhere. Returns an array of the same dimension as A with ones where A has NaN and zeros elsewhere. (NaN stands for not a number, which means an undefined result.)
isinf(A)
isnan(A)
4-31
Returns a 1 if A is a numeric array and 0 otherwise. Returns a 1 if A has no elements with imaginary parts and 0 otherwise. Converts the elements of the array A into logical values.
xor(A,B)
Returns an array the same dimension as A and B; the new array has ones where either A or B is nonzero, but not both, and zeros where A and B are either both nonzero or both zero.
4-32
[u,v,w] = find(A)
4-33
Logical Operators and the find Function Consider the session >>x = [5, -3, 0, 0, 8];y = [2, 4, 0, 5, 7]; >>z = find(x&y) z = 1 2 5 Note that the find function returns the indices, and not the values.
4-34
Note that the find function returns the indices, and not the values. In the following session, note the difference between the result obtained by y(x&y) and the result obtained by find(x&y) in the previous slide. >>x = [5, -3, 0, 0, 8];y = [2, 4, 0, 5, 7]; >>values = y(x&y) values = 2 4 7 >>how_many = length(values) how_many = 3
4-35
Structured Programming
Structured Programming
Initialization
Sequential Programming
Initialization
Initialization Input
Input
Calculation
Calculation Results
Decision making
Calculation 1
Result 1
Result 2
Structured Programming
Format of if statement:
if
if Logical Expression Statements end
True Statement
False
if
True
Statement1
False
Statement2
Structured Programming
Example: iftest1.m
Initialization % Program to test the if statement #1
Input X
False
If X>=0
True
Calculate
>>iftest1 Enter value for x: 9 The squareroot of 9.00 is 3.0000 >>iftest1 Enter value for x: -2 >>
Display Result
End of script
Structured Programming
Example: iftest2.m
Initialization % Program to test the if statement #2 X=input(Enter value for x:); if X>=0 Y=sqrt(X); fprintf(The squareroot of %3.2f is %3.4f ,X,Y) else disp(x is negative: there is no real result) end
Input X
False
If X>=0
True
Calculate
Display NO Result
Display Result
End of script
>>iftest2 Enter value for x: 3 The squareroot of 9.00 is 3.0000 >>iftest2 Enter value for x: -2 x is negative: there is no real result >>
Structured Programming
if Logical Expression Statements 1 elseif Logical Expression Statements 2 else Statements 3 end
if
True
Statement1
False
elseif
True
Statement2
False
Statement3
Structured Programming
Example: iftest3.m
Initialization
Input X
False
If X>0
True False
% Program to test the if statement #3 X=input(Enter value for x:); if X>0 disp(x is positive); elseif X<0 disp(x is negative); else disp(x equal 0); end
If X<0
True
Display Result = 0
End of script
>>iftest3 Enter value for x: 3 x is positive >>iftest3 Enter value for x: -2 x is negative
Structured Programming
nesting
Problem:
Pick a random number N (-2<N<2) Calculate B=
log( N )
Statement2
Statement4
>> Testswitch
Example1
Write a script example.m to find roots of a second order equation ax2+bx+c=0. When the script is executed it will
ask the user enter the coefficients a,b,c
calculate discriminant
calculate the roots and display the case according to
sign of discriminant.
Example2
Write a script that allows a user to enter a string containing a day of a week (Sunday, Monday etc) uses a switch construct to convert the day to its corresponding number, where Monday is the first day of the week. Print out the resulting day number. Also be sure to handle the case of an illegal day name.