# Lecture10 - Engineering Analysis ENG 3420 Fall 2009 Dan C...

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

Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00

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

View Full Document
2 Lecture 10 Lecture 10 ± Last time: ² Bracketing vs Open Methods ² Convergence vs Divergence ² Simple Fixed-Point Iteration ² Newton-Raphson ± Today: ² More on functions nargin, nargout, varargin, and varargout ² The secant open method ² Optimization ± Golden ratio Æ makes one-dimensional optimization efficient. ± Parabolic interpolation Æ locate the optimum of a single-variable function. ± fminbnd function Æ determine the minimum of a one-dimensional function. ± fminsearch function Æ determine the minimum of a multidimensional function . ± Next Time ² More on optimization
nargin Æ returns the number of input arguments specified for a function or -1 if the function has a variable number of input arguments. nargout Æ returns the number of output arguments specified for a function. Example: Function myplot accepts an optional number of input and output arguments: function [x0, y0] = myplot(x, y, npts, angle, subdiv) % The first two input arguments are required; the other three have default values. . if nargin < 5, subdiv = 20; end if nargin < 4, angle = 10; end if nargin < 3, npts = 25; end . .. if nargout == 0 plot(x, y) else x0 = x; y0 = y; end

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

View Full Document
± varargin and varargout Æ used only inside a function M-file to contain the optional arguments to the function. Each must be declared as the last argument to a function, collecting all the inputs or outputs from that point onwards. In the declaration, varargin and varargout must be lowercase . ± Examples function myplot(x,varargin) plot(x,varargin{:}) Æ ² collects all the inputs starting with the second input into the variable varargin. ² myplot uses the comma-separated list syntax varargin{:} to pass the optional parameters to plot. ² The call myplot(sin(0:.1:1),'color',[.5 .7 .3],'linestyle',':') results in varargin being a 1-by-4 cell array containing the values 'color', [.5 .7 .3], 'linestyle', and ':'. function [s,varargout] = mysize(x) nout = max(nargout,1)-1; s = size(x); for k=1:nout, varargout(k) = {s(k)}; end returns the size vector and, optionally, individual sizes. So ² [s,rows,cols] = mysize(rand(4,5)); ² returns s = [4 5], rows = 4, cols = 5.
5 Newton-Raphson Method ± Express x i+1 function of x i and the values of the function and its derivative at x i . ± Graphically Æ draw the tangent line to the f ( x ) curve at some guess x , then follow the tangent line to where it crosses the x -axis. f ' ( x i ) = f ( x i ) 0 x i x i + 1 x i + 1 = x i f ( x i ) f ' ( x i )

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

View Full Document
6 function[root,relative_error,number_iterations]=newton_raphson(myfunction,derivative, initial_guess,desired_relative_error, max_number_iterations,varagin) if(nargin<3) error('at least 3 input arguments, required'); end if (nargin<4) is_empty(desired_realtive_error), desired_realtive_error=0.0001; end % set desired_realtive_error to the default, 0.0001, if none specified if (nargin<5) is_empty(max_number_iterations),max_number_iterations=50; end % set max_number_iterations to the default, 50, if none specified number_iterations=0; current_guess = initial_guess; while(1) next_guess = current_guess;
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/17/2012 for the course EGN 3420 taught by Professor Staff during the Spring '08 term at University of Central Florida.

### Page1 / 39

Lecture10 - Engineering Analysis ENG 3420 Fall 2009 Dan C...

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

View Full Document
Ask a homework question - tutors are online