MATLAB - Overview
MATLAB (matrix laboratory) is a fourth-generation high-level programming language and interactive environment for numerical computation, visualization and programming.MATLAB is developed by MathWorks.It allows matrix manipulations; plotting of functions and data; implementation of algorithms; creation of user interfaces; interfacing with programs written in other languages, including C, C++, Java, and FORTRAN; analyze data; develop algorithms; and create models and applications.
It has numerous built-in commands and math functions that help you in mathematical calculations, generating plots, and performing numerical methods.
MATLAB's Power of Computational Mathematics
MATLAB is used in every facet of computational mathematics. Following are some commonly used mathematical calculations where it is used most commonly −- Dealing with Matrices and Arrays
- 2-D and 3-D Plotting and graphics
- Linear Algebra
- Algebraic Equations
- Non-linear Functions
- Statistics
- Data Analysis
- Calculus and Differential Equations
- Numerical Calculations
- Integration
- Transforms
- Curve Fitting
- Various other special functions
Features of MATLAB
Following are the basic features of MATLAB −- It is a high-level language for numerical computation, visualization and application development.
- It also provides an interactive environment for iterative exploration, design and problem solving.
- It provides vast library of mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, numerical integration and solving ordinary differential equations.
- It provides built-in graphics for visualizing data and tools for creating custom plots.
- MATLAB's programming interface gives development tools for improving code quality maintainability and maximizing performance.
- It provides tools for building applications with custom graphical interfaces.
- It provides functions for integrating MATLAB based algorithms with external applications and languages such as C, Java, .NET and Microsoft Excel.
Uses of MATLAB
MATLAB is widely used as a computational tool in science and engineering encompassing the fields of physics, chemistry, math and all engineering streams. It is used in a range of applications including −- Signal Processing and Communications
- Image and Video Processing
- Control Systems
- Test and Measurement
- Computational Finance
- Computational Biology
MATLAB - Environment Setup
Local Environment Setup
Setting up MATLAB environment is a matter of few clicks. The installer can be downloaded from here −MathWorks provides the licensed product, a trial version and a student version as well. You need to log into the site and wait a little for their approval.
After downloading the installer the software can be installed through few clicks.
Understanding the MATLAB Environment
MATLAB development IDE can be launched from the icon created on the desktop. The main working window in MATLAB is called the desktop. When MATLAB is started, the desktop appears in its default layout −The desktop has the following panels −
- Current Folder − This panel allows you to access the project folders and files.
- Command Window − This is the main area where commands can be entered at the command line. It is indicated by the command prompt (>>).
- Workspace − The workspace shows all the variables created and/or imported from files.
- Command History − This panel shows or rerun commands that are entered at the command line.
Set up GNU Octave
If you are willing to use Octave on your machine ( Linux, BSD, OS X or Windows ), then kindly download latest version from Download GNU Octave. You can check the given installation instructions for your machine.MATLAB - Basic Syntax
MATLAB environment behaves like a super-complex calculator. You can enter commands at the >> command prompt.MATLAB is an interpreted environment. In other words, you give a command and MATLAB executes it right away.
Hands on Practice
Type a valid expression, for example,5 + 5And press ENTER
When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 10Let us take up few more examples −
3 ^ 2 % 3 raised to the power of 2When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 9Another example,
sin(pi /2) % sine of angle 90oWhen you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 1Another example,
7/0 % Divide by zeroWhen you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = Inf warning: division by zeroAnother example,
732 * 20.3When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
ans = 1.4860e+04MATLAB provides some special expressions for some mathematical symbols, like pi for π, Inf for ∞, i (and j) for √-1 etc. Nan stands for 'not a number'.
Use of Semicolon (;) in MATLAB
Semicolon (;) indicates end of statement. However, if you want to suppress and hide the MATLAB output for an expression, add a semicolon after the expression.For example,
x = 3; y = x + 5When you click the Execute button, or type Ctrl+E, MATLAB executes it immediately and the result returned is −
y = 8
Adding Comments
The percent symbol (%) is used for indicating a comment line. For example,x = 9 % assign the value 9 to xYou can also write a block of comments using the block comment operators % { and % }.
The MATLAB editor includes tools and context menu items to help you add, remove, or change the format of comments.
Commonly used Operators and Special Characters
MATLAB supports the following commonly used operators and special characters −Operator | Purpose |
---|---|
+ | Plus; addition operator. |
- | Minus; subtraction operator. |
* | Scalar and matrix multiplication operator. |
.* | Array multiplication operator. |
^ | Scalar and matrix exponentiation operator. |
.^ | Array exponentiation operator. |
\ | Left-division operator. |
/ | Right-division operator. |
.\ | Array left-division operator. |
./ | Array right-division operator. |
: | Colon; generates regularly spaced elements and represents an entire row or column. |
( ) | Parentheses; encloses function arguments and array indices; overrides precedence. |
[ ] | Brackets; enclosures array elements. |
. | Decimal point. |
… | Ellipsis; line-continuation operator |
, | Comma; separates statements and elements in a row |
; | Semicolon; separates columns and suppresses display. |
% | Percent sign; designates a comment and specifies formatting. |
_ | Quote sign and transpose operator. |
._ | Nonconjugated transpose operator. |
= | Assignment operator. |
Special Variables and Constants
MATLAB supports the following special variables and constants:Name | Meaning |
---|---|
ans | Most recent answer. |
eps | Accuracy of floating-point precision. |
i,j | The imaginary unit √-1. |
Inf | Infinity. |
NaN | Undefined numerical result (not a number). |
pi | The number π |
Naming Variables
Variable names consist of a letter followed by any number of letters, digits or underscore.MATLAB is case-sensitive.
Variable names can be of any length, however, MATLAB uses only first N characters, where N is given by the function namelengthmax.
Saving Your Work
The save command is used for saving all the variables in the workspace, as a file with .mat extension, in the current directory.For example,
save myfile
You can reload the file anytime later using the load command.load myfile
MATLAB - Variables
In MATLAB environment, every variable is an array or matrix.You can assign variables in a simple way. For example,
x = 3 % defining x and initializing it with a valueMATLAB will execute the above statement and return the following result −
x = 3It creates a 1-by-1 matrix named x and stores the value 3 in its element. Let us check another example,
x = sqrt(16) % defining x and initializing it with an expressionMATLAB will execute the above statement and return the following result −
x = 4Please note that −
- Once a variable is entered into the system, you can refer to it later.
- Variables must have values before they are used.
- When an expression returns a result that is not assigned to any variable, the system assigns it to a variable named ans, which can be used later.
sqrt(78)MATLAB will execute the above statement and return the following result −
ans = 8.8318You can use this variable ans −
sqrt(78); 9876/ansMATLAB will execute the above statement and return the following result −
ans = 1118.2Let's look at another example −
x = 7 * 8; y = x * 7.89MATLAB will execute the above statement and return the following result −
y = 441.84
Multiple Assignments
You can have multiple assignments on the same line. For example,a = 2; b = 7; c = a * bMATLAB will execute the above statement and return the following result −
c = 14
I have forgotten the Variables!
The who command displays all the variable names you have used.
who
MATLAB will execute the above statement and return the following result −Your variables are: a ans b cThe whos command displays little more about the variables −
- Variables currently in memory
- Type of each variables
- Memory allocated to each variable
- Whether they are complex variables or not
whos
MATLAB will execute the above statement and return the following result −Attr Name Size Bytes Class ==== ==== ==== ==== ===== a 1x1 8 double ans 1x70 757 cell b 1x1 8 double c 1x1 8 double Total is 73 elements using 781 bytesThe clear command deletes all (or the specified) variable(s) from the memory.
clear x % it will delete x, won't display anything clear % it will delete all variables in the workspace % peacefully and unobtrusively
Long Assignments
Long assignments can be extended to another line by using an ellipses (...). For example,initial_velocity = 0; acceleration = 9.8; time = 20; final_velocity = initial_velocity + acceleration * timeMATLAB will execute the above statement and return the following result −
final_velocity = 196
No comments:
Post a Comment