06_GENN004m_MATLABPrograms
-
Upload
bishoy-emile -
Category
Documents
-
view
221 -
download
0
Transcript of 06_GENN004m_MATLABPrograms
-
7/29/2019 06_GENN004m_MATLABPrograms
1/6
GENN004: Introduction to Computers 09-Feb-
MATLAB Programs
MATLAB Programs
Programmer-Defined Functions
Outline
The need for repetition
Counted Loops (for)
Conditional Loops (while)
-
7/29/2019 06_GENN004m_MATLABPrograms
2/6
GENN004: Introduction to Computers 09-Feb-
MATLAB Programs
Custom (User-Defined) Functions
So far we have used MATLABs built-in functions
rem, fix, sin, cos, etc
We have created programs that use various functions
We will now learn how to create our own functions
We will see that MATLAB functions follow very nicelythe steps of Top-Down Design
Define the function description text
Define the inputs and outputs function imprint
Define the algorithm that leads from input to output function body
Simple Custom Function
Functions are contained in M-files
The function name and file name should match
so the function distance should be in distance.m
For help about the distance function use
>> help distance
Now in matlab I can use it the same way as the built-in function
>> D=distance(0,0,10,10);
>> disp(D)
14.1060
function dist = distance(x1, y1, x2, y2)
%distance calculates the distance between two points (x1,y1) and (x2, y2)
dist = sqrt((x2-x1)^2+(y2-y1)^2);
end
-
7/29/2019 06_GENN004m_MATLABPrograms
3/6
GENN004: Introduction to Computers 09-Feb-
MATLAB Programs
Simple Custom Functions
function deg = degree2rad(x)
%degree2rad converts the input degree x into radians
deg=x*pi/180;
end
function rad = rad2degree(x)
%rad2degree converts the input radians x into degrees
rad=x*180/pi;
end
function f = fact(x)
%fact calculates the factorial of x
f=1;for i=1:x
f=f*i;
end
end
Custom Functions with more returnsfunction [large, small] = sort2(x,y)
%sort2 compares the two inputs and return them sorted, larger first.
if x>=y
large=x;
small=y;
else
large=y;
small=x;
end
end
>> [x,w]=sort2(5,10)
x =
10
w =
5
>> [x,w]=sort2(10,5)
x =
10
w =
5
-
7/29/2019 06_GENN004m_MATLABPrograms
4/6
GENN004: Introduction to Computers 09-Feb-
MATLAB Programs
Error Check Inputs
function x = correctInput(msg,low,high)
% correctInput prompt the user using msg for an input and
% error check to make sure it is >= low and > x=correctInput('enter num:',0,10);
enter num:-5
Error! enter num:11
Error! enter num:4>>
>> x
x =
4
Custom (User-Defined) Functions
function [out_arg1, out_arg2, ]
= fname(in_arg1, in_arg2, )
The word function is a keyword.
[out_arg1, out_arg2, ] is the output
argument list
fname is the name of the function
(in_arg1, in_arg2, ) is the input argumentlist
in_arg1, in_arg2, etc. are called dummy arguments
because they are filled will values when the function is
called
-
7/29/2019 06_GENN004m_MATLABPrograms
5/6
GENN004: Introduction to Computers 09-Feb-
MATLAB Programs
Script Vs. Function
Script
A script is executed line-
byline just as if you are
typing it into the Command
Window
The value of a variable in a
script is stored in the
Command Window
Workspace
Function
A function has its own
private (local) function
workspace that does not
interact with the workspace
of other functions or the
Command Window
workspace
Variables are not shared
between workspaces even ifthey have the same name
Local WorkpaceScript
w=5;
y=fact(w);
disp(y);
Function
function f = fact(x)
%fact calculates the factorial of x
f=1;
for n=1:x
f=f*n;
end
end
Command Window Workspace
contains w and y.
So f, x, and n are not known here
Function fact Workspace contains f, x, and n
When a function reaches the end of execution (and returns the output argument), the
function spacelocal spaceis deleted.
-
7/29/2019 06_GENN004m_MATLABPrograms
6/6
GENN004: Introduction to Computers 09-Feb-
MATLAB Programs
Persistent Variable
The variable count ispersistent
To clear it use>> clear functions
function func2
% func2 increments a persistent variable "count"
persistent count
if isempty(count)
count = 0;
end
count =count + 1;
fprintf('The value of count is %d\n',count);
end
>> func2
The value of count is 1
>> func2
The value of count is 2
>> func2
The value of count is 3
Use Functions for Clean and Organized
Code (Modular Programs)
x1=correctInput('Enter x1 from 0 to 10:',0,10);
y1=correctInput('Enter y1 from 0 to 10:',0,10);
x2=correctInput('Enter x2 from 0 to 10:',0,10);
y2=correctInput('Enter y2 from 0 to 10:',0,10);
d=distance(x1,y1,x2,y2);
fprintf('distance = %.2f\n',d);
m=correctInput('Enter math grade from 0 to 100:',0,100);
s=correctInput('Enter science grade from 0 to 100:',0,100);
e=correctInput('Enter englishgrade from 0 to 150:',0,150);
mr=grade2rank(m); % A function to convert grade numeric to alphanumeric
sr=grade2rank(s);
er=grade2rank(e);
fprintf('You got %s in math\n',mr);
fprintf('You got %s in science\n',sr);
fprintf('You got %s in english\n',er);