Computers and Programming , Programming Languages Types, Problem solving, Introduction to the MATLAB environment, Using MATLAB Documentation
Introduction to the course, Operating methodology-Installation Procedure
1. Compare a sample code in C with MATLAB
2. Trajectory of a particle in projectile motion ( solving quadratic equations)
3. Ideal gas law problem to find volume
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
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
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
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
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