Class Notes (943,459)
US (364,635)
U of M (6,208)
ENGR (61)
ENGR 101 (50)
Lecture 1

ENGR 101 Lecture 1: Functions

5 Pages
21 Views

Department
Engineering
Course Code
ENGR 101
Professor
Jeffrey Ringenberg

This preview shows pages 1-2. Sign up to view the full 5 pages of the document.
'whos'&lists%in%alphabetical%order%the%names,%sizes,%and%types%of%all%the%variables%
in%the%workspace.%
'char'&(character)&refers%to%the%letters,%single%quotes%('')%
Strings refer%to%words%("")%
ones(m,n)%creates%a%m%*%n%matrix%of%zeroes%
ones(m)%same,%but%uses%m%for%both%rows%and%columns%
Each%column%sum%=%each%row%sum%=%each%diagonal%sum%
o
Creates%a%new%matrix%based%on%repeating%matrix%A%a%certain%number%of%times%in%
the%x%and%y%direction
o
repmat(A,%xTimes,%yTimes)%
Creates%a%new%matrix%with%the%same%elements%as%A,%but%the%requested%number%
of%rows%and%columns.%The%dimension%must%match%with%the%original%number%of%
elements%in%A.%
o
reshape(A,%numRows,%numCols)%
Numel(x)%yields%the%#%of%elements%in%x%
Length(x)%yields%the%number%of%elements%along%the%longest%dimension%of%x%
Size(x)%yields%a%vector%with%the%#%of%elements%along%each%dimension%of%x%
"Array%operations"%work%with%array%element-by-element
Vector%indexing
Bad%idea
o
Code%duplication%occurs%when%you%have%more%than%one%of%code%that%does%the%same%
thing
It's%an%algorithm%because%we%can%use%it%without%having%to%worry%about%
what's%inside.%
Is%an%algorithm%that%returns%data%to%the%caller,%and%operates%independently%of%
the%caller.%
o
Ex.%sqrt%function.%
o
The%interface%of%a%functions%describes%how%we%use%it
o
Functions&
Ex.%sum%function%
o
Many%work%column%by%column:%
o
Functions%with%matrices:
Name%-what%is%the%function%called?
§
Parameters%-variables%used%to%pass%dat%into%the%function%as%input
§
Return%variables%-a%list%of%variables%whose%values%will%be%returned%
back%to%the%outside%word%as%the%output%of%a%fucntion
§
Defines%how%we%interact%with%the%funciton%
An interface that%specifies%how%it%must%be%used%by%other%code
o
A%function%call%takes%in%arguments%that%correspond%to%the%parameters%of%the%
function
o
The%arguments%could%be%expressions,%they%do%not%have%variables
o
We%write%code%that%uses%the%parameters%to%calculate%
An implementation with%code%that%makes%it%work%behind%the%scenes
o
Every%function%has%two%major%pieces:
Code%is%vectorized%if%it%can%work%with%vectors%of%data%just%as%well%as%scalars
It%uses%a%compound%return%to%give%us%both%a%sorted%version%of%the%vector%and%a%
vector%of%sorted%indices.%
o
Write%"ascend"%or%"descend"
o
The%sort&function&works%with%column%vectors
Ex.%
K%=%find(X)%-returns%the%indices%of%the%array%x%that%point%to%nonzero%elements.%If%
non%is%found,%find%returns%an%empty%matrix.%
o
The%find&function&converts%a%logical%index%matrix%into%a%regular%index%matrix.%
A = [1,0,4,5,0; 0,3,1,2,0; 3,0,2,0,0; 1,5,3,3,5];
y = find(A)
(it%displayed:)
y%=
%%%%%1
%%%%%3
%%%%%4
%%%%%6
%%%%%8
%%%%%9
%%%%10
%%%%11
%%%%12
%%%%13
%%%%14
%%%%16
%%%%20
The%all&function&returns%true%if%ALL%elements%of%an%array%are%true.%If%even%one%element%
is%false,%it%returns%false.%
Will%produce%a%matrix%of%that%size%containing%random%samples
o
Randn()%function%samples%from%a%standard%normal%distribution%(mean%=%0%and%stddev%=%
1)
Returns%average%value%of%a%data%set
o
Mean&
Returns%the%median%value%of%the%data%set;%the%median%is%the%middle%value%of%the%
dataset.%It%is%used%because%the%media%is%more%resistant%to%outliners%than%the%
mean%of%the%same%dataset.%
o
Median&
A%metric%to%measure%the%variation%of%the%normal%distribution%of%the%data%set.%
o
Standard&deviation&
Another%metric%to%measure%the%variation%of%the%normal%distribution%of%a%data%
set.%Also,%the%variance%of%any%distribution%is%equal%to%the%square%of%the%standard%
deviation.
o
Variance&
Will%sort%a%group%of%numbers%in%a%matrix%X.%The%second%parameter%'direction'%is%
either%'ascend'%or%'descend'.%
o
Sort(x,&'direction')&
The%randperm&function&scrambles%numbers%in%the%vector%
They%should%be%called%from%scripts,%the%command%window,%or%other%functions.%
Functions%run%a%group%of%commands%with%a%given%input.%
The%numel()&function%returns%the%number%of%elements,%n,%in%array%A,%equivalent%to%
prod(size(A));%
Functions
Wednesday,% September% 27,%2017
9:48%AM
'whos'&lists%in%alphabetical%order%the%names,%sizes,%and%types%of%all%the%variables%
in%the%workspace.%
'char'&(character)&refers%to%the%letters,%single%quotes%('')%
Strings refer%to%words%("")%
ones(m,n)%creates%a%m%*%n%matrix%of%zeroes%
ones(m)%same,%but%uses%m%for%both%rows%and%columns%
Each%column%sum%=%each%row%sum%=%each%diagonal%sum%
o
magic(s)%creates%an%s%x%s%"magic"%matrix
Creates%a%new%matrix%based%on%repeating%matrix%A%a%certain%number%of%times%in%
the%x%and%y%direction
o
repmat(A,%xTimes,%yTimes)%
Creates%a%new%matrix%with%the%same%elements%as%A,%but%the%requested%number%
of%rows%and%columns.%The%dimension%must%match%with%the%original%number%of%
elements%in%A.%
o
reshape(A,%numRows,%numCols)%
Numel(x)%yields%the%#%of%elements%in%x%
Length(x)%yields%the%number%of%elements%along%the%longest%dimension%of%x%
Size(x)%yields%a%vector%with%the%#%of%elements%along%each%dimension%of%x%
"Array%operations"%work%with%array%element-by-element
Vector%indexing
Bad%idea
o
Code%duplication%occurs%when%you%have%more%than%one%of%code%that%does%the%same%
thing
It's%an%algorithm%because%we%can%use%it%without%having%to%worry%about%
what's%inside.%
Is%an%algorithm%that%returns%data%to%the%caller,%and%operates%independently%of%
the%caller.%
o
Ex.%sqrt%function.%
o
The%interface%of%a%functions%describes%how%we%use%it
o
Functions&
Ex.%sum%function%
o
Many%work%column%by%column:%
o
Functions%with%matrices:
Name%-what%is%the%function%called?
§
Parameters%-variables%used%to%pass%dat%into%the%function%as%input
§
Return%variables%-a%list%of%variables%whose%values%will%be%returned%
back%to%the%outside%word%as%the%output%of%a%fucntion
§
Defines%how%we%interact%with%the%funciton%
An interface that%specifies%how%it%must%be%used%by%other%code
o
A%function%call%takes%in%arguments%that%correspond%to%the%parameters%of%the%
function
o
The%arguments%could%be%expressions,%they%do%not%have%variables
o
We%write%code%that%uses%the%parameters%to%calculate%
An implementation with%code%that%makes%it%work%behind%the%scenes
o
Every%function%has%two%major%pieces:
Code%is%vectorized%if%it%can%work%with%vectors%of%data%just%as%well%as%scalars
It%uses%a%compound%return%to%give%us%both%a%sorted%version%of%the%vector%and%a%
vector%of%sorted%indices.%
o
Write%"ascend"%or%"descend"
o
The%sort&function&works%with%column%vectors
Ex.%
K%=%find(X)%-returns%the%indices%of%the%array%x%that%point%to%nonzero%elements.%If%
non%is%found,%find%returns%an%empty%matrix.%
o
The%find&function&converts%a%logical%index%matrix%into%a%regular%index%matrix.%
A = [1,0,4,5,0; 0,3,1,2,0; 3,0,2,0,0; 1,5,3,3,5];
y = find(A)
(it%displayed:)
y%=
%%%%%1
%%%%%3
%%%%%4
%%%%%6
%%%%%8
%%%%%9
%%%%10
%%%%11
%%%%12
%%%%13
%%%%14
%%%%16
%%%%20
The%all&function&returns%true%if%ALL%elements%of%an%array%are%true.%If%even%one%element%
is%false,%it%returns%false.%
Will%produce%a%matrix%of%that%size%containing%random%samples
o
Randn()%function%samples%from%a%standard%normal%distribution%(mean%=%0%and%stddev%=%
1)
Returns%average%value%of%a%data%set
o
Mean&
Returns%the%median%value%of%the%data%set;%the%median%is%the%middle%value%of%the%
dataset.%It%is%used%because%the%media%is%more%resistant%to%outliners%than%the%
mean%of%the%same%dataset.%
o
Median&
A%metric%to%measure%the%variation%of%the%normal%distribution%of%the%data%set.%
o
Standard&deviation&
Another%metric%to%measure%the%variation%of%the%normal%distribution%of%a%data%
set.%Also,%the%variance%of%any%distribution%is%equal%to%the%square%of%the%standard%
deviation.
o
Variance&
Will%sort%a%group%of%numbers%in%a%matrix%X.%The%second%parameter%'direction'%is%
either%'ascend'%or%'descend'.%
o
Sort(x,&'direction')&
The%randperm&function&scrambles%numbers%in%the%vector%
They%should%be%called%from%scripts,%the%command%window,%or%other%functions.%
Functions%run%a%group%of%commands%with%a%given%input.%
The%numel()&function%returns%the%number%of%elements,%n,%in%array%A,%equivalent%to%
prod(size(A));%
Functions
Wednesday,% September% 27,%2017 9:48%AM

