This preview shows pages 1–9. Sign up to view the full content.
Rutgers University
School of Engineering
Fall 2011
14:440:127  Introduction to Computers for Engineers
Sophocles J. Orfanidis
ECE Department, Rm ELE230
[email protected]
week 5
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Week
1
 Basics – variables, arrays, matrices, plotting (ch. 2 & 3)
Week
2
 Basics – operators, functions, program flow (ch. 2 & 3)
Week
3
 Matrices (ch. 4)
Week
4
 Plotting – 2D and 3D plots (ch. 5)
Week
5
 Userdefined functions (ch. 6)
Week
6
 Inputoutput formatting – fprintf, sprintf (ch. 7)
Week
7
 Program flow control & relational operators (ch. 8)
Week
8
 Matrix algebra – solving linear equations (ch. 9)
Week
9
 Structures & cell arrays (ch. 10)
Week 10  Symbolic math (ch. 11)
Week 11  Numerical methods – data fitting (ch. 12)
Week 12 – Selected topics
Weekly Topics
Textbook:
H. Moore,
MATLAB for Engineers
, 2
nd
ed.,
Prentice Hall, 2009
UserDefined Functions
Mfiles, script files, function files
anonymous & inline functions
function handles
function functions,
fzero,fminbnd
multiple inputs & outputs
subfunctions, nested functions
homework template function
function types
recursive functions, fractals
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Script Mfiles
contain commands to be executed as
though they were typed into the command window, i.e.,
they collect many commands together into a single file.
Function Mfiles
must start with a
function
definition
line, and may accept input variables and/or return output
variables.
The function definition line has syntax:
function [outputs] = func(inputs)
where the function name,
func
,
is arbitrary and must
match the name of the Mfile, i.e.,
func.m
Mfiles: script or function files
% file
rms.m
calculates the
% rootmeansquare (RMS) value and the
% meanabsolute value of a vector x:
function [r,m] = rms(x)
r = sqrt(sum(abs(x).^2) / length(x));
m = sum(abs(x)) / length(x);
>> x = 4:4;
>> [r,m] = rms(x)
r =
2.5820
m =
2.2222
>> r = rms(x)
r =
2.5820
returns only the
first output
Example:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Variables defined in a script file are known to the whole
current workspace, outside the script file.
Script files
may not
have any function definitions in them,
unless the functions are defined as inline or anonymous one
line functions, e.g., using the functionhandle
@(x)
.
Variables in a function Mfile are
local
to that function and
are not recognized outside the function (unless they are
declared as
global
variables, which is usually not
recommended.)
Function files
may
include the definition of other functions,
either as
subfunctions
, or as
nested functions
. This helps to
collect together all relevant functions into a single file (e.g.,
this is how you will be structuring your homework reports.)
Make up your own functions using three methods:
1.
anonymous, with functionhandle, @(x)
2.
inline
3.
Mfile
>> f = @(x) exp(0.5*x).*sin(5*x);
>> g = inline('exp(0.5*x).*sin(5*x)');
% edit & save file h.m containing the lines:
function y = h(x)
y = exp(0.5*x).*sin(5*x);
.*
allows vector or matrix inputs x
example 1:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document How to include parameters in functions
% method 1: define a,b first, then define f
a = 0.5; b = 5;
f = @(x) exp(a*x).*sin(b*x);
% method 2: pass parameters as arguments to f
f = @(x,a,b) exp(a*x).*sin(b*x);
% this defines the function f(x,a,b)
% so that f(x, 0.5, 5) would be equivalent to
% the f(x) defined in method 1.
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 10/25/2011 for the course COMPUTER S 101 taught by Professor Orfandi during the Spring '11 term at Rutgers.
 Spring '11
 Orfandi

Click to edit the document details