Academia.eduAcademia.edu

Introduction to MATLAB

Aim: To know about the MATLAB working procedures such as saving files, basic commands and symbolic computations. Step 1: Opening Matlab-saving M-fles, script M-fles, function M-fles and Help window etc.

Introduction to MATLAB Aim: To know about the MATLAB working procedures such as saving files, basic commands and symbolic computations. Step 1: Opening Matlab - saving M-fles, script M-fles, function M-fles and Help window etc. Step 2: Basic calculations – Elementary math functions: max, min, mean, median, standard deviation - Elementary matrix operations-addition, multiplication, inverse, Eigen value and Eigen vectors. Step 3: symbolc computation,- Inline function, operation on fuctions – Finding solution to equations - Differentiation – Integration. Introduction The name MATLAB stands for MATrix LABoratory. MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming environment. MATLAB has many advantages compared to conventional computer languages (Ex. C, FORTRAN) for solving technical problems. The software package has been commercially available since 1984. It has graphics commands that make the visualization of results immediately. Specific applications are collected in packages referred as toolbox (Ex. signal processing, symbolic computation, control theory, simulation, optimization) The major components in the MatLab: The Command Window The Command History The Workspace The Current Directory The Help Browser The Start button Figure 1: The graphical interface to the MATLAB workspace M-fles: MATLAB can execute a sequence of statements stored on disk-files. Such files are called "M-files" because they must have the file type of ".m" as the last part of their filename. Creating M-files: Select FILE OPEN NEW M-files Types of M-files:  Script M-files Function M-files Script M-files List of MATLAB statements Variables are global Run it by typing the file name Ex 1. Find the square roots of 1,2,3,4. a = 1:4 b = sqrt(a) Output: b = 1.0000 1.4142 1.7321 2.0000 Pr 1. Find the sum of first 100 Natural numbers. Function M-files Starts with function List of MATLAB statements Variables are local Ex 2. Find where . Create the function M-file: function [y]=fun(x) y=x^3+3*x^2+2; Create the script M-file: a=fun(2) Output: a = 22 Pr 2. Create a function to the average of two numbers. Basic calculations using elementary math functions Ex 3. x=[13, 18, 13, 14, 13, 16, 14, 21, 13] a = min(x) b = max(x) c = mean(x) d = std(x) e = median(x) f = range(x) g = mode(x) h = var(x) Pr 3. X = 1 to 100. Elementary matrix operations: Ex 4. Type the following matrices in the Command window: 1. ; ; 2. ; 3. 1. A=[1;2;3] 2. B=[1 2] 3. C=[0 1; -2 -3] 4. D=[-1 2 0; 4 10 -2; 1 0 6] 5. Use MATLAB to find the value in the second row and the third column of matrix D. 6. Use MATLAB to find the second row of matrix D. 7. Use MATLAB to find the third column of matrix D. 5. D(2,3) 6. D(2,:) 7. D(:,3) Matrix functions: Ex 5. Let the matrix A, B and C are ; ; then solve the following basic matrix operations using MATLAB: A=[0 1; -2 -3] B=[1 0; 3 -2] C=[1 -1; -2 2] 1. Addition: A+B 2. Subtraction: A-B 3. Multiplication: A*B 4. Transpose: A' 5. Inverse: inv(A) 6. Diagonals of a matrix diag(A) 7. Determinant det(A) 8. Eigen values eig(A) 9. Eigen values and Eigen vectors [v,e]=eig(A)10. Norm: norm(A) 11. Rank: rank(A) Pr 6a. Use MATLAB to prove the following: A*B,B*A A*(B*C),(A*B)*C (A+B)*C, A*C + B*C C*(A+B), C*A + C*B det(A*B), det(A)*det(B) det(A'), det(A) A*inv(A), inv(A)*A Pr 6b. If A=[2 2 1; 1 3 1; 1 2 2], B=[1 2 1; 2 3 1; 1 1 2] and C=[3 2 1; 2 3 1; 1 3 2] then perform the matrix operations 1 to 11. Symbolc Computation Inline function: An alternative to function M-files is provided by objects called inline functions. Inline functions are like function M-files, i.e. they accept (usually numerical) input and return output. The difference to function M-files is that the function evaluation takes place in the current workspace, whereas function M-files operate in separate workspaces. Ex 6a: Let's define the function y(x)=(1+x)2/(1+x2) as inline function: » '(1+x).^2./(1+x.^2)' ans = (1+x).^2./(1+x.^2) » y=inline(ans,'x') y = Inline function:       y(x) = (1+x).^2./(1+x.^2) In the first command the formula is edited as a string, i.e. it is quoted by apostrophes. In the second command the string is evaluated and converted to an inline function. With the third argument we fix the parameter of the inline function. Note that all arguments passed to inline must be strings. Since the answer of the first command is already a string, the apostrophes must be omitted in the argument ans, but the apostrophe quotation  must be used in the variable declaration 'x'. The above two commands  can also be combined in the single commandy=inline('(1+x).^2./(1+x.^2)','x'). The definition of the inline function was made array smart: » y(1),y([1 2 3]) ans =      2    ans =     2.0000    1.8000    1.6000 Inline functions can be exploited in a similar manner as Function M-files. Specifically we can apply the fplot command: » fplot(y,[-1 1]) will generate a graph of the function in the range . Note that here y must not be quoted by apostrophes, in contrast to plots of functions defined in Function M-files. Inline functions can depend on several arguments. Ex 6b. The syntax is to put the expression to be evaluated in single quotes, followed in order by the variables of the function with each of these also surrounded by single quotes. For example, if you want, to return , you could create an inline function as follows: c = inline('sqrt(a.^2+b.^2-2*a.*b.*cos(theta))', 'a', 'b', 'theta') Output: c = Inline function: c(a,b,theta) = sqrt(a.^2+b.^2-2*a.*b.*cos(theta)) Use the function by putting numbers in for the arguments: SideThree = c(2, 3, pi/6) Output: SideThree = 1.6148 Ploting the Graph: [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 Pr 7: Define the function y(x)=(2+3x)3/(4+5x3) as inline function. operation on fuctions: Finding solution to equations: Ex 8a. solve x-5 = 0. solve('x-5=0')Output: ans = 5 Differentiation: Integration: