Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab...

22
Lecture Notes 7 Matlab Function Files Matlab Function Files Introduction to Computational Science , Fall 2010 Marcus Pendergrass Hampden-Sydney College

Transcript of Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab...

Page 1: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Matlab Function FilesMatlab Function FilesIntroduction to Computational Science , Fall 2010

Marcus PendergrassHampden-Sydney College

Page 2: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Matlab Script Files

We have seen Matlab script files:

% MATLAB script file diceTossing.m%% User Input SectionnumDice = 9;numSides = 6;numTosses = 10000;

%% Calculation SectiondiceTosses = randi(numSides , numDice , numTosses );diceSums = sum(diceTosses );

%% Output Sectionhist(diceSums , [numDice:numDice*numSides ]);

I Scripts can be called from the command line:

>>diceTossing;

I But to change user input values, you must edit the script!

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 2 / 9

Page 3: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Matlab Script Files

We have seen Matlab script files:

% MATLAB script file diceTossing.m%% User Input SectionnumDice = 9;numSides = 6;numTosses = 10000;

%% Calculation SectiondiceTosses = randi(numSides , numDice , numTosses );diceSums = sum(diceTosses );

%% Output Sectionhist(diceSums , [numDice:numDice*numSides ]);

I Scripts can be called from the command line:

>>diceTossing;

I But to change user input values, you must edit the script!

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 2 / 9

Page 4: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Matlab Script Files

We have seen Matlab script files:

% MATLAB script file diceTossing.m%% User Input SectionnumDice = 9;numSides = 6;numTosses = 10000;

%% Calculation SectiondiceTosses = randi(numSides , numDice , numTosses );diceSums = sum(diceTosses );

%% Output Sectionhist(diceSums , [numDice:numDice*numSides ]);

I Scripts can be called from the command line:

>>diceTossing;

I But to change user input values, you must edit the script!

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 2 / 9

Page 5: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Matlab Function Files

Matlab function files allow you to specify the user input values inthe command line call itself.

% MATLAB function file tossDice.mfunction tossDice(numDice , numSides , numTosses)

%% Calculation sectiondiceTosses = randi(numSides , numDice , numTosses );diceSums = sum(diceTosses );

%% Output Sectionhist(diceSums , [numDice : numDice*numSides ]);

I To simulate the distribution of the sum when 10,000 tosses of apair of four-sided dice are made:

>>tossDice(2, 4, 10000);

I Inputs are included in parentheses in the function call.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 3 / 9

Page 6: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Matlab Function Files

Matlab function files allow you to specify the user input values inthe command line call itself.

% MATLAB function file tossDice.mfunction tossDice(numDice , numSides , numTosses)

%% Calculation sectiondiceTosses = randi(numSides , numDice , numTosses );diceSums = sum(diceTosses );

%% Output Sectionhist(diceSums , [numDice : numDice*numSides ]);

I To simulate the distribution of the sum when 10,000 tosses of apair of four-sided dice are made:

>>tossDice(2, 4, 10000);

I Inputs are included in parentheses in the function call.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 3 / 9

Page 7: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Matlab Function Files

Matlab function files allow you to specify the user input values inthe command line call itself.

% MATLAB function file tossDice.mfunction tossDice(numDice , numSides , numTosses)

%% Calculation sectiondiceTosses = randi(numSides , numDice , numTosses );diceSums = sum(diceTosses );

%% Output Sectionhist(diceSums , [numDice : numDice*numSides ]);

I To simulate the distribution of the sum when 10,000 tosses of apair of four-sided dice are made:

>>tossDice(2, 4, 10000);

I Inputs are included in parentheses in the function call.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 3 / 9

Page 8: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Basic Function File Rules

1. The first statement in the function file is the function definitionline:

function outputVar = functionName(inputVars)

2. The function is stored in an m-file with the same name as thefunction.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 4 / 9

Page 9: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Basic Function File Rules

1. The first statement in the function file is the function definitionline:

function outputVar = functionName(inputVars)

2. The function is stored in an m-file with the same name as thefunction.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 4 / 9

Page 10: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Basic Function File Rules

1. The first statement in the function file is the function definitionline:

function outputVar = functionName(inputVars)

2. The function is stored in an m-file with the same name as thefunction.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 4 / 9

Page 11: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: A Simple Matlab Function

Example

Implement the height function s(t) = 11 + 15t− 4.9t2 as a Matlabfunction file. The function should accept an array of times, andoutput an array of corresponding heights. Use the function to make aplot of s(t) for t ranging between 0 and 3.

Solution

Let’s call the Matlab function s. In order to handle an array oftime values, we should implement s using vectorized arithmetic. Hereis the code:

function heights = s(times)heights = 11 + 15*t - 4.9*t.^2;

This code should be stored in a file named s.m.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 5 / 9

Page 12: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: A Simple Matlab Function

Example

Implement the height function s(t) = 11 + 15t− 4.9t2 as a Matlabfunction file. The function should accept an array of times, andoutput an array of corresponding heights. Use the function to make aplot of s(t) for t ranging between 0 and 3.

