MATLAB PPT
MATLAB PPT
(MTH563)
Rahul Kumar
Assistant Professor
Department of Mathematics
Lovely Professional University
Table of Contents
1 Course Introduction
Course Details
Course Outcomes
Evaluation & Task Details
Syllabus
Textbooks
2 Computational Techniques
Introduction
Why this course?
How this course?
3 MATLAB
Introduction to MATLAB
MATLAB Environment
4 Basic Syntax and General Purpose Commands
Basic Syntax
General Purpose Commands
Rahul, Asstt. Prof. (LPU)
MTH563 3/38 Lecture : MATLAB
Table of Contents
5 Common Formatting MATLAB Functions
6 Managing a session
9 Files in MATLAB
Course Details
Course Outcomes
Evaluation Weightage
Attendance : 5
CA : 45 (best 2 of 3)
MTP : NA
ETP : 50
Total : 100
Rahul, Asstt. Prof. (LPU) Course Introduction Evaluation & Task Details
MTH563 7/38 Lecture : MATLAB
Syllabus
1 To learn working in MATLAB with a brief overview.
2 Implementing common formatting MATLAB functions.
3 To perform mathematical calculation in MATLAB.
4 To work with script files and learn the execution of programs.
5 To create vectors and arrays in MATLAB.
6 To create special arrays and perform operations on arrays.
7 To use inbuilt functions for array handling.
8 To solve system of simultaneous linear equations.
9 To create and edit graphs in MATLAB.
10 To plot graph of a mathematical equation in 2D & 3D.
11 To create and execute a function file in MATLAB.
12 Designing program in MATLAB using control statements(if-else).
13 Designing program using loop control statements (for loop).
Rahul, Asstt. Prof. (LPU) Course Introduction Syllabus
MTH563 8/38 Lecture : MATLAB
Syllabus
Textbooks
Introduction
The first step to get started with computational techniques is understanding the basics of
Programming. Computational techniques involve writing codes/programs to perform
calculations and simulations.
Therefore, a programming language like MATLAB which is widely used in scientific computing
and has a rich ecosystem of libraries for various computational tasks is required.
Introduction to MATLAB
Features of MATLAB
Applications 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 (but not limited to)
Signal Processing and Communications
Image and Video Processing
Control systems
Test and measurement
Computational Finance
Computational biology
Disadvanteges
It is an interpreted language and therefore may execute more slowly than compiled
languages.
Cross-compiling of Matlab code to other languages is very difficult and requires deep
Matlab knowledge to deal with errors produced.
Matlab is more expensive. The license is very costly, and users need to buy each and every
module and need to pay for the same.
Rahul, Asstt. Prof. (LPU) MATLAB Introduction to MATLAB
MTH563 18/38 Lecture : MATLAB
MATLAB Environment
MATLAB Environment To begin MATLAB, double-click the MATLAB icon on the computer’s
desktop or select MATLAB from the Start or Program menu. Immediately a special window
called the MATLAB desktop appears as shown below:
When MATLAB opens, a special window called the MATLAB desktop appears. This desktop
contains some sub-windows namely
Command Window: A command window is a place in which a user can enter interactive
commands at the command prompt (») to execute certain files or basic operations, and
they will be executed on the spot.
Workspace: It is the place which contains all the variables that can be used by MATLAB
when a particular command, M-file, or function is executed.
Current Directory: The current directory shows the location where the program is held
or the directory from where the current execution is running.
Basic Syntax
Basic Syntax
MATLAB environment behaves like a super-complex calculator. You can enter commands at
the command prompt (»).
Since MATLAB is an interpreted environment, as you give a command, it executes it right away.
For example, type a valid expression in command window and pess enter to see the answer.
» 5 + 5 or 3 ∧ 2 or sin(pi/2) or 7/0 or 732 ∗ 20.3.
Rahul, Asstt. Prof. (LPU) Basic Syntax and General Purpose Commands Basic Syntax
MTH563 21/38 Lecture : MATLAB
Rahul, Asstt. Prof. (LPU) Basic Syntax and General Purpose Commands Basic Syntax
MTH563 22/38 Lecture : MATLAB
Basic Operators
Operator Description
, separates statements and elements in a row.
; separates columns and suppresses display.
% designates a comment and specifies formatting.
’ Quote sign and transpose operator.
.’ Nonconjugated transpose operator.
= Assignment operator.
Rahul, Asstt. Prof. (LPU) Basic Syntax and General Purpose Commands General Purpose Commands
MTH563 24/38 Lecture : MATLAB
Relational Operators
Operator Description
< Less Than
<= Less Than or Equal
> Greater Than
>= Greater Than or Equal
== Equal To
∼= Not Equal TO
Logical Operators
Name Description
∼ NOT
& AND
| OR
Rahul, Asstt. Prof. (LPU) Basic Syntax and General Purpose Commands General Purpose Commands
MTH563 25/38 Lecture : MATLAB
Try the following commands with above format commands and see the results.
x = 7 + 10/3 + 5∧ 1.2
x = pi
x = sqrt(2)
Rahul, Asstt. Prof. (LPU) Common Formatting MATLAB Functions
MTH563 26/38 Lecture : MATLAB
MATLAB provides various commands for managing a session. The following are some
important commands:
Command Description
clc Clears command window.
clear Removes variables from memory.
exist Checks for existence of file or variable.
global Declares variables to be global.
help Searches for a help topic.
lookfor Searches help entries for a keyword.
who Lists current variables.
whos Lists current variables (long display).
quit Stops MATLAB.
MATLAB has a number of built-in functions, such as square root, sine, cosine, exponential,
etc, functions. The following table gives a list of some commonly used functions:
Command Description
abs finds absolute value of all elements in the matrix
sign signum function
sin,cos,. . . Trignometric functions
asin,acos. . . Inverse trignometric functions
exp Exponential
sqrt Square root a number
rem(y,x) Remainder after dividing y by x
sign(x) Returns sign (+/-) of a number
Command Description
log,log10 natural logarithm, logarithm (base 10)
ceil,floor round towards +infinity, -infinity respectively
round round towards nearest integer
real,imag real and imaginary part of a complex matrix
sort sort elements in ascending order
sum,prod summation and product of elements
max,min maximum and minimum of arrays
mean,median average and median of arrays
std,var Standard deviation and variance
Script Files
Scripts files are files that contain multiple sequential lines of MATLAB commands and function
calls that can be executed together. These are simplest kind of code file because they have no
input or output arguments. The extension of script files is (.m)
A script file can be created in following three ways:
Highlight commands from the Command History, right-click, and select Create Script.
On the Home tab, click the New Script button.
Use the edit function. For example, the command
» edit new _file_name
creates (if the file does not exist) and opens the file new _file_name (if the file exist).
If new _file_name is unspecified, MATLAB opens a new file called Untitled.
Run File
After creating and saving the file, you can run it in two ways:
Clicking the Run button on the editor window or
Typing the filename (without extension) in the command prompt.
» prog1
Press enter and see the result.
Rahul, Asstt. Prof. (LPU) Files in MATLAB
MTH563 33/38 Lecture : MATLAB
Function Description
edit Edit or create file
input Request user input
publish Generate view of MATLAB file in specified format
grabcode Extract MATLAB code from file published to HTML
snapnow Take snapshot of image for inclusion in published document
Following shortcut keys can also be used to comment and uncomment the code:
Windows: Ctrl+R to comment and Ctrl+T to uncomment.
macOS: Command+/ to comment and Command+Option+/ to uncomment.
Linux: Ctrl+/ to comment and Ctrl+Shift+/ to uncomment.
MATLAB code files often contain many commands and lines of text. You typically focus your
efforts on a single part of your code at a time, working with the code and related text in pieces.
For easier document management and navigation, divide your file into sections. Then, you can
run the code in an individual section and navigate between sections, as needed.
Creating Sections
To create a section,
go to the Editor or Live Editor tab and in the Section section, click the Section Break
button.
You also can enter two percent signs (%%) at the start of the line where you want to
begin the new section.
The new section is highlighted with a blue border, indicating that it is selected. If there is only
one section in your code file, the section is not highlighted, as it is always selected.