0% found this document useful (0 votes)
79 views51 pages

Introduction to MATLAB Programming Basics

This document discusses Matlab and its features. It introduces Matlab as a tool for numerical computations and matrices. It describes two approaches for writing Matlab code - using the command window or writing .m files. It also summarizes key interfaces in Matlab like the command window, workspace, and command history.

Uploaded by

Sujan Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
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)
79 views51 pages

Introduction to MATLAB Programming Basics

This document discusses Matlab and its features. It introduces Matlab as a tool for numerical computations and matrices. It describes two approaches for writing Matlab code - using the command window or writing .m files. It also summarizes key interfaces in Matlab like the command window, workspace, and command history.

Uploaded by

Sujan Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
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

Introduction

• MatLab : Matrix Laboratory


• Numerical Computations with matrices
• Every number can be represented as matrix
• Why Matlab?
• User Friendly (GUI)
• Easy to work with
• Powerful tools for complex mathematics
Change the current
directory to the
location of your
Matlab file at
startup
Workspace

Command
History Command Window
Matlab Programming
• Two Approaches
– Coding in Command Window
• Good for short programs
• Commands must be re-entered each time you run
a simulation

– Coding with .m-files


• Good for long programs
• Allows users to save all the commands written in
the .m-files
Command Window Interface

Write your
commands here
(1) Creating .m-files

File  New  M-
File

(2) Opening .m-files


File  Open 
select the .m-file
.m-file Interface

Write your
commands here

To run the program, press F5


Workspace Interface
• The workspace provides a summary of the
variables
Command History Interface
• The Command History stores all the commands
entered in the Command Window previously
– No history of .m-files is shown here
Syntax Errors
• All errors are given in the Command Window in
red
– Nature of error is explained
MATLAB AS A CALCULATOR

Command window

>> 39*4.4+5

ans =
176.6000 The MATLAB command
Command window

The result.
MATLAB
• Variable names:
– Starts with a letter
– Up to 31 characters ( some use 19 or
21)
– May contain letters, digits and
underscore_
– Case sensitive (“A” is not the same as
“a”)
MATLAB Assignment
• Variable names:
– Starts with a letter
– Up to 31 characters ( some use 19 or 21)
– May contain letters, digits and underscore_
– Case sensitive (“A” is not the same as “a”)
» A=2.3
A=
The MATLAB command
2.3000
This is the result of the
MATLAB statement
Scalar Assignment

» A=2.3 this creates a variable “A”


A= and set its value to 2.3
2.3000
» A=[2.3] The square braces [ ] are
used to define matrices.
A=
We can use them for
2.3000 scalars too.
Row vector
The square braces are
used to define a matrix

» X=[2,3 7 ] Space or comma are


used to separate
X= elements in the same row
2 3 7
Column vector
The square braces are
used to define a matrix

» X=[2;3 ; 7 ] semicolon are used to end


X=
a row.
2
3
You can also use ENTER
7 to end a row
MATLAB Statements
MATLAB Statement Remarks
C=5.66 C is a scalar

C=[5.66] An alternative way

X=[3.5 6.3, 33] X is a 1X3 matrix with elements 3.5 ,


6.3 and 33. Commas or space are used
to separate the elements in a row
Y=[1 Y is a 2X1 matrix whose elements are 1
and 4.
4]
Y = [ 1 ; 4] Semicolon are used to indicate the end
of the row.
A=1:5 Equivalent to A=[1 2 3 4 5]
MATLAB Statements
MATLAB Remarks
Statement
 2 3 5
V=[ 2 3 5 V  
 3 3 8 
3 3 8]
C=[1:3:11] C=[1 4 7 10]

Z=4\8 Z=2
1 0 
Y=eye(2) V  
0 1 

W = zeros(2,3) 0 0 0 
V  
 0 0 0 
Help
• A good idea is use the help
• help provides information about the
available functions and how to use
them.
• Try
help eig
help inv
help roots
Making the best from Matlab
• Need help ?
• HELP <function name>
• LOOKFOR <key word>
• Case sensitive
• M files (.m)
• To write and save matlab commands
• Save time and easy to debug
• Use of semicolon (;) Examples during Matlab demo
• Comments (%)
ARITHMETIC OPERATIONS
Operation Symbol Example

