SlideShare a Scribd company logo
INTRODUCTION
to
MATLAB
By: Prof. Ganesh Ingle
Session objective
COMPUTERS AND PROGRAMMING
PROGRAMMING LANGUAGES TYPES
MATLAB INTRODUCTION
IDE ,COMMANDS ,RESULTS ,M FILE
COMPARE A SAMPLE CODE IN C WITH MATLAB
SUMMARY
Computers and Programming
1. Computer: An electronic device which
is capable of performing complex
arithmetical and logical operations to
get the desired result
2. Program: Set of instructions given to
the computer to execute a particular
task.
Programming Languages Types
1. Machine level language
2. Assembly level language (Assembler)
3. High level language (Compiler interpreter)
MATLAB Introduction
1. What is MATLAB ???
2. Why MATALB???
a. Interfacing API for
TMS320(DSP),ARM,89s51,89s52,…..
b. Port interfacing
c. Reduces time required for PDLC
d. Scientific calculations
MATLAB Applications
1. Communication engineering
2. AI,ML,DL
3. Image processing
4. Optimization
5. Data access ,analysis and visualization
6. Programming and algorithm designing
7. Performance analysis of algorithms
8. Simulink
MATLAB Commands
1. date
2. clock
3. ver
4. whos
5. what(files in current dir)
6. clc
7. clear
8. diary filename
9. diary off
10. diary on
11. path
12. editpath GUI ,home,set path
13.! DOS command
14. exit
MATLAB Operators/Symbols/Functions
1. +
2. -
3. *
4. /
5. ^
6. =
7. ==
8. pi
9. vpa (digit precision)
10. %
11. ;
12. : range
13.randn() %10,
1:1:10;
14. Ctrl+c
15. ceil
16. floor
17. sqrt
18. Matrix (+,-,*)
19. ‘.’ operator
20. eye
21. zeros(5)
22. ` transpose
23. ones(6)
MATLAB Operators/Symbols/Functions
24. imread()
25. imshow
26. disp()
27. input()
28. factorial()
29. length(a)
30. size(a)
31. fprintf()-only o/p
32. sprintf()-o/p with save in workspace
33. format rat/ long /hex /shortEng/logEng
34. de2bi, d=(1:10)’; b=de2bi(d); [d,b]
35. decimalToBinaryVector(x);MSB,LSB
MATLAB Operators/Symbols/Functions
36. All trigonometric values and error o/p
37. cosd(),sind()
38. All hyperbolic trigonometric values
39. mod(x,y)
40. log(x) %natural log
41. log10(x)
MATLAB Variables and Data Types
Data types
Logical Char Numeric
Int 8 unint 8
Int 16 unint 16
Int 32 unint 32
Int 64 unint 64
Single Double
Structure Function Handle
Typing Mathematical Expression
1)
R1=
−𝑏+ 𝑏2−4𝑎𝑐
2𝑎
R2=
−𝑏± 𝑏
2
−4𝑎𝑐
2𝑎
2)
R1= 𝑎2 + 𝑏2
e-0.2t cos 2t
3)
E=mc2
Y=1 +
𝑛𝑥
1!
+
𝑛 𝑛−1 𝑥2
2!
MATLAB Code File
1. File name should not start with number
2. No Space in the file name
3. No library function name
4. No use of keywords reserved by MATALAB
MATLAB Searching Mechanism
1. Workspace
2. Current directory
3. Set path directory
Compare code in C with MATLAB
1. WAP program to perform addition of two
numbers a and b with following constraint:
 Do not use + operator i.e. c=a+b
 Do not use –(-) operator i.e. c=a-(-b)
 Do not use x(-1) i.e. c=a-b(-1)
 Do not use c=b-(-a)
 Do not c=b-(-a)
 Do not use c=a-
𝟏
𝟏
−𝒃
 Do not use c=b-
𝟏
𝟏
−𝒂
 Do not use c=
𝒂 𝟐−𝒃 𝟐
𝒂−𝒃
Objectives:
1. Find the solution and
logic
2. Compare C and Matlab
code
MATLAB Programming Exercise
1. WAP to find the roots of the quadratic
equations
2. Ideal gas law problem to find volume
3. Let us see direct implementation using
MATLAB………..
THANK YOU
Image Source
searchenterpriseai.techtarget.com
wikipedia
Matlab Commands
With Example
%Matrix computations and operations
>> %creating vectors and matrices
>> %create a row vector, we can form a row vector.
>> a=[1 3 6 8];
>> a
a =
1 3 6 8
MATLAB Command With Syntax
>> %create a column vector,
>> b=[1;4;6;10];
>> b
b =
1
4
6
10
MATLAB Command With Syntax
>>Create the matrix x of 3 rows and 4 columns
x=[12 23 21 3; 2 34 5 7; 31 32 33 34];
>> x
x =
12 23 21 3
2 34 5 7
31 32 33 34
MATLAB Command With Syntax
%elements of a Matrix
>> z=x(2,3)
z = 5
>> x1=x(1,:)
x1 = 12 23 21 3
>> x2=x(:,2)
x2 =
23
34
32
>> s=size(x)
s = 3 4
MATLAB Command With Syntax
Remove the n th
X(n, : ) = [ ]
x(2, : ) = [ ]
Add a column of 4,s to the end of matrix.
x( ; , 4) = 4
MATLAB Command With Syntax
We can extend the colon notation to specify a
sequence
Create a vector v which starts at 1, with increment
of 2 and stops at 10:
v=1:2:10
v = 1 3 5 7 9
>> v=1:10
v = 1 2 3 4 5 6 7 8 9 10
MATLAB Command With Syntax
x=[11 12 13 14; 21 22 23 24;31 32 33 34];
>> x
x =
11 12 13 14
21 22 23 24
31 32 33 34
We can use this vector notation when
referring to sub matrix:
w=x(1:2:3, 2:4)
MATLAB Command With Syntax
w =
12 13 14
32 33 34
Rows :start at 1, increment by 2, stop at 3
Columns 2,3,4start at 2, stop at 4 default
increment of 1 is used
MATLAB Command With Syntax
x=[11 12 13 14;21 22 23 24 ;31 32 33 34 ]
x =
11 12 13 14
21 22 23 24
31 32 33 34
>> x4=x(1:2:3,2:4)
x4 =
12 13 14
32 33 34
MATLAB Command With Syntax
x4=x(1:2:2,2:4)
x4 =
12 13 14
>> x4=x(1:1:3,2:4)
x4 =
12 13 14
22 23 24
32 33 34
MATLAB Command With Syntax
x4=x(1:1:2,2:4)
x4 =
12 13 14
22 23 24
x4=[x;41 42 43 44]
x4 =
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
x= [41 42 43 44;x] ?
MATLAB Command With Syntax
x4 = x(2:1:3,2:4)
x4 =
22 23 24
32 33 34
x(4 , : ) = [41 42 43 44]
x =
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
MATLAB Command With Syntax
x4=x(1:1:2,1:4)
x4 =
11 12 13 14
21 22 23 24
>> x4=x(1:1:2,2:4)
x4 =
12 13 14
22 23 24
>> x4=x(1:1:2,3:4)
x4 =
13 14
23 24
MATLAB Command With Syntax
Strings
String handling
Syntax
S = 'Any Characters'
S = [S1 S2 ...]
S = strcat(S1, S2, ...)
MATLAB Command With Syntax
Examples
Create a simple string that includes a single quote.
msg = 'You''re right!'
msg =
You're right!
Create the string name using two methods of
concatenation.
name = ['Thomas' ' R. ' 'Lee']
name = strcat('Thomas',' R.',' Lee')
MATLAB Command With Syntax
Create a vertical array of strings.
C = strvcat('Hello','Yes','No','Goodbye‘)
C =
Hello
Yes
No
Goodbye
MATLAB Command With Syntax
S = {'Hello' 'Yes' 'No' 'Goodbye'}
S =
'Hello' 'Yes' 'No' 'Goodbye'
Create a cell array of strings.
S = {'Hello' 'Yes' 'No' 'Goodbye}
MATLAB Command With Syntax
1. Commands for Managing a Session
2. MATLAB provides various commands for managing a session.
3. The following table provides all such commands −
4. I have forgotten the Variables!
5. The who command displays all the variable names you have
used.
6. who
7. MATLAB will execute the above statement and return the
following result
MATLAB Command With Syntax
>> who
Your variables are:
a b x
>> whos
Name Size Bytes Class Attributes
a 1x4 32 double
b 4x1 32 double
x 3x4 96 double
MATLAB Command With Syntax
whos
Name Size Bytes Class Attributes
a 1x4 32 double
b 4x1 32 double
x 3x4 96 double
MATLAB Command With Syntax
Basic Task: Plot the function sin(x) between 0≤x≤4π
 Create an x-array of 100 samples between 0 and 4π.
 Calculate sin(.) of the x-array
 Plot the y-array
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(y)
0 10 20 30 40 50 60 70 80 90 100
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
MATLAB Command With Syntax
Plot the function e-x/3sin(x) between 0≤x≤4π
 Create an x-array of 100 samples between 0 and 4π.
 Calculate sin(.) of the x-array
 Calculate e-x/3 of the x-array
 Multiply the arrays y and y1
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>y1=exp(-x/3);
>>y2=y.*y1;
MATLAB Command With Syntax
Plot the function e-x/3sin(x) between 0≤x≤4π
 Multiply the arrays y and y1 correctly
 Plot the y2-array
>>y2=y.*y1;
>>plot(y2)
0 10 20 30 40 50 60 70 80 90 100
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
MATLAB Command With Syntax
Display Facilities
 plot(.)
 stem(.)
Example:
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(y)
>>plot(x,y)
Example:
>>stem(y)
>>stem(x,y)
0 10 20 30 40 50 60 70 80 90 100
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0 10 20 30 40 50 60 70 80 90 100
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
MATLAB Command With Syntax
Display Facilities
 title(.)
 xlabel(.)
 ylabel(.)
>>title(‘This is the sinus function’)
>>xlabel(‘x (secs)’)
>>ylabel(‘sin(x)’)
0 10 20 30 40 50 60 70 80 90 100
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
This is the sinus function
x (secs)
sin(x)
MATLAB Command With Syntax
Operators (relational, logical)
 == Equal to
 ~= Not equal to
 < Strictly smaller
 > Strictly greater
 <= Smaller than or equal to
 >= Greater than equal to
 & And operator
 | Or operator
 ~ NOT
MATLAB Command With Syntax
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 π
Special Variables and Constants
MATLAB supports the following special variables and constants:
MATLAB Command With Syntax
Multiple Assignments
You can have multiple assignments on the same
line.
For example,
a = 2; b = 7; c = a * b
MATLAB will execute the above statement and
return the following result −
c = 14
MATLAB Command With Syntax
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
% peacefully and unobtrusively
MATLAB Command With Syntax
Command Purpose
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.
quit Stops MATLAB.
who Lists current variables.
whos Lists current variables (long display).
Commands for Managing a Session
MATLAB provides various commands for managing a session.
The following table provides all such commands −
MATLAB Command With Syntax
MATLAB provides various useful commands for
working with the system, like saving the current work
in the workspace as a file and loading the file later.
It also provides various commands for other system-
related activities like, displaying date, listing files in
the directory, displaying current directory, etc.
MATLAB Command With Syntax
Command Purpose
cd Changes current directory.
date Displays current date.
delete Deletes a file.
diary Switches on/off diary file recording
Commands for Working with the System
The following table displays some commonly used system-related commands −
MATLAB Command With Syntax
Command Purpose
disp Displays contents of an array or string.
fscanf Read formatted data from a file.
format Controls screen-display format.
fprintf Performs formatted writes to screen or file.
input Displays prompts and waits for input.
; Suppresses screen printing.
Input and Output Commands
MATLAB provides the following
Input and output related commands −
MATLAB Command With Syntax
Assignments
1. Create a matrix A of zeros [2x4], and B of ones[2x3].
i. Add A by 2 to create matrix of twos.
ii. Can we add A+B? and A-B? justify your Answer.
2. Create matrix of 4 rows and 3 columns.
i. Add X by 4 to create new matrix called Y.
ii. Form C= X+Y
iii. Form D= X-Y
iv. Remove second row.
v. Add a column of 4’s to the end of matrix.
3. Using matrix operations to solve the following system of linear equations.
4x - 2y + 6z = 8
2x + 8y + 2z = 4
6x +10y +3z = 0.
MATLAB Command With Syntax
THANK YOU
Image Source
searchenterpriseai.techtarget.com
wikipedia

More Related Content

What's hot (20)

PPTX
Introduction to matlab lecture 3 of 4
Randa Elanwar
 
PDF
Basics of MATLAB programming
Ranjan Pal
 
PPT
Matlab1
guest8ba004
 
PPTX
Lectue five
Mahmoud Hussein
 
PPTX
Introduction to matlab lecture 2 of 4
Randa Elanwar
 
PPTX
Matlab Tutorial
Ahmad Siddiq
 
PPTX
16. Arrays Lists Stacks Queues
Intro C# Book
 
PDF
Matlab programming project
Assignmentpedia
 
PPT
Introduction to MatLab programming
Damian T. Gordon
 
PPT
Matlab introduction
Satish Gummadi
 
PPTX
Matlab Functions
Umer Azeem
 
PPTX
Introduction to matlab
Khulna University
 
PDF
Sample presentation slides template
Valerii Klymchuk
 
PDF
Matlab commands
avinashkumer
 
PPT
Matlab Overviiew
Nazim Naeem
 
PPTX
An Introduction to MATLAB for beginners
Murshida ck
 
PDF
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
PPT
Matlab intro
THEMASTERBLASTERSVID
 
PPTX
Computational Assignment Help
Programming Homework Help
 
PDF
C programming & data structure [arrays & pointers]
MomenMostafa
 
Introduction to matlab lecture 3 of 4
Randa Elanwar
 
Basics of MATLAB programming
Ranjan Pal
 
Matlab1
guest8ba004
 
Lectue five
Mahmoud Hussein
 
Introduction to matlab lecture 2 of 4
Randa Elanwar
 
Matlab Tutorial
Ahmad Siddiq
 
16. Arrays Lists Stacks Queues
Intro C# Book
 
Matlab programming project
Assignmentpedia
 
Introduction to MatLab programming
Damian T. Gordon
 
Matlab introduction
Satish Gummadi
 
Matlab Functions
Umer Azeem
 
Introduction to matlab
Khulna University
 
Sample presentation slides template
Valerii Klymchuk
 
Matlab commands
avinashkumer
 
Matlab Overviiew
Nazim Naeem
 
An Introduction to MATLAB for beginners
Murshida ck
 
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Matlab intro
THEMASTERBLASTERSVID
 
Computational Assignment Help
Programming Homework Help
 
C programming & data structure [arrays & pointers]
MomenMostafa
 

Similar to Programming with matlab session 1 (20)

PDF
Introduction to matlab
Sourabh Bhattacharya
 
PDF
Introduction to matlab
Sourabh Bhattacharya
 
DOCX
Introduction to matlab
Indrani Jangete
 
PPTX
1. Ch_1 SL_1_Intro to Matlab.pptx
MOHAMMAD SAYDUL ALAM
 
PDF
A complete introduction on matlab and matlab's projects
Mukesh Kumar
 
PPTX
From zero to MATLAB hero: Mastering the basics and beyond
MahuaPal6
 
PPTX
1. Introduction.pptx
SungaleliYuen
 
PDF
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
PDF
Matlab Introduction and Basics Guide.pdf
JesseGrant9
 
PPTX
intro2matlab-basic knowledge about Matlab.pptx
uf5221985
 
PPTX
Matlab introduction
Ameen San
 
PPT
MATLAB-Introd.ppt
kebeAman
 
PPT
MatlabIntro (1).ppt
AkashSingh728626
 
PPT
Introduction to Matlab - Basic Functions
joellivz
 
PPT
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
PPT
Matlab Introduction for beginners_i .ppt
DrSeemaBHegde1
 
PPT
Matlab_Introduction_by_Michael_Medvinsky.ppt
khaliq1
 
PPT
MatlabIntroduction presentation xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
divyapriya balasubramani
 
PPT
WIDI ediot autis dongok part 2.ediot lu lembot lu
IrlanMalik
 
PPT
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
Introduction to matlab
Sourabh Bhattacharya
 
Introduction to matlab
Sourabh Bhattacharya
 
Introduction to matlab
Indrani Jangete
 
1. Ch_1 SL_1_Intro to Matlab.pptx
MOHAMMAD SAYDUL ALAM
 
A complete introduction on matlab and matlab's projects
Mukesh Kumar
 
From zero to MATLAB hero: Mastering the basics and beyond
MahuaPal6
 
1. Introduction.pptx
SungaleliYuen
 
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
Matlab Introduction and Basics Guide.pdf
JesseGrant9
 
intro2matlab-basic knowledge about Matlab.pptx
uf5221985
 
Matlab introduction
Ameen San
 
MATLAB-Introd.ppt
kebeAman
 
MatlabIntro (1).ppt
AkashSingh728626
 
Introduction to Matlab - Basic Functions
joellivz
 
MatlabIntro1234.ppt.....................
RajeshMadarkar
 
Matlab Introduction for beginners_i .ppt
DrSeemaBHegde1
 
Matlab_Introduction_by_Michael_Medvinsky.ppt
khaliq1
 
MatlabIntroduction presentation xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
divyapriya balasubramani
 
WIDI ediot autis dongok part 2.ediot lu lembot lu
IrlanMalik
 
WIDI ediot autis dongok part 3.EDIOT LU LEMBOT LY
IrlanMalik
 
Ad

More from Infinity Tech Solutions (20)

PDF
Database management system session 6
Infinity Tech Solutions
 
PDF
Database management system session 5
Infinity Tech Solutions
 
PDF
Database Management System-session 3-4-5
Infinity Tech Solutions
 
PDF
Database Management System-session1-2
Infinity Tech Solutions
 
PDF
Main topic 3 problem solving and office automation
Infinity Tech Solutions
 
PDF
Introduction to c programming
Infinity Tech Solutions
 
PDF
E commerce
Infinity Tech Solutions
 
PDF
Bds session 13 14
Infinity Tech Solutions
 
PDF
Computer memory, Types of programming languages
Infinity Tech Solutions
 
PDF
Basic hardware familiarization
Infinity Tech Solutions
 
PDF
User defined functions in matlab
Infinity Tech Solutions
 
PDF
Programming with matlab session 3 notes
Infinity Tech Solutions
 
PPTX
AI/ML/DL/BCT A Revolution in Maritime Sector
Infinity Tech Solutions
 
PDF
Programming with matlab session 5 looping
Infinity Tech Solutions
 
PDF
BIG DATA Session 7 8
Infinity Tech Solutions
 
PDF
BIG DATA Session 6
Infinity Tech Solutions
 
PDF
DBMS CS 4-5
Infinity Tech Solutions
 
Database management system session 6
Infinity Tech Solutions
 
Database management system session 5
Infinity Tech Solutions
 
Database Management System-session 3-4-5
Infinity Tech Solutions
 
Database Management System-session1-2
Infinity Tech Solutions
 
Main topic 3 problem solving and office automation
Infinity Tech Solutions
 
Introduction to c programming
Infinity Tech Solutions
 
Bds session 13 14
Infinity Tech Solutions
 
Computer memory, Types of programming languages
Infinity Tech Solutions
 
Basic hardware familiarization
Infinity Tech Solutions
 
User defined functions in matlab
Infinity Tech Solutions
 
Programming with matlab session 3 notes
Infinity Tech Solutions
 
AI/ML/DL/BCT A Revolution in Maritime Sector
Infinity Tech Solutions
 
Programming with matlab session 5 looping
Infinity Tech Solutions
 
BIG DATA Session 7 8
Infinity Tech Solutions
 
BIG DATA Session 6
Infinity Tech Solutions
 
Ad

Recently uploaded (20)

PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PDF
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
PPTX
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
PDF
01-introduction to the ProcessDesign.pdf
StiveBrack
 
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
PPTX
WHO And BIS std- for water quality .pptx
dhanashree78
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
PPTX
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
PPTX
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
01-introduction to the ProcessDesign.pdf
StiveBrack
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
WHO And BIS std- for water quality .pptx
dhanashree78
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
Computer network Computer network Computer network Computer network
Shrikant317689
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 

Programming with matlab session 1

  • 2. Session objective COMPUTERS AND PROGRAMMING PROGRAMMING LANGUAGES TYPES MATLAB INTRODUCTION IDE ,COMMANDS ,RESULTS ,M FILE COMPARE A SAMPLE CODE IN C WITH MATLAB SUMMARY
  • 3. Computers and Programming 1. Computer: An electronic device which is capable of performing complex arithmetical and logical operations to get the desired result 2. Program: Set of instructions given to the computer to execute a particular task.
  • 4. Programming Languages Types 1. Machine level language 2. Assembly level language (Assembler) 3. High level language (Compiler interpreter)
  • 5. MATLAB Introduction 1. What is MATLAB ??? 2. Why MATALB??? a. Interfacing API for TMS320(DSP),ARM,89s51,89s52,….. b. Port interfacing c. Reduces time required for PDLC d. Scientific calculations
  • 6. MATLAB Applications 1. Communication engineering 2. AI,ML,DL 3. Image processing 4. Optimization 5. Data access ,analysis and visualization 6. Programming and algorithm designing 7. Performance analysis of algorithms 8. Simulink
  • 7. MATLAB Commands 1. date 2. clock 3. ver 4. whos 5. what(files in current dir) 6. clc 7. clear 8. diary filename 9. diary off 10. diary on 11. path 12. editpath GUI ,home,set path 13.! DOS command 14. exit
  • 8. MATLAB Operators/Symbols/Functions 1. + 2. - 3. * 4. / 5. ^ 6. = 7. == 8. pi 9. vpa (digit precision) 10. % 11. ; 12. : range 13.randn() %10, 1:1:10; 14. Ctrl+c 15. ceil 16. floor 17. sqrt 18. Matrix (+,-,*) 19. ‘.’ operator 20. eye 21. zeros(5) 22. ` transpose 23. ones(6)
  • 9. MATLAB Operators/Symbols/Functions 24. imread() 25. imshow 26. disp() 27. input() 28. factorial() 29. length(a) 30. size(a) 31. fprintf()-only o/p 32. sprintf()-o/p with save in workspace 33. format rat/ long /hex /shortEng/logEng 34. de2bi, d=(1:10)’; b=de2bi(d); [d,b] 35. decimalToBinaryVector(x);MSB,LSB
  • 10. MATLAB Operators/Symbols/Functions 36. All trigonometric values and error o/p 37. cosd(),sind() 38. All hyperbolic trigonometric values 39. mod(x,y) 40. log(x) %natural log 41. log10(x)
  • 11. MATLAB Variables and Data Types Data types Logical Char Numeric Int 8 unint 8 Int 16 unint 16 Int 32 unint 32 Int 64 unint 64 Single Double Structure Function Handle
  • 12. Typing Mathematical Expression 1) R1= −𝑏+ 𝑏2−4𝑎𝑐 2𝑎 R2= −𝑏± 𝑏 2 −4𝑎𝑐 2𝑎 2) R1= 𝑎2 + 𝑏2 e-0.2t cos 2t 3) E=mc2 Y=1 + 𝑛𝑥 1! + 𝑛 𝑛−1 𝑥2 2!
  • 13. MATLAB Code File 1. File name should not start with number 2. No Space in the file name 3. No library function name 4. No use of keywords reserved by MATALAB MATLAB Searching Mechanism 1. Workspace 2. Current directory 3. Set path directory
  • 14. Compare code in C with MATLAB 1. WAP program to perform addition of two numbers a and b with following constraint:  Do not use + operator i.e. c=a+b  Do not use –(-) operator i.e. c=a-(-b)  Do not use x(-1) i.e. c=a-b(-1)  Do not use c=b-(-a)  Do not c=b-(-a)  Do not use c=a- 𝟏 𝟏 −𝒃  Do not use c=b- 𝟏 𝟏 −𝒂  Do not use c= 𝒂 𝟐−𝒃 𝟐 𝒂−𝒃 Objectives: 1. Find the solution and logic 2. Compare C and Matlab code
  • 15. MATLAB Programming Exercise 1. WAP to find the roots of the quadratic equations 2. Ideal gas law problem to find volume 3. Let us see direct implementation using MATLAB………..
  • 18. %Matrix computations and operations >> %creating vectors and matrices >> %create a row vector, we can form a row vector. >> a=[1 3 6 8]; >> a a = 1 3 6 8 MATLAB Command With Syntax
  • 19. >> %create a column vector, >> b=[1;4;6;10]; >> b b = 1 4 6 10 MATLAB Command With Syntax
  • 20. >>Create the matrix x of 3 rows and 4 columns x=[12 23 21 3; 2 34 5 7; 31 32 33 34]; >> x x = 12 23 21 3 2 34 5 7 31 32 33 34 MATLAB Command With Syntax
  • 21. %elements of a Matrix >> z=x(2,3) z = 5 >> x1=x(1,:) x1 = 12 23 21 3 >> x2=x(:,2) x2 = 23 34 32 >> s=size(x) s = 3 4 MATLAB Command With Syntax
  • 22. Remove the n th X(n, : ) = [ ] x(2, : ) = [ ] Add a column of 4,s to the end of matrix. x( ; , 4) = 4 MATLAB Command With Syntax
  • 23. We can extend the colon notation to specify a sequence Create a vector v which starts at 1, with increment of 2 and stops at 10: v=1:2:10 v = 1 3 5 7 9 >> v=1:10 v = 1 2 3 4 5 6 7 8 9 10 MATLAB Command With Syntax
  • 24. x=[11 12 13 14; 21 22 23 24;31 32 33 34]; >> x x = 11 12 13 14 21 22 23 24 31 32 33 34 We can use this vector notation when referring to sub matrix: w=x(1:2:3, 2:4) MATLAB Command With Syntax
  • 25. w = 12 13 14 32 33 34 Rows :start at 1, increment by 2, stop at 3 Columns 2,3,4start at 2, stop at 4 default increment of 1 is used MATLAB Command With Syntax
  • 26. x=[11 12 13 14;21 22 23 24 ;31 32 33 34 ] x = 11 12 13 14 21 22 23 24 31 32 33 34 >> x4=x(1:2:3,2:4) x4 = 12 13 14 32 33 34 MATLAB Command With Syntax
  • 27. x4=x(1:2:2,2:4) x4 = 12 13 14 >> x4=x(1:1:3,2:4) x4 = 12 13 14 22 23 24 32 33 34 MATLAB Command With Syntax
  • 28. x4=x(1:1:2,2:4) x4 = 12 13 14 22 23 24 x4=[x;41 42 43 44] x4 = 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 x= [41 42 43 44;x] ? MATLAB Command With Syntax
  • 29. x4 = x(2:1:3,2:4) x4 = 22 23 24 32 33 34 x(4 , : ) = [41 42 43 44] x = 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 MATLAB Command With Syntax
  • 30. x4=x(1:1:2,1:4) x4 = 11 12 13 14 21 22 23 24 >> x4=x(1:1:2,2:4) x4 = 12 13 14 22 23 24 >> x4=x(1:1:2,3:4) x4 = 13 14 23 24 MATLAB Command With Syntax
  • 31. Strings String handling Syntax S = 'Any Characters' S = [S1 S2 ...] S = strcat(S1, S2, ...) MATLAB Command With Syntax
  • 32. Examples Create a simple string that includes a single quote. msg = 'You''re right!' msg = You're right! Create the string name using two methods of concatenation. name = ['Thomas' ' R. ' 'Lee'] name = strcat('Thomas',' R.',' Lee') MATLAB Command With Syntax
  • 33. Create a vertical array of strings. C = strvcat('Hello','Yes','No','Goodbye‘) C = Hello Yes No Goodbye MATLAB Command With Syntax
  • 34. S = {'Hello' 'Yes' 'No' 'Goodbye'} S = 'Hello' 'Yes' 'No' 'Goodbye' Create a cell array of strings. S = {'Hello' 'Yes' 'No' 'Goodbye} MATLAB Command With Syntax
  • 35. 1. Commands for Managing a Session 2. MATLAB provides various commands for managing a session. 3. The following table provides all such commands − 4. I have forgotten the Variables! 5. The who command displays all the variable names you have used. 6. who 7. MATLAB will execute the above statement and return the following result MATLAB Command With Syntax
  • 36. >> who Your variables are: a b x >> whos Name Size Bytes Class Attributes a 1x4 32 double b 4x1 32 double x 3x4 96 double MATLAB Command With Syntax
  • 37. whos Name Size Bytes Class Attributes a 1x4 32 double b 4x1 32 double x 3x4 96 double MATLAB Command With Syntax
  • 38. Basic Task: Plot the function sin(x) between 0≤x≤4π  Create an x-array of 100 samples between 0 and 4π.  Calculate sin(.) of the x-array  Plot the y-array >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y) 0 10 20 30 40 50 60 70 80 90 100 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 MATLAB Command With Syntax
  • 39. Plot the function e-x/3sin(x) between 0≤x≤4π  Create an x-array of 100 samples between 0 and 4π.  Calculate sin(.) of the x-array  Calculate e-x/3 of the x-array  Multiply the arrays y and y1 >>x=linspace(0,4*pi,100); >>y=sin(x); >>y1=exp(-x/3); >>y2=y.*y1; MATLAB Command With Syntax
  • 40. Plot the function e-x/3sin(x) between 0≤x≤4π  Multiply the arrays y and y1 correctly  Plot the y2-array >>y2=y.*y1; >>plot(y2) 0 10 20 30 40 50 60 70 80 90 100 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 MATLAB Command With Syntax
  • 41. Display Facilities  plot(.)  stem(.) Example: >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y) >>plot(x,y) Example: >>stem(y) >>stem(x,y) 0 10 20 30 40 50 60 70 80 90 100 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0 10 20 30 40 50 60 70 80 90 100 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 MATLAB Command With Syntax
  • 42. Display Facilities  title(.)  xlabel(.)  ylabel(.) >>title(‘This is the sinus function’) >>xlabel(‘x (secs)’) >>ylabel(‘sin(x)’) 0 10 20 30 40 50 60 70 80 90 100 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 This is the sinus function x (secs) sin(x) MATLAB Command With Syntax
  • 43. Operators (relational, logical)  == Equal to  ~= Not equal to  < Strictly smaller  > Strictly greater  <= Smaller than or equal to  >= Greater than equal to  & And operator  | Or operator  ~ NOT MATLAB Command With Syntax
  • 44. 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 π Special Variables and Constants MATLAB supports the following special variables and constants: MATLAB Command With Syntax
  • 45. Multiple Assignments You can have multiple assignments on the same line. For example, a = 2; b = 7; c = a * b MATLAB will execute the above statement and return the following result − c = 14 MATLAB Command With Syntax
  • 46. 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 % peacefully and unobtrusively MATLAB Command With Syntax
  • 47. Command Purpose 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. quit Stops MATLAB. who Lists current variables. whos Lists current variables (long display). Commands for Managing a Session MATLAB provides various commands for managing a session. The following table provides all such commands − MATLAB Command With Syntax
  • 48. MATLAB provides various useful commands for working with the system, like saving the current work in the workspace as a file and loading the file later. It also provides various commands for other system- related activities like, displaying date, listing files in the directory, displaying current directory, etc. MATLAB Command With Syntax
  • 49. Command Purpose cd Changes current directory. date Displays current date. delete Deletes a file. diary Switches on/off diary file recording Commands for Working with the System The following table displays some commonly used system-related commands − MATLAB Command With Syntax
  • 50. Command Purpose disp Displays contents of an array or string. fscanf Read formatted data from a file. format Controls screen-display format. fprintf Performs formatted writes to screen or file. input Displays prompts and waits for input. ; Suppresses screen printing. Input and Output Commands MATLAB provides the following Input and output related commands − MATLAB Command With Syntax
  • 51. Assignments 1. Create a matrix A of zeros [2x4], and B of ones[2x3]. i. Add A by 2 to create matrix of twos. ii. Can we add A+B? and A-B? justify your Answer. 2. Create matrix of 4 rows and 3 columns. i. Add X by 4 to create new matrix called Y. ii. Form C= X+Y iii. Form D= X-Y iv. Remove second row. v. Add a column of 4’s to the end of matrix. 3. Using matrix operations to solve the following system of linear equations. 4x - 2y + 6z = 8 2x + 8y + 2z = 4 6x +10y +3z = 0. MATLAB Command With Syntax