



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A set of lecture slides from cs1112, covering user-defined functions, vectorized arithmetic operations, and examples of matlab code. Topics include element-by-element arithmetic operations on arrays, shift, reciprocate, and converting from polar to cartesian coordinates. The slides also discuss the importance of user-defined functions, including their role in elevating reasoning, facilitating top-down design, and software management.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




September 25, 2008 Lecture 9 3
.* ./
A dot (. ) is necessary in front of these math operators
.^
September 25, 2008 Lecture 9 4
x
= z^5 4 3.5^11
September 25, 2008 Lecture 9 5
x
/ y
= z .5^12.
September 25, 2008 Lecture 9 6
./
A dot (.) is necessary in front of these math operators
/
.^ .^
The dot in .^ , .^ , ./ not necessary but OK
September 25, 2008 Lecture 9 7
−6 −4 −2 0 2 4 6
−
−
−
−
0
1
2
3
4
major axis = 5, minor axis = 3
Different methods based on different ways to “average” the major and minor axes
September 25, 2008 Lecture 9 10
September 25, 2008 Lecture 9 11
September 25, 2008 Lecture 9 14
θ
r (^) y
x
Polar coordinates Cartesian coordinates
September 25, 2008 Lecture 9 15
% Convert polar coordinates (r,theta) to % Cartesian coordinates (x,y). % theta is in degrees.
r= input(‘Enter radius: ’); theta= input(‘Enter angle in degrees: ’);
rads= thetapi/180; % radian x= rcos(rads); y= r*sin(rads);
September 25, 2008 Lecture 9 16
function [x, y] = polar2xy(r,theta) % Convert polar coordinates (r,theta) to % Cartesian coordinates (x,y). % theta is in degrees.
rads= thetapi/180; % radian x= rcos(rads); y= r*sin(rads);
r= input(‘Enter radius: ’); theta= input(‘Enter angle in degrees: ’);
rads= thetapi/180; % radian x= rcos(rads); y= r*sin(rads);
A function file polar2xy.m
(Part of) a script file
September 25, 2008 Lecture 9 25
2 2
September 25, 2008 Lecture 9 26
September 25, 2008 Lecture 9 27
function [x, y] = polar2xy(r,theta) % Convert polar coordinates (r,theta) to % Cartesian coordinates (x,y). % theta is in degrees.
rads= thetapi/180; % radian x= rcos(rads); y= r*sin(rads);
r= input(‘Enter radius: ’); theta= input(‘Enter angle in degrees: ’);
rads= thetapi/180; % radian x= rcos(rads); y= r*sin(rads);
A function file polar2xy.m
(Part of) a script file
September 25, 2008 Lecture 9 30
function [x, y] = polar2xy(r,theta)
Output parameter list
Function name (This file’s name is polar2xy.m)
Input parameter list
September 25, 2008 Lecture 9 32
Function header is the “contract” for how the function will be used (called)
You have this function:
Code to call the above function:
September 25, 2008 Lecture 9 33
Function header is the “contract” for how the function will be used (called)
You have this function:
Code to call the above function:
September 25, 2008 Lecture 9 34
General form of a user-defined function
function [ out1 , out2 , …]= functionName ( in1 , in2 , …) % 1-line comment to describe the function % Additional description of function
Executable code that at some point assigns values to output parameters out1 , out2 , …
in1 , in2 , … are defined when the function begins execution. Variables in1 , in2 , … are called function parameters and they hold the function arguments used when the function is invoked (called). out1 , out2 , … are not defined until the executable code in the function assigns values to them.
September 25, 2008 Lecture 9 35
function m = convertLength(ft,in) % Convert length from feet (ft) and inches (in) % to meters (m).
...
Given this function:
How many proper calls to convertLength are shown below?
E: 5 or 0
September 25, 2008 Lecture 9 37
*The path function gives greater flexibility. Not required in CS1112.