Professional Documents
Culture Documents
Introduction To Matlab Tutorial 7 PDF
Introduction To Matlab Tutorial 7 PDF
Download:
(http://www.weizmann.ac.il/feinberg/courses/EranEden/course_outline.htm)
Tutorial 7:
Functions and Program Design
Introduction to Matlab
& Data Analysis
Local functions
Debugger
Recursion
More:
Introduction
Functions M file structure
Functions workspace
Functions Input and output
Goals
90
95
100
98
88
92
91.85
Output:
(Data)
643
100
4
X = 80
Functions:
function
Input
Output
5
5
my_sum = a+b;
sumTwoNums.m:
The output variables (if there are few use []: [out1 out2] )
Testing for
proper
variables
Examples
Output
Input
Usage
help:
my_sum = a+b;
if (~isscalar(a))
error('First argument is not a scalar');
end
if (~isscalar(a))
error('Second argument is not a scalar');
end
Functions Documentation
and Variable Verification
=
=
=
=
=
1;
2;
3;
4;
sumTwoNums(x, y)
a
b
x
y
s
s=7
y=4
X=3
b=2
a=1
Workspace:
Matlab
my_sum = 7
b=4
a=3
Function
Workspace:
ax + bx + c
y = a*x.^2 + b*x + c;
Switch according
to input arguments
number
= calSecondOrderPoly(x, a, b, c)
switch nargin
case 4
% do nothing
case 3
Default value
c = 0;
case 2
c = 0;
b = 0;
otherwise
error('Incorrect input');
end
function y
Recall:
[ax 2 + bx + c,2ax + b]
10
if nargout == 2
y_derivative = 2*a*x + b;
end
y = a*x.^2 + b*x + c;
Checks number of
output arguments
11
12
Problem specifications:
13
Display the
game matrix
Update the
game matrix
Initiate the
game matrix
Have some
game matrix
Get row
Check whether
the move is legal
Get column
Play Tic-Tac-Toe
14
NaN
NaN
2
NaN
NaN
NaN
NaN
NaN
15
Play Tic-Tac-Toe
16
Get row
Get column
Check if it is
a legal move
about the
program status
A variable which
holds information
Flags
edit playTicTacToe.m;
17
Red
Gray
Modifying a file
Debug menu
Stop if errors / warn
Debug buttons
Our weapons:
Break points
To the code!
18
Runtime errors
Syntax errors
Error
end
disp(Caught);
func3();
B = A(1,2);
A = ones(1,1);
func4 ()
func4()
func3()
Catch
Try
func2()
func2()
func1()
func1;
19
The factorial of 1 is 1!
Ah ha!
if (x == 0 || x == 1)
res = 1;
else
res = x * myFactorial(x-1);
end
20
Local functions
Debugger
Recursion
More:
Introduction
Functions M file structure
Functions workspace
Functions Input and output
Summary
21