MATLAB for Engineers and Scientists
INDEX
Section-I Section-II Section-III Section-IV Section-V Section-VI Introduction to Matlab Data Plotting in Matlab Writing Scripts & Function Files Programming in Matlab 3D Plots & Data Visualization Case Studies
Section-VII Applications Section-VIII Symbolic Computations
Introduction to MATLAB
Section-I
Introduction to MATLAB
Prepared by: Engr. Tarique Ahmad Reviewed by Prof. Dr. Asad-ur-Rehman
1 of 24
Introduction to MATLAB
+ - / * ^ Clear, clc, cla % format diary
Basic algebraic operations Clear variables from the workspace Beginning of a comment line Start saving commands in a file named diary Element by element algebraic operators Create an array of N equally spaced values between x0 and x1 Create an array of values between x0 and x1 spaced by x. Compute Inverse of a square matrix Compute determinant of a square matrix Transpose of a Matrix Compute rank of Matrix Command for opening a File Asking help from Matlab Returns all the commands related to that keyword
.*
linspace (x0, x1, N) x :x : x1 inv( ) det() ' Rank() fopen help lookfor
./
.^
Prepared by: Engr. Tarique Ahmad Reviewed by Prof. Dr. Asad-ur-Rehman
2 of 24
Introduction to MATLAB
Introduction to MATLAB
Matlab is an interactive system for doing numerical computations. A numerical analyst called Cleve Moler wrote the first version of Matlab in the 1970s. It has since evolved into a successful commercial software package. Matlab relieves you of a lot of the mundane tasks associated with solving problems numerically. This allows you to spend more time thinking, and encourages you to experiment. Matlab makes use of highly respected algorithms and hence you can be confident about your results. Powerful operations can be performed using just one or two commands. You can build up your own set of functions for a particular application. MATLAB is an acronym for MATrix LABoratory and as the name suggests, was initially developed to provide an environment to deal with matrix based computations. Since then the software has been diversified and now includes numerous collection of special functions. These include Toolboxes, Block sets Virtual world etc. MATLAB also provides an easyto-use interface to display your results graphically on screen as well as on hard copy. Thus MATLAB provides an integrated environment and is an ideal tool to solve problems of practical interest in an interactive fashion.
Prepared by: Engr. Tarique Ahmad Reviewed by Prof. Dr. Asad-ur-Rehman
3 of 24
Introduction to MATLAB
MATLAB as a Calculation Engine
The basic arithmetic operators are + - * / ^ and these are used in conjunction with brackets: ( ). The symbol ^ is used to get exponents (powers): 2^4=16. Type the commands shown following the MATLAB prompt: >> 2 + 3/4*5 ans = 5.7500 Is this calculation 2 + 3/(4*5) or 2 + (3/4)*5? Matlab works according to the priorities: 1. quantities in brackets, 2. powers 2 + 3^2 2 + 9 = 11, 3. * /, working left to right (3*4/5=12/5), 4. + -, working left to right (3+4-5=7-5), Thus, the earlier calculation was for 2 + (3/4)*5 by priority 3 mentioned above. MATLAB can be used as a calculator and can store the inputs or the outputs into variables. If a semicolon ; is placed at the end of a statement, the value of the variable is calculated but not displayed on the screen. To clear variables from the workspace, use the command clear.
Prepared by: Engr. Tarique Ahmad Reviewed by Prof. Dr. Asad-ur-Rehman
4 of 24
Introduction to MATLAB