Loved by over 2.2 million students

Over 90% improved by at least one letter grade.

Leah — University of Toronto

OneClass has been such a huge help in my studies at UofT especially since I am a transfer student. OneClass is the study buddy I never had before and definitely gives me the extra push to get from a B to an A!

Leah — University of Toronto
Saarim — University of Michigan

Balancing social life With academics can be difficult, that is why I'm so glad that OneClass is out there where I can find the top notes for all of my classes. Now I can be the all-star student I want to be.

Saarim — University of Michigan
Jenna — University of Wisconsin

As a college student living on a college budget, I love how easy it is to earn gift cards just by submitting my notes.

Jenna — University of Wisconsin
Anne — University of California

OneClass has allowed me to catch up with my most difficult course! #lifesaver

Anne — University of California
Description
Functions Wednesday, September 27, 2017 9:48 AM whos lists in alphabetical order the names, sizes, and types of all the variables in the workspace. char (character) refers to the letters, single quotes () Strings refer to words () ones(m,n) creates a m * n matrix of zeroes ones(m) same, but uses m for both rows and columns magic(s) creates an s x s magic matrix o Each column sum = each row sum = each diagonal sum repmat(A, xTimes, yTimes) o Creates a new matrix based on repeating matrix A a certain number of times in the x and y direction reshape(A, numRows, numCols) o Creates a new matrix with the same elements as A, but the requested number of rows and columns. The dimension must match with the original number of elements in A. Numel(x) yields the of elements in x Length(x) yields the number of elements along the longest dimension of x
More Less
Unlock Document


Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Unlock Document
You're Reading a Preview

Unlock to view full version

Unlock Document

Log In


OR

Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit