MATLAB MATLAB Lab Manual Numerical Methods and Matlab
MATLAB MATLAB Lab Manual Numerical Methods and Matlab
Using MATLAB, we can solve technical computing problems faster than with traditional programming languages.
Numerical Methods:
Working in MATLAB:
Those are the methods by which mathematical problems are formulated so that they can be solved using arithmetic and logical operations. MATLAB provides a very nice tool to implement NUMERICAL METHODS. MATLAB can be employed to compute quantities and illustrate their graphical capabilities.
When we start MATLAB, the desktop appears in its default layout. MATLAB uses three primary windows : 1. Command Window-> Used to enter Commands and Data. 2. Graphics Window-> Used to Display plots and graphs. 3. Edit Window-> Used to create and edit m-files. Desktops default layout contains: Command Window Enter commands at the command line, indicated by the prompt (>>). Workspace The workspace contains variables that we create within or import into MATLAB from data files or other programs. Command History View or rerun commands that you entered at the command line.
Matlab Basics:
Variables and Assignment statements are used. There are many built-in functions in MATLAB Some of built-in functions are: abs( ), cos( ), exp( ), log( ), log10( ), sqrt( ), atan( ), etc. The reserved words or key words or name of built-in functions are NOT to be used. Expressions can be created using values, variables that have already been created, operators, built-in-functions, and parenthesis. Echo printing means to perform operation but not show the results this is done by placing semi colon ; at the end of statement.
Arrays:
A rectangular arrangement of numbers is called an array. 1-by-1 arrays are also called scalars 1-by-N arrays are also called row vectors
Row and Column vectors are also sometimes just called vectors In Matlab, all arrays of numbers are called double arrays. If we say that Array is two or three dimensional it is called Matrix.
c= [1,2; 3,4] It is a 2 x 2 matrix or 2 x 2 array. sum( ) ,mean( ) , length( ) ,max( ) ,min( ) , prod( ) , sign( ) ,fix( ) ,floor( ) ,ceil( ) ,round( ) , sort( ) are some functions that can be used on matrices and arrays. det( ) ,rank( ) ,trace( ) ,inv( ) , x=A\b ,poly( ) ,eig( ) , [v ,x]=eig(A) are the operations that are used on matrices. Matrix Generator:
1. zeros(p,q) Returns a matrix of all zeros of order pxq. 2. ones(p,q) Returns a matrix of all ones of order pxq. 3. eye(p,q) Returns a matrix with ones on the main diagonal and zeros elsewhere of order pxq. 4. rand(n) ,rand(m, n) (chooses random entries from interval 0-1), rands(n), rands(m, n) (chooses random entries from interval (-1,+1)), diag(v). Arithmetic operations can carried out on matrices provided that the operands are compatible with the operations.
Working of Commands:
Linspace:
linspace(x1, x2) generates a row vector of 100 linearly equally spaced points between x1 and x2. linspace(x1, x2, n) generates n points between x1 and x2. For n < 2, linspace returns x2.
Example: x=linspace(0 , 10 , 3) 3
x= 0 5 10
Colon operator:
X= x1: increment :x2 generates a row vector between x1 and x2 with increment. Example: x= 9: -2 : 1 x= 9 7 5 3 1
polynomial:
Polyder: Gives the derivative of polynomial polyder(p). Polyint: Gives the integral of the polynomial polyint(p). Polyval: Evaluates the value of polynomial at any specific value of x Roots: find the roots of the polynomial
Polyval(p , x). f= [ 1 2 3 4] >>f = 1 2 3 >> polyder(f) ans = 3 4 3 >> polyint(f) ans =
1.5000
4.0000
Fprintf:
avg = 8 >> fprintf ('the average is %f/n', avg) the average is 8.000000/n>>
Strcmp: compares two strings and returns 1 or 0 . Strmatch: matches two strings returns 1 or []. Findstr : finds one string in other and returns 1 or 0.
s2 = 'javaria roll# 116' s2 = javaria roll# 116 >> s1 = 'javaria' s1 =javaria >> strmatch (s1 , s2) ans = 1 >> findstr( s1 , s2) ans = 1 >> strcmp ( s1 , s2) ans = 0
Disp: simple commend used to display something. Plot: To create two-dimensional line plots, use the plot function. For example, plot
the value of the sine function from 0 to 2: x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x') ylabel('sin(x)') title('Plot of the Sine Function')
Subplot: we can display multiple plots in different sub regions of the same window
using the subplot function. Example:
Plots the following functions in interval 0x2 using 100 points .use the subplot command to display these functions on four windows on same graph Sin(x) Cos(x) Sin(x)*cos(x) Sin(x)/cos(x)
x= linspace ( 0 , 2*pi , 100); y=sin (x); subplot (2, 2, 1) plot ( x , y) z= cos (x); subplot ( 2, 2 , 2) plot ( x , z) w = sin (x).* cos(x); subplot ( 2 , 2 , 3) plot ( x , w) v = sin (x)./ cos(x); subplot ( 2, 2, 4) plot (x , v)
Example:
Program which inputs marks and tells what grade is yours x=input('Enter the marks') if (x>=90) disp (' your grade is A') else if (x>90) disp ('your grade is B') else if (x>80) disp('your grade is C') else if (x>70) disp ('your grade is D') else if (x>60) disp('your grade is F you are fail ') else disp ('') end end end end 7
end
ans:
Enter the marks77 x= 77 your grade is D
Switch:
Loop control:
For loop:
MATLAB functions that provide control over program loops. The for loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements. Example: Program which gives the average of numbers using for loop:
While loop:
The while loop repeats a group of statements an indefinite number of times under control of a logical condition. A matching end delineates the statements.
Break:
Dsolve:
We use dsolve to find solution of ordinary differential equations Its syntax is: r = dsolve('eq1,eq2,...', 'cond1,cond2,...', 'v')
Who: it lists all the current variables in use during a session. Whos: it also lists all current variables (long display) in use during a session
Fzero: fzero can be used to solve a single variable nonlinear equation of the form f(x)
= 0. The equation must first be programmed as a function (either inline or m-file). Example: x = fzero(fun,x0) fun is the function whose zero is to be computed. >>x = fzero('sin',3) x= 3.1416
10
Realmax is largest positive floating-point number Realmin is smallest positive floating-point number >> realmax ans = 1.7977e+308 >> realmin ans = 2.2251e-308
Eps: eps is also known as Machine epsilon in MATLAB is the distance from 1 to the next
valid floating point number..This value is used in determining the numerical rank of the matrix and in determining the pseudo inverse of a matrix. On an intel based machine
Used to calculate time duration of the process. Place tic before the code and toc after the code
Interpolation:
For one dimensional interpolation interp1 commend is used there are basic five methods for linear interpolation. yi = interp1(x,Y,xi,method) is the basic syntax of the command 1. 'nearest'Nearest neighbor interpolation 2. 'linear'Linear interpolation (default) 3. Sunday, June 24, 2012'spline'Cubic spline interpolation 4. 'pchip'Piecewise cubic Hermite interpolation 5. 'cubic'(Same as 'pchip')'v5cubic'Cubic interpolation used in MATLAB 5 are the five methods used for linear interpolation.
11
12
Secent Method:
13
Bisection Method:
Vectorization:
Vectorization means converting for and while loops to equivalent vector or matrix operations
14