0% found this document useful (0 votes)
24 views

Bushra Fatima NED University of Engineering & Technology: April 11, 2017

This document provides an introduction to MATLAB. It discusses the default MATLAB desktop view including the command window, command history window, and workspace window. It also covers basic MATLAB commands like assigning scalar variables, arithmetic calculations, using built-in math functions, and viewing command help. The document is intended to familiarize users with MATLAB's basic features and capabilities for scientific and engineering calculations.

Uploaded by

Juzer Shabbir
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)
24 views

Bushra Fatima NED University of Engineering & Technology: April 11, 2017

This document provides an introduction to MATLAB. It discusses the default MATLAB desktop view including the command window, command history window, and workspace window. It also covers basic MATLAB commands like assigning scalar variables, arithmetic calculations, using built-in math functions, and viewing command help. The document is intended to familiarize users with MATLAB's basic features and capabilities for scientific and engineering calculations.

Uploaded by

Juzer Shabbir
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/ 38

MATLAB TUTORIAL LAB 01

INTRODUCTION TO MATLAB

Bushra Fatima
NED University of Engineering & Technology
April 11, 2017
Outline
• Default view of MATLAB desktop

• Command Window

• Command History.

• Scalar variables and their use in arithmetic


calculations

• Elementary Math built-in functions

Textbook: A. Gilat, Matlab: An Introduction with Applications 4th Edition. John Wiley
& Sons, Incorporated, 2017. (Chapter 1: Introduction)
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 3 of 38

Introduction
• MAT/LAB: Matrix Laboratory

• Makes matrix computations easy

• Handles scientific and engineering calculations.

• MATLAB system is an Interpreter

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 4 of 38

Prerequisites for solving a problem using MATLAB

• Mathematics

• An algorithm

• The exact rules for writing MATLAB statements

• Correct usage of MATLAB utilities

• Converting algorithm into MATLAB statements


and/or program

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 5 of 38

Default view of MATLAB desktop

CURRENT WORKSPACE
FOLDER WINDOW
WINDOW
COMMAND WINDOW

COMMAND HISTORY
WINDOW

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 6 of 38

Default view: Home  Layout  Default

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 7 of 38

Command Window: Executing commands one at a time


• Main window: For executing commands
Command 1: A=5
press enter

To execute
Command line: The line with command prompt >>
Command 2: B=10 each
press enter
command:
Command 3: C=A+B
Press Enter
press enter

The cursor next to the command


prompt is prompting the user to enter
the next Command

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 8 of 38

Command Window: Executing several commands

3 commands in the same


line separated by a
comma
Order of
execution:
Left to Right

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 9 of 38

Command Window: Editing

• Editing:

The already executed command can not be corrected


and re-executed

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 10 of 38

Command Window: Editing

Command-line editing:

• Left-arrow, Right-arrow

• Up-arrow and Down-arrow for accessing history

• Smart recall: type some character and press


Up-arrow and Down-arrow

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 11 of 38

Command Window: Use of percent symbol (%)

Comment
Comment line is not executed when ENTER is
pressed

Comments are added to aid the programmer for his/her reference


METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 12 of 38

Command Window: Use of Semi-colon (;)

Output of the command is displayed

Output of the command


is not displayed
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 13 of 38

Command Window: Use of Semi-colon (;)

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 14 of 38

Command Window: Use of the command clc

clc  clears the command window only

CAUTION: The commands are CASE-SENSITIVE! METU NCC


INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 15 of 38

Drawbacks of a command window

• The commands in the Command window


cannot be saved and executed again.

• It is not interactive.

• Already executed command can not be


modified

• Better option: script file


METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 16 of 38

Command Window: Font size: Home  Preferences  Fonts

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 17 of 38

Useful commands you should know


CAUTION: The commands are CASE-SENSITIVE!

• help  lists all the primary help topics.

• help <command>  displays information about the command.

• doc On-line help  accesses the hypertext reference manual.

• doc <command>  displays information about the command.

• type <command>  Displays the actual MATLAB code for this


command.

• lookfor <keyword>  Searches all MATLAB commands for this


keyword.

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 18 of 38

Useful commands you should know

CAUTION: The commands are CASE-SENSITIVE!

• who  Lists all the current variables.

