0% found this document useful (0 votes)
14 views31 pages

Matlab Leason One

MATLAB is a high-level programming language and interactive environment designed for numerical computation, visualization, and programming, featuring extensive built-in functions for various mathematical operations. It is widely utilized in science and engineering for applications such as signal processing, image processing, and computational finance. The environment includes a command window for immediate execution of commands, a workspace for variable management, and tools for creating custom graphical interfaces.

Uploaded by

jaraemmanuel562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

Matlab Leason One

MATLAB is a high-level programming language and interactive environment designed for numerical computation, visualization, and programming, featuring extensive built-in functions for various mathematical operations. It is widely utilized in science and engineering for applications such as signal processing, image processing, and computational finance. The environment includes a command window for immediate execution of commands, a workspace for variable management, and tools for creating custom graphical interfaces.

Uploaded by

jaraemmanuel562
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

MATLAB

Introduction to the enviroment


MATLAB
INTRODUCTION
MATLAB (matrix laboratory) is a fourth-generation high-level
programming language and interactive environment for numerical
computation, visualization and programming.
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 area 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
• 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
INSTALLING MATLAB
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:
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.
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 + 5 And press ENTER
• When you click the Execute button, or type Ctrl+E,
MATLAB executes it immediately and the result
returned is:
• ans = 10
MORE EXAMPLES
• >>3*3
• ans 27
• >>4/2
• ans 2
• >>3^3
• ans 27
• >> 9 – 2
• ans 7
• >>7/0
• ans Inf
• Etc.
• MATLAB 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'.
Command History
• This panel shows or rerun commands that
are entered at the command line.
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,
– 5+3;
– 5+8;
Adding comments
• The percent symbol (%) is used for
indicating a comment line. For example,
• 9+9 % adding two numbers
• You can also write a block of comments
using the block comment operators %
{ and % }.
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.
/
Commonly used Operators and Special Characters
Operator Purpose
./ 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


Commonly used Operators and Special Characters

; Semicolon; separates columns and


suppresses display.

% Percent sign; designates a comment


and specifies formatting.

‘ Quote sign and transpose operator.

.’ Non-conjugated transpose operator.

= Assignment operator.
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.
VARIABLES
• In MATLAB environment, every variable is an array or
matrix.
• You can assign variables in a simple way. For
example
Example 1.
• x = 7 % defining x and initializing it with a value
• MATLAB will execute the above statement and return
the following result:
• x=
3
• It creates a 1-by-1 matrix named x and stores the
value 3 in its element.
• Example 2.
• x = sqrt(16) % defining x and initializing it with an
expression
• MATLAB will execute the above statement and return
the following result:
• x=4
• 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.
• For example,
sqrt(78)
• MATLAB will execute the above statement and return
the following result:
ans =
8.8318
• You can use this variable ans:
ans * 56
Result
ans =

494.5786
The who and whos command

• The who command displays all the variable


names you have used.
• The 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
Clear and long assignment
commands
• The 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
• Long assignments can be extended to another line by
using an ellipses (...).
• Example
• Initial_velocity = 0;
• acceleration = 9.8;
• time = 20;
• Final_velocity = initial_velocity ...
+ acceleration * time
The format Command
• By default, MATLAB displays numbers
with four decimal place values. This is
known as short format.
• However, if you want more precision, you
need to use the format command.
• The format long command displays 16
digits after decimal.
• The format bank command rounds
numbers to two decimal places.
EXAMPLES
• Example
• format long
x = 7 + 10/3 + 5 ^ 1.2 ans =
• Example
• format short
x = 7 + 10/3 + 5 ^ 1.2 ans =
• format blank
x = 7 + 10/3 + 5 ^ 1.2 ans =
• format bank
• daily_wage = 177.45;
• weekly_wage = daily_wage * 6 ans =
• MATLAB displays large numbers using exponential
notation.
• The format short e command allows displaying in
exponential form with four decimal places plus the
exponent.
• The format long e command allows displaying in
exponential form with four decimal places plus the
exponent.
• The format rat command gives the closest rational
expression resulting from a calculation.
Examples
• Example
• format short e
4.678 * 4.9
• Example
• format long e
x = pi
• Example
• format rat
x=4.678*4.9
Help Command
• help, by itself, lists all primary help topics.
Each primary topic corresponds to a
folder name on the MATLAB search path.
• help NAME displays the help for the
functionality specified by NAME, such as a
function, operator symbol, method, class,
or toolbox. NAME can include a partial
path.

You might also like