

































































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
An overview of the differences between scripts and functions in matlab programming. It covers the use of script files, script side-effects, functions, function input and output, and various examples of using functions and the disp and fprintf functions. The document also discusses vectorization and matlab's features to solve recurring programming problems.
Typology: Study notes
1 / 73
This page cannot be seen from the preview
Don't miss anything!


































































Gerald W. Recktenwald Department of Mechanical Engineering Portland State University [email protected]
These slides are a supplement to the book Numerical Methods with Matlab: Implementations and Applications, by Gerald W. Recktenwald, ©c 2001, Prentice-Hall, Upper Saddle River, NJ. These slides are ©c 2001 Gerald W. Recktenwald. The PDF version of these slides may be downloaded or stored or printed only for noncommercial, educational use. The repackaging or sale of these slides in any form, without written consent of the author, is prohibited.
The latest version of this PDF file, along with other supplemental material for the book, can be found at www.prenhall.com/recktenwald.
Version 0.97 August 28, 2001
Creating Side effects
Relational operators Conditional execution of blocks Loops
Using vector operations instead of loops Preallocation of vectors and matrices Logical and array indexing
Free Advice: Scripts offer no advantage over functions. Functions have many advantages over scripts. Always use functions instead of scripts.
Enter statements in file called tanplot.m
Contents of tanplot.m:
theta = linspace(1.6,4.6); tandata = tan(theta); plot(theta,tandata); xlabel(’\theta (radians)’); ylabel(’tan(\theta)’); grid on; axis([min(theta) max(theta) -5 5]);
tanplot
All variables created in a script file are added to the workplace. This may have undesirable effects because
Example: The easyplot script
% easyplot: Script to plot data in file xy.dat % Load the data D = load(’xy.dat’); % D is a matrix with two columns x = D(:,1); y = D(:,2); % x in 1st column, y in 2nd column plot(x,y) % Generate the plot and label it xlabel(’x axis, unknown units’) ylabel(’y axis, unknown units’) title(’Plot of generic x-y data set’)
The easyplot script affects the workspace by creating three variables:
clear who (no variables show) easyplot who
Your variables are:
D x y
The D, x, and y variables are left in the workspace. These generic variable names might be used in another sequence of calculations in the same Matlab session. See Exercise 10 in Chapter 4.
Functions use input and output parameters to communicate with other functions and the command window Functions use local variables that exist only while the function is executing. Local variables are distinct from variables of the same name in the workspace or in other functions.
Syntax:
The first line of a function m-file has the form:
function [outArgs] = funName(inArgs)
outArgs are enclosed in [ ]
inArgs are enclosed in ( )
twosum.m
function twosum(x,y) % twosum Add two matrices % and print the result x+y
threesum.m
function s = threesum(x,y,z) % threesum Add three variables % and return the result s = x+y+z;
addmult.m
function [s,p] = addmult(x,y) % addmult Compute sum and product % of two matrices s = x+y; p = x*y;
Example: Experiments with twosum:
twosum(2,2) ans = 4
x = [1 2]; y = [3 4]; twosum(x,y) ans = 4 6
A = [1 2; 3 4]; B = [5 6; 7 8]; twosum(A,B); ans = 6 8 10 12
twosum(’one’,’two’) ans = 227 229 212
Notes: 1. The result of the addition inside twosum is exposed because the x+y expression does not end in a semicolon. (What if it did?)
Example: Experiments with threesum:
a = threesum(1,2,3) a = 6
threesum(4,5,6) ans = 15
b= threesum(7,8,9);
Note: The last statement produces no output because the assignment expression ends with a semicolon. The value of 24 is stored in b.
Example: Experiments with addmult:
[a,b] = addmult(3,2) a = 5 b = 6
addmult(3,2) ans = 5
v = addmult(3,2) v = 5
Note: addmult requires two return variables. Calling addmult with no return variables or with one return variable causes undesired behavior.
It is usually desirable to print results to the screen or to a file. On rare occasions it may be helpful to prompt the user for information not already provided by the input parameters to a function.
Inputs to functions:
Text output from functions:
The input function can be used to prompt the user for numeric or string input.
x = input(’Enter a value for x’);
yourName = input(’Enter your name’,’s’);
Prompting for input betrays the Matlab novice. It is a nuisance to competent users, and makes automation of computing tasks impossible.
Free Advice: Avoid using the input function. Rarely is it necessary. All inputs to a function should be provided via the input parameter list. Refer to the demonstration of the inputAbuse function in § 3.3.1.