week05 - Rutgers University School of Engineering Fall 2011...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Rutgers University School of Engineering Fall 2011 14:440:127 - Introduction to Computers for Engineers Sophocles J. Orfanidis ECE Department, Rm ELE-230 [email protected] week 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 - User-defined functions (ch. 6) Week 6 - Input-output 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
Background image of page 2
User-Defined Functions M-files, 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Script M-files 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 M-files 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 M-file, i.e., func.m M-files: script or function files
Background image of page 4
% file rms.m calculates the % root-mean-square (RMS) value and the % mean-absolute 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:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 function-handle @(x) . Variables in a function M-file 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 sub-functions , 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.)
Background image of page 6
Make up your own functions using three methods: 1. anonymous, with function-handle, @(x) 2. inline 3. M-file >> 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:
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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.
Background image of page 8
Image of page 9
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.

Page1 / 41

week05 - Rutgers University School of Engineering Fall 2011...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online