Lecture 14: Functions and Program Development - Prof. Gregory Sawicki, Study notes of Biology

A lecture note from a university course on functions and program development. It covers the concept of functions as pre-defined or user-defined blocks of code, their syntax, and usage in matlab. The lecture also discusses the benefits of using functions and provides examples of simple functions and their usage. The document also mentions the upcoming topics in the course, which include numerical analysis and signal processing.

Typology: Study notes

2010/2011

Uploaded on 04/06/2011

goalie4eva
goalie4eva 🇺🇸

31 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 14: Wednesday March 2, 2011:
Announcements:
Case 2 resubmission is due today. Be sure to submit you’re your original and the
new version of your solution. Please take advantage of Prof. Sawicki’s Office
Hours on M’s and T’s from 4-5PM 4212C EB3. Ben’s hours are still being held
Thursday afternoons.
SH6 and Case 3 will be posted by the end of the week. These will be due Wed.
March 16th (SH6) and Monday March 21st (Case 3).
SL5 is not due until after Spring Break (Thursday March 17th).
Lab tomorrow will be a demonstration of a powered ankle exoskeleton to give
you a flavor of what is to come following spring break.
I am still grading the exam and hope to return in Monday March 14th. Thanks for
your patience.
Reading for Next Time:
Reading on Functions and Program Development: Palm Chapter 3.1-3.3, Palm Chapter
4.1 Reading on Debugging Palm Chapter 4.8
*SH EC: How could we modify the pendulum simulation script file from last time so that it
is a user-defined function called PendSim. It should take
length,grav,tfinal,dt,ang0,angvel0 as inputs and return time,ang,angvel.
See posted code on Moodle for solution.
*Today’s Goals:
Continue discussing functions-
*I REMEMBER: turn on your diary to save your Command Window
>>diary LectureMar2.m
*II. Functions Continued-
Functions (or commands) are defined blocks of code that live in their own files.
Functions are pre-defined blocks of code that can be used over and over again.
Parameters and variables may be passed into or returned from functions. Functions can
be user-defined or already defined in MATLAB. Help files will always give instructions on
how previously defined functions are used.
You can nest subfunctions within primary functions - subfunctions can only be called
from within the primary/parent function. Functions are handy because you can write a
block of code once, and then re-use it as many times as you want, in as many other
programs as you want. Using a given function from other pieces of code is referred to as
‘calling’ that function.
For example, let’s say I have some code that I have programmed in a script file-
% Example of Conditional Code Block
x=3, y=0, z=10;
if x==y % use == to compare for equivalence
y = z;
x = x^2;
pf3
pf4

Partial preview of the text

Download Lecture 14: Functions and Program Development - Prof. Gregory Sawicki and more Study notes Biology in PDF only on Docsity!

Lecture 14: Wednesday March 2, 2011:

Announcements:

 Case 2 resubmission is due today. Be sure to submit you’re your original and the

new version of your solution. Please take advantage of Prof. Sawicki’s Office

Hours on M’s and T’s from 4-5PM 4212C EB3. Ben’s hours are still being held

Thursday afternoons.

 SH6 and Case 3 will be posted by the end of the week. These will be due Wed.

March 16th^ (SH6) and Monday March 21st^ (Case 3).

 SL5 is not due until after Spring Break (Thursday March 17th).

 Lab tomorrow will be a demonstration of a powered ankle exoskeleton to give

you a flavor of what is to come following spring break.

 I am still grading the exam and hope to return in Monday March 14th. Thanks for

your patience.

Reading for Next Time:

Reading on Functions and Program Development: Palm Chapter 3.1-3.3, Palm Chapter

4.1 Reading on Debugging Palm Chapter 4.

*SH EC: How could we modify the pendulum simulation script file from last time so that it

is a user-defined function called PendSim. It should take

length,grav,tfinal,dt,ang0,angvel0 as inputs and return time,ang,angvel.

See posted code on Moodle for solution.

*Today’s Goals:

