Advanced Concepts in MATLAB [Version 1]

Add to Favourites
Post to:

Description
Advanced Concepts in MATLAB


Type: ppt

Discussion
Presentation Transcript Presentation Transcript

LEARNING MATLAB – ADVANCED TOPICS : LEARNING MATLAB – ADVANCED TOPICS Class By: Shivgan Presentation By: Arpit

Contents : Contents Functions overloading Random generation Matrix manipulation Advanced indexing Inline functions Call Function Mesh grids Array Slicing User input Cumulative products Newton Raphson

Function Overloading : Function Overloading Function overloading or method overloading is a feature found in various programming languages, that allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different tasks. Eg for MATLAB: methods function rts = roots( polyobject ) % Extract data for MATLAB version of roots function coef = double( polyobject.coefficients ); rts = roots( coef ); end end

Random Generation : Random Generation A random number generator (RNG) is a computational or physical device designed to generate a sequence of numbers or symbols that lack any pattern, i.e. appear random. MATLAB functions for Random Generation: rng Control random number generation rand Uniformly distributed pseudorandom numbers randn Normally distributed pseudorandom numbers randi Uniformly distributed pseudorandom integers randstream Random number stream randperm Random permutation

Matrix Manipulation : Matrix Manipulation Squaring a matrix a=[1 2;3 4]; a^2; Determinant a = [1 2; 3 4]; det (a) ans = -2 Transpose a=[1 2 3] aTranspose=[1 2 3]‘ Ones Matrix a=ones(4,3) Zero matrix a=zeros(4,3)

Advanced Indexing : Advanced Indexing An array with ‘n’ dimensions can be indexed using ‘m’ indices. More generally, the set of index tuples determining the result is formed by the Cartesian product of the index vectors (or ranges or scalars). MATLAB contains functions to help you find desired values within a vector or matrix: vec = [5 3 1 9 7] To get the minimum value and its index: [ minVal,minInd ] = min( vec ); max works the same way To find any the indices of specific values or ranges ind = find( vec == 9); ind = find( vec > 2 & vec < 6); To convert between subscripts and indices, use ind2sub ,and sub2ind. Look up help to see how to use them.

Inline functions : Inline functions MATLAB has a command that lets you develop an analytical expression of one or more inputs and assign that expression to a variable. The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. You can also use that function to return entire matrices. [ x,y ] = meshgrid (0:.1:2, 0:.1:2); mesh(x, y, c(x, y, pi/4)); xlabel ('Side 1'); ylabel ('Side 2'); zlabel ('Side 3'); title('Triangle Third Side vs. Sides Surrounding a 45^o Angle ( mrg )') print - depsc InlineExamplePlot

Call Function : Call Function You can call MATLAB functions using either command syntax or function syntax , as described below. Command Syntax A function call in this syntax consists of the function name followed by one or more arguments separated by spaces: functionname arg1 arg2 ... Argn Function Syntax A function call in this syntax consists of the function name followed by one or more arguments separated by commas and enclosed in parentheses: functionname (arg1, arg2, ..., argn )

Mesh Grids : Mesh Grids MESHGRID X and Y arrays for 3-D plots. [X,Y] = MESHGRID( x,y ) transforms the domain specified by vectors x and y into arrays X and Y that can be used for the evaluation of functions of two variables and 3-D surface plots. The rows of the output array X are copies of the vector x and the columns of the output array Y are copies of the vector y. [X,Y] = MESHGRID(x) is an abbreviation for [X,Y] = MESHGRID( x,x ).

Contd… : Contd … [X,Y,Z] = MESHGRID( x,y,z ) produces 3-D arrays that can be used to evaluate functions of three variables and 3-D volumetric plots. For example, to evaluate the function x*exp(-x^2-y^2) over the range -2 < x < 2, -2 < y < 2, [X,Y] = meshgrid (-2:.2:2, -2:.2:2); Z = X .* exp(-X.^2 - Y.^2); surf(X,Y,Z) MESHGRID is like NDGRID except that the order of the first two input and output arguments are switched (i.e., [X,Y,Z] = MESHGRID( x,y,z ) produces the same result as [Y,X,Z] = NDGRID( y,x,z )). Because of this, MESHGRID is better suited to problems in cartesian space, while NDGRID is better suited to N-D problems that aren’t spatially based. MESHGRID is also limited to 2-D or 3-D.

Array Slicing : Array Slicing array slicing is an operation that extracts certain elements from an array and packages them as another array, possibly with different number of indices (or dimensions) and different index ranges. Two common examples are extracting a substring from a string of characters (e.g. " ell " from "h ell o"), and extracting a row (or a column) of a rectangular matrix to be used as a vector. index_of_element_to_remove = 3; A( index_of_element_to_remove ) = [];now A will be [1 2 4] If you want to remove more elements at the same time you can do: index_of_element_to_remove = [1 3]; A( index_of_element_to_remove ) = []; now A will be [2 4]

User Input : User Input Syntax evalResponse = input( prompt ) strResponse = input( prompt , 's') evalResponse = input( prompt ) displays the prompt string on the screen, waits for input from the keyboard, evaluates any expressions in the input, and returns the value in evalResponse . To evaluate expressions, the input function accesses variables in the current workspace. strResponse = input( prompt , 's') returns the entered text as a MATLAB string, without evaluating expressions.

Cumulative products : Cumulative products Syntax: B = cumprod (A) B = cumprod ( A,dim ) B = cumprod (A) returns the cumulative product along different dimensions of an array. If A is a vector, cumprod (A) returns a vector containing the cumulative product of the elements of A. If A is a matrix, cumprod (A) returns a matrix the same size as A containing the cumulative products for each column of A. If A is a multidimensional array, cumprod (A) works on the first nonsingleton dimension. B = cumprod ( A,dim ) returns the cumulative product of the elements along the dimension of Aspecified by scalar dim. For example, cumprod (A,1) increments the column index, thus working along the columns of A. Thus, cumprod (A,1) and cumprod (A) will return the same thing. To increment the row index, use cumprod (A,2).

Newton Raphson using MATLAB : Newton Raphson using MATLAB % Newton Raphson solution of two nonlinear algebraic equations % set up the iteration error1 = 1.e8; xx(1) = 0; % initial guesses xx(2) = 0; iter =0; maxiter =30. % begin iteration while error1>1.e-12 iter =iter+1; x = xx(1); y = xx(2); % calculate the functions f(1) = 10*x+3*y*y-3; f(2) = x*x-exp(y) -2.; % calculate the Jacobian J(1,1) = 10; J(1,2) = 6*y; J(2,1) = 2*x; J(2,2) = -exp(y); % solve the linear equations y = -J\f'; % move the solution, xx(k+1) - xx(k), to xx(k+1) xx = xx + y';

Contd… : Contd … % calculate norms error1= sqrt (y(1)*y(1)+y(2)*y(2)) error( iter )= sqrt (f(1)*f(1)+f(2)*f(2)); ii( iter )= iter ; if ( iter > itermax ) error1 = 0.; s= sprintf ('****Did not converge within %3.0f iterations.****', itermax ); disp (s) end % check if error1 < 1.e-12 end x = xx(1); y = xx(2); f(1) = 10*x+3*y*y-3; f(2) = x*x-exp(y) -2.; % print results f xx iter % plot results semilogy ( ii,error ) xlabel ('iteration number') ylabel ('norm of functions') clear ii clear error

PowerPoint Presentation : THANK YOU

Shivgan Joshi
Free Class on: CFA,BAT,FRM, GMAT, Quant FinanFollow me to get updates.
User
1805 Members Recommend
2406 Followers

Your Facebook Friends on WizIQ