Addition + 6+3=9
Subtraction - 6-3=3

Multiplication * 6*3=18

Right division / 6/3=2


Left division \ 3/63=0.5

Exponentiation ^ 6^3=216
DISPLAY FORMATS
Command Description Example

Short Fixed-point with 351/7


4 decimal digits Ans=50.1429

Long Fixed-point with 351/7


14 decimal digits Ans=50.14285756121456
Short e scientific notation with 351/7
4 decimal digits Ans=5.0143e+001

Long e scientific notation with 351/7


15 decimal digits
Ans=5.014284564654654e001

Short g Best of 5 digit fixed 351/7


or floating pont Ans=50.143

Lond g Best of 15 digit fixed 351/7


or floating point Ans=50.1428546874567
Bank Two decimal digits 351/7=50.14

Compact : Eliminates empty lines to allow more lines with


information
displayed on the screen

Loose : Adds empty lines (opposite of compact)


Elementary MATH Built-in
Functions
 Common Math functions.
 Exponential functions.
 Trigonometric and hyperbolic functions.
 Round-off functions.
 Complex number functions.
Arithmetic Operations with complex
numbers.
Common Math functions
Function Description
abs(x)  compute the absolute value of ‘x’

Ex: i) x=4;
abs(x)=4
ii) x=4+3i
abs(x)=25
iii) x=3i
abs(x)=3
Sqrt(x)  computes the square root of ‘x’

Ex: x=4
sqrt(x)=2

Round(x)  Rounds ‘x’ to nearest integer.


Ex :
i) x=12.56
round(x)=13
ii) x=12.46
round(x)=12
Fix(x)  rounds ‘x’ to the nearest integer towards 0.

ex : x=12.3
fix(x)=12
x=12.99
fix(x)=12
Floor(x)  rounds ‘x’ to the nearest integer towards -∞

Ex : x=12.99
floor(x)=12
x=11.23
floor(x)=11
Ceil  rounds x to the nearest integer toward ∞.

ex : i) x=12.1
ceil(x)=13
ii) x=22.4
ceil(x)=23

Sign(x)  returns the value of ‘-1’ if ‘x’ is less than ‘0’


“ “ ‘0’ if ‘x’ is equals ‘0’
otherwise returns the value ‘1’
Ex : x=232 x=0 x=-234
sign(x)=1 sign(x)=0 sign(x)=-1
Rem(x,y)  returns the remainder of x/y.

Ex : x=25; y=4
rem(x,y)=1

Exp(x) computes e^x.

Ex : x=4
exp(x)=54.5982
Log  computes ln ‘x’  natural logarithm, base ‘e’

Ex : x=12
log(x)=2.4849

Log10(x)  computes log10(x)  common logarithm,


base’10’

Ex : i) x=12
log10(x)=1.0792
x=10
log10(x)=1.0000
EXPONENTIAL FUNCTIONS
Function Description
exp(x) Exponential

log(x) Natural logarithm

log10(x) Base 10 logarithm

sqrt(x) Square root


Trigonometric functions

Function Description

sin(x)  Computes the sin of ‘x’, where ‘x’ is in radians.

cos(x)  “ “ cosine of ‘x’, “ “

tan(x)  “ “ tangent of ‘x’ “ “

cot(x)  “ “ cot of ‘x’ “ “


sind(x)  computes the sin of ‘x’, where ‘x’ in degrees.

lly……. Cosd(x), tand(x), cotd(x).

asin(x)  computes the inverse sin of ‘x’, where ‘x’ in radians.

‘x’ must be between -1 and 1, & angle b/w –Π/2 and Π/2.

acos(x)  compute the inverse of ‘x’, where ‘x’ in radians.

‘x’ must be between -1 and 1, & angle b/w 0 and Π.

atan(x) 
• asind(x)  compute the inverse sin of
‘x’, where ‘x’ in degrees.

• lly…….. acosd(x),
atand(x), acotd(x).
Hyperbolic functions

You might also like