Sunteți pe pagina 1din 16

ENGFF012

Lesson 4
Basic User-Defined Functions
Contents
Write a simple user-defined function:
1. Sum of series
2. Arrays/Matrices of input arguments
3. Mathematical problems
Introduction
It is possible to code each sub-task as a separate function, and
each function can be tested and debugged independently of
all of the other sub-tasks in the program.
Benefits of basic user-defined functions:
1. Independent testing of sub-tasks - The sub-task can be tested
separately to ensure that it performs properly by itself before it is
integrated into the larger program.
2. Reusable code - In many cases, the same basic sub-task is needed in
many parts of a program.
3. Isolation from unintended side effects - Each function has its own
workspace with its own variables, which is independent of all other
functions and of the calling program.
M-files
Script files
• All of the M-files that we have seen so far have been
script files.
• Script files are just collections of MATLAB statements
that are stored in a file.
• When a script file is executed, the result is the same as it
would be if all of the commands had been typed directly
into the Command Window.
• Script files share the Command Window’s workspace
(i.e. variables).
Example of script file (example1.m)
% script file Command Window
>> example1
a=6;
b=3;
c=2;
sum=a+b+c
product=a*b*c
M-files
Function
• A MATLAB function is a special type of M-file that runs in its
own independent workspace.
• It receives input data through an input argument list and
returns results to the caller through an output argument list.
• Each ordinary MATLAB function should be placed in a file with
the same name (including capitalization) as the function
along with the file extension “.m”. For example, if a function is
named my_fun, that function should be placed in a file named
my_fun.m
Example of function file (my_fun.m)
% function file
function[total,multiplication]=my_fun(a,b,c)
total=a+b+c
multiplication=a*b*c
end
3 different ways to invoke my_fun
>> a=6; >> x=6;
>> b=3; >> y=3;
>> c=2; >> z=2;
>> [total,multiplication]=my_fun(a,b,c) >> [t,m]=my_fun(x,y,z)
A function can be invoked by typing its >> [t1,t2]=my_fun(6,3,2)
name directly in the Command Window.
Output argument list:
placeholders for the
Input argument list:
values returned to the
values that will be
caller when the function
passed from the
finishes executing.
caller to the function.

% function file
function[total,multiplication]=my_fun(a,b,c)
total=a+b+c
multiplication=a*b*c
end The use of an end statement to terminate a
function is a new feature of MATLAB. It is optional.
Example: Arrays of input arguments
% function that multiplies an array of numbers
function[a]=arrayproduct(x,y)
a=x.*y;
end
Command Window
>> m=[2 3 7 9];
>> n=[4 2 1 2];
The answer:
>> g=arrayproduct(m,n) g=8 6 7 18
Exercise: Matrices of input arguments
Write a function that calculates matrix multiplication.
% Matrix multiplication
function[a]=matrixproduct(x,y)
a=x*y;
end
Command Window
>> m=[2 3;7 9]; The answer:
>> n=[4 2;1 2]; g=
>> g=matrixproduct(m,n) 11 10
37 32
Example: Sum of series
Write a function that calculates the sum of
the series: s = 1 + 2 + 3 + 4 + …… + n

function[total]=sumseries(n);
a=1:1:n; %Create array a =[1 2 3 ....n]
total=sum(a); %Sum up all elements in the array a
end
Exercise
Write a function that calculates the sum of the series:
S = 1 + 1/2 + 1/3 +1/4 + ……. + 1/n
Solution
function[a]=sumrecip(n)
x=1:1:n; %Creates array x=[1 2 3 4 ...n]
x=1./x; %x now becomes x=[1/1 1/2 1/3...1/n]
a=sum(x); %Sum elements of x
end
Example: Triangle
Given the lengths of the three sides of a
triangle. Write a function that calculates
the three angles, expressed in degrees.
If a, b and c are the lengths of the legs of a triangle
opposite to the angles A, B and C respectively; then the
law of cosines states:
a2 = c2 + b2 - 2bc cos A  A = cos-1 [ ( b2 + c2 - a2 ) / 2bc]
b2 = a2 + c2 - 2ca cos B  B = cos-1 [ ( c2 + a2 - b2 ) / 2ca]
c2 = b2 + a2 - 2ab cos C  C = cos-1 [ ( a2 + b2 - c2 ) / 2ab]
function[A,B,C]=find_angles(a,b,c)
%A, B, C are the angles. a, b, c are the lengths of the sides
Aradian=acos((b^2+c^2-a^2)/(2*b*c)); % angle A in radian
A=Aradian*180/pi; %convert angle A in radian to degree
Bradian=acos((a^2+c^2-b^2)/(2*a*c)); % angle B in radian
B=Bradian*180/pi; %convert angle B in radian to degree
Cradian=acos((a^2+b^2-c^2)/(2*a*b)); % angle C in radian
C=Cradian*180/pi; %convert angle C in radian to degree
end
acos(x) returns the Inverse Cosine
(cos-1) of the elements of x.
To test this function, go to the command window:
>> [p,q,r]=find_angles(2,2,2)
p = 60
q = 60
r = 60
>> [x,y,z]=find_angles(sqrt(2),1,1)
x = 90
y = 45
z = 45
Contents
Write a simple user-defined function:
1. Sum of series – use of sum()
2. Arrays/Matrices of input arguments
- A.*B or A*B
3. Mathematical problems – use of
acos()

S-ar putea să vă placă și