• whos  Lists all the current variables in more detail than who.

• save  Saves all of your variables.

• ^C  Abort the command which is currently executing (i.e., hold down


the control key and type c).

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 19 of 38

Useful commands you should know

CAUTION: The commands are CASE-SENSITIVE!

• clc  clears the command window (but does not clear the saved
variables in the workspace)

• clear all  deletes all the saved variables from the workspace (but
does not clear the command window) and brings the cursor at top

• clear variable  deletes only that particular variable from the


workspace

• date  gives current date

• calendar shows up the calendar of the ongoing month

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 20 of 38

Busy…

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 21 of 38

Command History

• The Command History Window  includes commands from previous


sessions.

• A command in the Command History Window can be used again in the


Command Window.

• By double-clicking on the command, the command is reentered in the


Command Window and executed.

• It is also possible to drag the command to the Command Window, make


changes if needed, and then execute it.

• Clearing Command history:


• Whole history
• A selection

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 22 of 38

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
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 23 of 38

Order of Precedence

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 24 of 38

Order of Precedence: Practice

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 25 of 38

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
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 26 of 38

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
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 27 of 38

Display formats: compact vs loose

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 28 of 38

Inf & NaN

Infinity:
1/0

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

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 29 of 38

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
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 30 of 38

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

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


name e.g. cos, sin, sqrt etc. METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 31 of 38

Variables: Predefined keywords that can not be used as


variable name

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
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 32 of 38

Variables: Predefined variables

ans
pi

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 33 of 38

Elementary math built-in functions

A function has a name and an argument in parentheses.

a= 4
a = sqrt(4)

METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 34 of 38

Elementary functions in MATLAB: Trigonometry


Function Description Example
>> sin(pi/6)
sin(x) Sine of angle x (x in radians).
ans =
sind(x) Sine of angle x (x in degrees).
0.5

cos(x) Cosine of angle x (x in radians). >> cosd(30)


cosd(x) Cosine of angle x (x in degrees). ans =
0.86603

tan(x) Tangent of angle x (x in radians). >> tan(pi/6)


tand(x) Tangent of angle x (x in degrees). ans =
0.57735
>> cotd(30)
cot(x) Cotangent of angle x (x in radians).
ans =
cotd(x) Cotangent of angle x (x in degrees).
1.7321 METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 35 of 38

Elementary functions in MATLAB: Trigonometry

• Inverse trigonometric functions are:

asin(x), acos(x), atan(x), acot(x) for the angle in


radians.
asind(x), acosd(x), atand(x),acotd(x) for the angle
in degrees.

• Hyperbolic trigonometric functions are

sinh(x), cosh(x), tanh(x), and coth(x).


METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 36 of 38

Elementary functions in MATLAB: Exponential

Function Description Example


>> sqrt(81)
sqrt(x) square root ans =
9
>> nthroot(80,5)
nthroot(x,n) Real nth root of a real number x. ans =
2.4022
>> exp(5)
exp(x) Exponential ex ans =
148.41
>> log(1000)
log(x) Natural logarithm. ans =
Base e logarithm (ln). 6.9078
>> log10(1000)
log10(x) Base 10 logarithm. ans =
3
METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 37 of 38

Elementary functions in MATLAB: Rounding & Remainder


Function Description Example
>>
round(3/2)
round(x) Rounds to the nearest integer.
ans =
2
>> fix(3/2)
fix(x) Rounds towards zero. ans =
1
>>
ceil(3/2)
ceil(x) Rounds towards +ve infinity.
ans =
2
>>
floor(3/2)
floor(x) Rounds towards -ve infinity.
ans =
1
>>
rem(35,3)
rem(x,y) Returns the remainder after x is divided by y.
ans =
2
>> sign(5)
sign(x) Signum function. Returns 1 if x > 0, -1 if x < -1 & 0 if x = 0 ans =
1 METU NCC
INTRODUCTION TO MATLAB BUSHRA FATIMA
INTRO | COMMAND WINDOW | COMMAND HISTORY | ARITHMETIC | VARIABLES | FUNCTIONS 38 of 38

Practice …..

fix and rem


Converting 40 inches to feet-inch.

feet = fix(40/12)
inches = rem(40, 12)

METU NCC

You might also like