User Functions & Vectorized Arithmetic in CS1112 Lecture 9 (2008/9/25), Study notes of Computer Science

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-7cu
koofers-user-7cu 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 9 2008/9/25
Lecture slides 1
Previous Lecture:
Finite/inexact arithmetic
Discrete vs. continuous
Today’s Lecture:
User-defined functions
Announcement:
Prelim 1 tonight at 7:30pm
September 25, 2008 Lecture 9 3
Vectorized
element-by-element arithmetic operations
on arrays
+
-
.*
./
A dot (.) is necessary in front of these math operators
.^
September 25, 2008 Lecture 9 4
Shift
2 8.51
x
y
+
5113.54
z=
Matlab code: z= x + y
3
September 25, 2008 Lecture 9 5
Reciprocate
2 8.51
x
y
/
.5 .125
21
z=
Matlab code: z= x ./ y
1
September 25, 2008 Lecture 9 6
./
A dot (.) is necessary in front of these math operators
Vectorized
element-by-element arithmetic operations between an
array and a scalar
+
-
*
/
+
-
*
.^ .^
.*.*./
The dot in not necessary but OK,,
September 25, 2008 Lecture 9 7
−6 −4 −2 0 2 4 6
−4
−3
−2
−1
0
1
2
3
4
major axis = 5, minor axis = 3
Estimate the perimeter of an ellipse
Different methods
based on different
ways to “average”
the major and
minor axes
pf3
pf4
pf5

Partial preview of the text

Download User Functions & Vectorized Arithmetic in CS1112 Lecture 9 (2008/9/25) and more Study notes Computer Science in PDF only on Docsity!

„ Previous Lecture:

„ Finite/inexact arithmetic

„ Discrete vs. continuous

„ Today’s Lecture:

„ User-defined functions

„ Announcement:

„ Prelim 1 tonight at 7:30pm

September 25, 2008 Lecture 9 3

Vectorized

element-by-element arithmetic operations

on arrays

.* ./

A dot (. ) is necessary in front of these math operators

.^

September 25, 2008 Lecture 9 4

Shift

x

  • y

= z^5 4 3.5^11

Matlab code: z= x + y

September 25, 2008 Lecture 9 5

Reciprocate

x

/ y

= z .5^12.

Matlab code: z= x ./ y

September 25, 2008 Lecture 9 6

./

A dot (.) is necessary in front of these math operators

Vectorized

element-by-element arithmetic operations between an

array and a scalar

/

.^ .^

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

Estimate the perimeter of an ellipse

Different methods based on different ways to “average” the major and minor axes

September 25, 2008 Lecture 9 10

How many errors are there in the following

statement given that

x = linspace(0,1,100)?

Y = (3*x .+ 1)/(1 + x^2)

A: 1 B: 2 C: 3 D: 4

September 25, 2008 Lecture 9 11

Plotting an Ellipse

⎟^ =

b

y

a

x

( a cos( t ), b sin( t ))

Easier representation for plotting:

0 <= t <= 2 π

September 25, 2008 Lecture 9 14

Convert from polar to Cartesian coordinates

θ

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

dotsScript.m

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

Software Management

Today:

I write a function

EPerimeter(a,b)

that computes the perimeter of the ellipse

2 2

b

y

a

x

September 25, 2008 Lecture 9 26

Software Management

During this year :

You write software that makes extensive use of

EPerimeter(a,b)

Imagine hundreds of programs each with several

lines that reference EPerimeter

September 25, 2008 Lecture 9 27

Software Management

Next year:

I discover a more efficient way to approximate

ellipse perimeters. I change the implementation of

EPerimeter(a,b)

You do not have to change your software at all.

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)

function [x, y] = polar2xy(r, theta)

% Convert polar coordinates (r, theta) to

% Cartesian coordinates (x,y). Theta in degrees.

% Convert polar (r1,t1) to Cartesian (x1,y1)

r1= 1; t1= 30;

[x1, y1]= polar2xy(r1, t1);

plot(x1, y1, ‘*’)

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)

function [x, y] = polar2xy(r, theta)

% Convert polar coordinates (r,theta) to

% Cartesian coordinates (x,y). Theta in degrees.

% Convert polar (r1,t1) to Cartesian (x1,y1)

r1= 1; t1= 30;

[x1, y1]= polar2xy(r1, t1);

plot(x1, y1, ‘*’)

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

Comments in functions

„ Block of comments after the function header is

printed whenever a user types help

functionName at the Command Window

„ The 1st^ line of this comment block is searched

whenever a user types lookfor someWord at

the Command Window

„ Every function should have a comment block

after the function header:

„ 1 st^ line succinctly describes what the function does

„ Additional lines for more detail, if necessary

f= …; n= …;

d= convertLength(f,n);

d= convertLength(f*12+n);

d= convertLength(f+n/12);

x= min(convertLength(f,n), 1);

y= convertLength(pi*(f+n/12)^2);

A: 1 B: 2 C: 3 D: 4

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

dotsInCircles.m

(functions with multiple input parameters)

(functions with a single output parameter)

(functions with multiple output parameters)

(functions with no output parameter)

Accessing your functions

For now*, put your related functions and scripts

in the same directory.

dotsInCircles.m

randDouble.m

polar2xy.m

drawColorDot.m

*The path function gives greater flexibility. Not required in CS1112.

MyDirectory

Any script/function that

calls polar2xy.m