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

{[ snackBarMessage ]}

### 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