0% found this document useful (0 votes)
62 views15 pages

Introduction To Matlab: Bushra Fatima NED University of Engineering & Technology

This document provides an introduction to MATLAB, covering arithmetic operations with scalars, display formats, scalar variables and their use in calculations, and writing simple script files. It discusses how to perform basic arithmetic in MATLAB, the order of operations, and different display formats for output. It also covers naming rules for defining scalar variables in MATLAB and some predefined variables like Inf and NaN.

Uploaded by

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

Introduction To Matlab: Bushra Fatima NED University of Engineering & Technology

This document provides an introduction to MATLAB, covering arithmetic operations with scalars, display formats, scalar variables and their use in calculations, and writing simple script files. It discusses how to perform basic arithmetic in MATLAB, the order of operations, and different display formats for output. It also covers naming rules for defining scalar variables in MATLAB and some predefined variables like Inf and NaN.

Uploaded by

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

INTRODUCTION TO MATLAB

Bushra Fatima
NED University of Engineering & Technology
May 18, 2020
Outline – Lecture 02

• Arithmetic operations with scalars using MATLAB.

• Display Formats

• Scalar variables and their use in arithmetic


calculations

• Elementary Math built-in functions

• Script files  how to write, save, and execute


simple MATLAB programs

Textbook: A. Gilat, Matlab: An Introduction with Applications 6th Edition. John Wiley
& Sons, Incorporated, 2016. (Chapter 1: Introduction)
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 3 of 15

Simple Arithmetic Operations With Scalars

Addition: 3+2
Subtraction: 3-2
Multiplication: 3*2
Division: 3/2 or 2\3
Exponent: 3^2

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 4 of 15

Order of Precedence

Precedence Operation
First Parentheses, evaluated starting with the
innermost pair
Second Exponentiation
Third Multiplication and division with equal
precedence
Fourth Addition and subtraction with equal
precedence
Note:
If two or more operations have the same precedence, the
expression is executed from left to right.

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 5 of 15

Order of Precedence: Practice

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 6 of 15

Display formats
Command Description Example
>> 290/7
format short
upto 4 decimal digits ans =
(default)
41.4286
>> 290/7
ans =
format long upto 15 decimal digits
41.4285714285
71431
>> 290/7
format short e scientific notation with 4 decimal digits ans =
4.1429e+001
>> 290/7
ans =
format long e scientific notation with 15 decimal digits
4.14285714285
7143e+001

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 7 of 15

Display formats
Command Description Example
>> 290/7
format short g 5 s.g.f of floating digits ans =
41.429
>> 290/7
ans =
format long g 15 s.g.f of floating digits
41.4285714285
714
>> 290/7
format bank upto 2 decimal digits ans =
41.43
Eliminates empty lines to allow more lines with information
format compact
displayed on the screen.
format loose
Adds empty lines (opposite of compact).
(default) METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 8 of 15

Notation ‘e’ in MATLAB

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 9 of 15

Display formats: compact vs loose

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 10 of 15

Variables: Storing data

Save value in a variable:


x=3 = Assignment
x = 4 (over-ridden) operator

Variables are case-sensitive:


T=2
t=3
Z = t+T;

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 11 of 15

Variables: Naming rules …


1. May consist of:
 letters A-Z, a-z
 digits 0-9
 underscore ( _ )

2. Must start with a letter.

3. Variable names are case-sensitive

4. Can be up to 63 characters long

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 12 of 15

Variables: Naming rules …

5. Avoid using the built-in functions as the variable


name e.g. cos, sin, sqrt etc.

6. Cannot use punctuation characters (e.g. period,


comma, semi-colon e.t.c.)

7. No spaces are allowed between characters

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 13 of 15

Variables: Naming rules


8. Following predefined keywords can not be used
as variable names
break for
case function end
catch global return
classdef if spmd
continue otherwise switch
else parfor try
elseif persistent while
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 14 of 15

Variables: Predefined variables in MATLAB

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
ARITHMETIC | DISPLAY FORMATS | VARIABLES 15 of 15

Inf & NaN

Infinity:
1/0

inf + 3
Not-a-Number (NaN):
0/0

METU NCC

You might also like