Solution

Let’s call the Matlab function s. In order to handle an array oftime values, we should implement s using vectorized arithmetic. Hereis the code:

function heights = s(times)heights = 11 + 15*t - 4.9*t.^2;

This code should be stored in a file named s.m.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 5 / 9

Page 13: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: A Simple Matlab Function

Example

Implement the height function s(t) = 11 + 15t− 4.9t2 as a Matlabfunction file. The function should accept an array of times, andoutput an array of corresponding heights. Use the function to make aplot of s(t) for t ranging between 0 and 3.

Solution (continued)

We can now plot s(t) from the Matlab command line as follows:

>>t = 0 : 0.01 : 3;

>>h = s(t);

>>plot(t,h);

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 5 / 9

Page 14: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Example

Recall that the solution of the discrete constant growth rate problem

Pk+1 = (1 + γ)Pk

P0 = p0

is given by Pk = (1 + γ)kp0. Implement this solution as a Matlabfunction. The user should be able to specify the following items inthe function call:

1. The growth rate γ.

2. The initial population p0.

3. The range of k values for the solution.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 6 / 9

Page 15: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Solution

I Let’s call our function DCGRM.

I We need Matlab variables for the inputs γ, p0, and the rangeof k values.

I Let’s use gamma for γ, p0 for p0, and k for the range of k values(this will be a vector).

I Let’s return the solution values in an array called solVals.

I We now have enough information to write the function file:

function solVals = DCGRM(k, gamma , p0)solVals = (1 + gamma ).^k * p0;

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 6 / 9

Page 16: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Solution

I Let’s call our function DCGRM.

I We need Matlab variables for the inputs γ, p0, and the rangeof k values.

I Let’s use gamma for γ, p0 for p0, and k for the range of k values(this will be a vector).

I Let’s return the solution values in an array called solVals.

I We now have enough information to write the function file:

function solVals = DCGRM(k, gamma , p0)solVals = (1 + gamma ).^k * p0;

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 6 / 9

Page 17: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Solution

I Let’s call our function DCGRM.

I We need Matlab variables for the inputs γ, p0, and the rangeof k values.

I Let’s use gamma for γ, p0 for p0, and k for the range of k values(this will be a vector).

I Let’s return the solution values in an array called solVals.

I We now have enough information to write the function file:

function solVals = DCGRM(k, gamma , p0)solVals = (1 + gamma ).^k * p0;

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 6 / 9

Page 18: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Solution

I Let’s call our function DCGRM.

I We need Matlab variables for the inputs γ, p0, and the rangeof k values.

I Let’s use gamma for γ, p0 for p0, and k for the range of k values(this will be a vector).

I Let’s return the solution values in an array called solVals.

I We now have enough information to write the function file:

function solVals = DCGRM(k, gamma , p0)solVals = (1 + gamma ).^k * p0;

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 6 / 9

Page 19: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Solution

I Let’s call our function DCGRM.

I We need Matlab variables for the inputs γ, p0, and the rangeof k values.

I Let’s use gamma for γ, p0 for p0, and k for the range of k values(this will be a vector).

I Let’s return the solution values in an array called solVals.

I We now have enough information to write the function file:

function solVals = DCGRM(k, gamma , p0)solVals = (1 + gamma ).^k * p0;

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 6 / 9

Page 20: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Example: Discrete Constant Growth Rate Function

Solution (continued)

Suppose we wanted to use the function to plot the solutions of theproblem

Pk+1 = (1 + γ)Pk

P0 = 1000

where γ = 0.04 and k = 0, 1, 2, · · · 80. From the Matlab commandline we would enter:

>>timeSteps = 0 : 80;

>>solutionVals = DCGRM(timeSteps, 0.04, 1000);

>>plot(timeSteps, solutionVals);

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 7 / 9

Page 21: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Exercise: Height Versus Time

Exercise

Implement the height function s(t) = s0 + v0t− 4.9t2 as a Matlabfunction file. The user should be able to specify the following inputsin the function call:

1. The initial height s0.

2. The initial velocity v0.

3. The range of t-values for the solution.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 8 / 9

Page 22: Lecture Notes 7 - Hampden-Sydney CollegeLecture Notes 7 Dr. Pendergrass Matlab Function Files Matlab function lesallow you to specify the user input values in the command line call

Lecture Notes 7

Dr. Pendergrass

Exercise: Continuous Constant Growth RateFunction

Exercise

Recall that the solution of the approximate continuous constantgrowth rate problem

∆P

∆t= rP

P0 = p0

is given by P (t) = (1 + r∆t)t

∆t p0. Implement this solution as aMatlab function. The user should be able to specify the followingitems in the function call:

1. The growth rate r.

2. The initial population p0.

3. The time step ∆t.

4. The range of t-values for the solution.

Dr. Pendergrass (H-SC) Lecture Notes 7 Fall 2010 9 / 9