# week05 - Rutgers University School of Engineering Spring...

This preview shows pages 1–8. Sign up to view the full content.

Rutgers University School of Engineering Spring 2012 14:440:127 - Introduction to Computers for Engineers Sophocles J. Orfanidis ECE Department [email protected] week 5

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

View Full Document
Week 1 Week 2 Week 3 - Matrices (ch. 4) Week 4 - Plotting – 2D and 3D plots (ch. 5) Week 5 - User-defined functions (ch. 6) – Exam 1 Week 6 - Input-output formatting – fprintf, fscanf (ch. 7) Week 7 Week 8 Week 9 - Matrix algebra – solving linear equations (ch. 10) Week 10 - Cell arrays and structures (ch. 11) – Exam 2 Week 11 - Numerical methods – part I (ch. 13) Week 12 - Numerical methods – part II (ch. 13) Week 13 - Symbolic toolbox (ch. 12) Week 14 - Exam 3 Weekly Topics Textbook: H. Moore, MATLAB for Engineers , 3 d ed., Prentice Hall, 2011
User-Defined Functions M-files, script files, function files function handles function functions, fzero,fminbnd 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 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
% 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:

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 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.)
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)'); 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
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/26/2012 for the course ENGINEERIN 440:127 taught by Professor Blase during the Spring '10 term at Rutgers.

### Page1 / 42

week05 - Rutgers University School of Engineering Spring...

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

View Full Document
Ask a homework question - tutors are online