Continue discussing functions-

*I REMEMBER: turn on your diary to save your Command Window

>>diary LectureMar2.m

*II. Functions Continued-

Functions (or commands) are defined blocks of code that live in their own files.

Functions are pre-defined blocks of code that can be used over and over again.

Parameters and variables may be passed into or returned from functions. Functions can

be user-defined or already defined in MATLAB. Help files will always give instructions on

how previously defined functions are used.

You can nest subfunctions within primary functions - subfunctions can only be called

from within the primary/parent function. Functions are handy because you can write a

block of code once, and then re-use it as many times as you want, in as many other

programs as you want. Using a given function from other pieces of code is referred to as

‘calling’ that function.

For example, let’s say I have some code that I have programmed in a script file-

% Example of Conditional Code Block x=3, y=0, z=10; if x==y % use == to compare for equivalence y = z; x = x^2;

elseif x < y % otherwise, if x is less than y... y = x+1; else % if previous statements are false... y = x^2; x = z; end % terminate code block

but I want to be able to execute it for many different combinations of x, y, z. To do this I

could create a user-defined function by placing the above code in its own file called

computeThis1.m with the following syntax:

function [x, y, z] = computeThis1(x, y, z) % a simple function that takes 3 input values, % and returns the same 3 values that may or may not have been updated in %the function body if x==y %use == to compare for equivalence y = z x = x^2; elseif x < y %less than y = x+1; else %if previous statements are false... y = x^2; x = z; end % terminate code block end % terminate parent function

The above function requires 3 input variables, x, y, and z. The body of the function will

modify these variables as needed, and the updated values will be returned as 3 output

variables x, y, z.

Here is how I could use the function computeThis1 - by ‘calling’ it from a separate script

file (e.g. main program.m).

%define three variables a=4, b=3, c=7; %call function that takes 3 variables and returns 3 variables. [d, e, f] = computeThis1(a, b, c);

The function does not change the values of a, b, or c but instead, it returns 3 values that

I chose to save as d, e, and f. I could have chosen to overwrite a, b, c, but in this case I

want to save the original values.

NOTE WELL: The variable names in the function definition file do not have to match the

variable names that I use when ‘calling’ the function. The order that the variables are

passed in will be maintained, that is, x=a, y=b, and z=c inside the function. And on the

return, d=x, e=y, and f=z.

It is also possible to nest functions (i.e. subfunctions) within other functions

(primary/parent functions). For example:

function [x, y, z] = computeThis2(x, y, z) % a simple function that takes 3 input values, % and returns the same 3 values that may or may not have been updated in %the function body. This is the parent/primary function. if x==y %use == to compare for equivalence

*Rule 4. When calling a function, only the order of the inputs/arguments ( ) and

outputs [ ] matter, not the names.

*Rule 5. Functions can have any number of inputs or outputs (including no inputs or

outputs). For example, sometimes a function just returns a plot or screen display and

has no outputs. Or sometimes a function just initializes some local variables and has no

inputs. See pages 121 and 122 in Palm for other examples.

*Rule 6. NEVER define global variables! i.e. Ignore Palm top of pg. 124.

IN CLASS PROBLEMS:

Do an example similar to that on SH

-Palm Ch 3. page 141 degF to degC.

1. Write a function that accepts temperature in degrees Fahrenheit (degF) and computes

the corresponding value in degrees Celsius (degC).

TdegC= 5/9(TdegF-32)

Now, write a script that uses your function to create and plot values in degC vs. values in

degF over the range -200 degF to 200 degF.

2. Do an example similar to SL5 by real-time programming in front of class

-Palm Ch 4. page 216-217 Problem 48. Inventory Model.

Start as a script-then think about how to make a function-

*NEXT TIME: First enjoy Spring Break!

We’ll return with more on functions- including anonymous functions, function handles,

function functions, and other methods of calling functions-

Then we begin with numerical analysis topics- signal processing, data acquisition, curve

fitting, integration and differentiation etc.