Professional Documents
Culture Documents
(Autonomous)
PERUNDURAI, ERODE – 638 060
DEPARTMENT OF MATHEMATICS
REGULATIONS 2022
Prepared by
Dr. K. Arun Prakash
Department of Mathematics
Dr. S. Indra Kumar
Department of Computer Technology - UG
Kongu Engineering College
Perundurai, Erode – 638 060
KONGU ENGINEERING COLLEGE
(Autonomous)
PERUNDURAI, ERODE – 638 060
DEPARTMENT OF MATHEMATICS
REGULATIONS 2022
22MAC11 - MATRICES AND ORDINARY DIFFERENTIAL EQUATIONS
Name :________________________________________________________
Programme : ________________________________________________________
Branch : ________________________________________________________
Section : ________________________________________________________
Semester : I SEMESTER
2 Computation of Eigenvalues
and Eigen vectors
5 Solution of Simultaneous
First Order ODEs
1 Introduction to MATLAB 1
(i) Basic Windows in MATLAB 2
(ii) General Commands 4
(iii) Tool Bars in MATLAB 5
(iv) Basic Arithmetic Operations 5
(v) Built-in Functions 6
(vi) Creating, Saving and Executing a M-File 9
(vii) Output Commands 10
(viii) Conditional and Looping Structures 12
Aim: To have knowledge to basic windows in MATLAB built in function and able to create
vector matrices.
MATLAB is one of the high-performance languages that is generally used for the
purpose of technical computing. By integrating the visualization, and programming, MATLAB
creates an easy-to-use environment where understandable mathematical notations are used to
express the problems and solutions. This language is generally used in the development of
algorithms, and computation and mathematics.
MATLAB application is built on the basis of MATLAB scripting language. These
common uses of this MATLAB application include the deployment of Command Window in
the form of an interactive mathematical shell or the execution of files that consist of MATLAB
code.
History of MATLAB
The advancement of MATLAB started toward the finish of 1970s. The work on
MATLAB was started by Cleve Moler, who led the software engineering division at the
University of New Mexico amid that time. Cleve Moler began creating MATLAB with the
point of giving his understudies an entrance to EISPACK and LINPACK without having any
need to go for learning FORTRAN.
The dialect, not withstanding, before long ended up prominent among countless,
particularly in the field of connected arithmetic, in view of its high ease of use in the said
territory. At the season of the visit that was made by Moler to Stanford University in the 1983,
Jack Little, who was a designer, interacted with the MATLAB programming dialect.
Subsequent to understanding the way that MATLAB had an amazing business
potential, Jack Little continued for holding hands with Steve Bangert and Moler. These three
at that point began revamping MATLAB in C Language. This was likewise gone with the
arrangement of Math Works in the year 1984 to additionally upgrade the advancement of
MATLAB.
JACKPAC is the name that was given to the modified libraries of MATLAB. LAPACK
was made in the year 2000. This was another arrangement of libraries which were utilized by
the more up to date and altered MATLAB to fuse the element of grid control.
The early employments of MATLAB were particularly made by the experts and in
addition scientists that were related with the field of control designing, which was a claim to
fame of Jack Little. The dialect, on account of the prominent highlights, rushed to wind up
mainstream in an assortment of different areas also.
The common uses of this language are now made in the field of education, especially
in the teaching of numerical analysis, and linear algebra. Apart from that it is also deployed by
the scientists who work in the area of image processing.
1
Starting MATLAB:
On a Microsoft Windows platform, to start MATLAB, double-click the MATLAB
shortcut icon on your Windows desktop.
(i) Basic Windows in MATLAB:
The default layout:
The default layout consists of the following four windows that are displayed under the
Toolstrip: the Command Window (the larger window), the Current Folder Window (on the
top left), the Details Window and the Workspace Window (on the bottom lest). A list of
several MATLAB windows and their purposes is given in following Table.
Figure: 1 Default Layout
2
Command Window:
The Command Window is MATLAB’s main window and opens when MATLAB is
started. It is convenient to have the Command Window as the only visible window. This can
be done either by closing all the other windows, or by selecting Command Window Only in
the menu that opens when the Layout icon on the Toolstrip is selected. To close a window,
click on the pull-down menu at the top right-hand side of the window and then select Close.
Figure: 2
Command Window
Figure Window:
The Figure Window opens automatically when graphics commands are executed, and
contains graphs created by these commands. An example of a Figure Window is shown in
Figure 3. A more detailed description of this window is given in Chapter 5.
Figure: 3
3
Editor Window:
The Editor Window is used for writing and editing programs. This window is opened
by clicking on the New Script icon in the Toolstrip, or by clicking on the New icon and then
selecting Script from the menu that opens.
Figure: 4
Workspace information
✓ who lists variables currently in the workspace
✓ whos lists variables currently in the workspace with their size
✓ what lists M-files, Mat-files, and Mex- files on the disk
✓ clear clears the workspace, all variables are removed
✓ clear x y z clears only variables 𝑥, 𝑦 and 𝑧
✓ clear all clears all variables and functions from workspace
✓ mlock fun lock’s function fun so that clear cannot remove it
✓ munlock fun unlocks function fun so that clear can remove it
✓ clc clears command window, cursor moves to the top
✓ home scrolls the command window to put the curser on top
✓ clf clears figure window
4
Directory information
➢ 𝑝𝑤𝑑 shows the current working directory
➢ 𝑐𝑑 changes the current working directory
➢ 𝑑𝑖𝑟 lists contents of the current directory
➢ 𝑙𝑠 lists contents of the current directory, same as dir
➢ 𝑝𝑎𝑡ℎ gets or sets MATLAB search path
➢ 𝑒𝑑𝑖𝑡𝑝𝑎𝑡ℎ modifies MATLAB search path
➢ 𝑐𝑜𝑝𝑦𝑓𝑖𝑙𝑒 copies a file
➢ 𝑚𝑘𝑑𝑖𝑟 creates a directory
General information
➢ 𝑐𝑜𝑚𝑝𝑢𝑡𝑒𝑟 tells you the computer type you are using
➢ 𝑐𝑙𝑜𝑐𝑘 gives you wall clock time and date as a vector
➢ 𝑑𝑎𝑡𝑒 tells you the date as a string
➢ 𝑚𝑜𝑟𝑒 controls the paged output according to the screen size
➢ 𝑣𝑒𝑟 gives the license and the MATLAB version information
➢ 𝑏𝑒𝑛𝑐ℎ benchmarks your computer on running MATLAB compared to other
computers
Termination
➢ ^𝑐 (Control-c) local abort, kills the current command execution
➢ 𝑞𝑢𝑖𝑡 quits MATLAB
➢ 𝑒𝑥𝑖𝑡 same as quit.
(iii) Toolbars
Figure: 5
(iv) Basic Arithmetic Operations
The symbols of arithmetic operations are:
Operation Symbol Examples Operation Symbol Examples
Addition + 2 + 8 = 10 Right division / 72/8 = 9
Subtraction - 3 − 45 = −42 Left division \ 5\6 = 6/5
Multiplication * 5 ∗ 9 = 45 Exponentiation ^ 5^2 = 25
5
DISPLAY FORMATS:
Though computations inside MATLAB are performed using double precision, the
appearance of floating-point numbers on the screen is controlled by the output format in use.
There are several different screen output formats. The following table shows the printed value
of 10𝜋 in seven different formats.
➢ format short 31 ∙ 4159
➢ format short e 3 ∙ 1416𝑒 + 00 1
➢ format long 31 ∙ 41592653589793
➢ format long e 3 ∙ 141592653589793𝑒 + 00 1
➢ format short g 31 ∙ 416
➢ format long g 31 ∙ 41592653589797
➢ format hex 403𝑓6𝑎7𝑎2955385𝑒8
➢ format rat 3550/1139
➢ format bank 31 ∙ 42
ans =
5
2 𝑡ℎ
𝑛𝑡ℎ𝑟𝑜𝑜𝑡(𝑥, 𝑛) Real 𝑛 root of a real number 𝑥. >> 𝑛𝑡ℎ𝑟𝑜𝑜𝑡(125,3)
(If 𝑥 is negative 𝑛 must be an odd
integer.) ans =
5
3 𝑒𝑥𝑝(𝑥) Exponential of 𝑥 >> 𝑒𝑥𝑝(3)
ans =
20.0855
4 𝑎𝑏𝑠(𝑥) Absolute value. >> 𝑎𝑏𝑠(−3.5)
ans =
3.5000
5 𝑙𝑜𝑔(𝑥) Natural logarithm. >> 𝑙𝑜𝑔(5)
Base e logarithm (𝑙𝑛).
ans =
1.6094
6 𝑙𝑜𝑔10(𝑥) Base 10 logarithm. >> 𝑙𝑜𝑔10(5)
ans =
0.6990
6
7 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(𝑥) The factorial function 𝑥! >> 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(5)
(𝑥 must be a positive integer.)
ans =
120
Trigonometric functions
ans =
0.8940
2 𝑠𝑖𝑛𝑑(𝑥) Sine of angle x (x in degrees). >> 𝑠𝑖𝑛𝑑(90)
ans =
1
3 𝑐𝑜𝑠(𝑥) Cosine of angle x (x in radians). >> 𝑐𝑜𝑠(45)
ans =
0.5253
4 𝑐𝑜𝑠𝑑(𝑥) Cosine of angle x (x in degrees). >> 𝑐𝑜𝑠𝑑(45)
ans =
0.7071
5 𝑡𝑎𝑛(𝑥) Tangent of angle x (x in radians). >> 𝑡𝑎𝑛(45)
ans =
1.6198
6 𝑡𝑎𝑛𝑑(𝑥) Tangent of angle x (x in degrees). >> 𝑡𝑎𝑛𝑑(45)
ans =
1
7 𝑐𝑜𝑡(𝑥) Cotangent of angle x (x in radians). >> 𝑐𝑜𝑡(45)
ans =
0.6174
8 𝑐𝑜𝑡𝑑(𝑥) Cotangent of angle x (x in degrees). >> 𝑐𝑜𝑡𝑑(45)
ans =
1
Inverse Trigonometric functions
7
3 𝑎𝑐𝑜𝑠(𝑥) Inverse Cosine of angle 𝑥 >> 𝑎𝑐𝑜𝑠(45)
(𝑥 in radians). ans =
0.0000 + 4.4997i
4 𝑎𝑐𝑜𝑠𝑑(𝑥) Inverse Cosine of angle 𝑥 >> 𝑎𝑐𝑜𝑠𝑑(45)
(𝑥 in degrees). ans =
0.0000e+00 + 2.5781e+02i
5 𝑎𝑡𝑎𝑛(𝑥) Inverse Tangent of angle 𝑥 >> 𝑎𝑡𝑎𝑛(45)
(𝑥 in radians). ans =
1.5486
6 𝑎𝑡𝑎𝑛𝑑(𝑥) Inverse Tangent of angle 𝑥 >> 𝑎𝑡𝑎𝑛𝑑(45)
(𝑥 in degrees). ans =
88.7270
7 𝑎𝑐𝑜𝑡(𝑥) Inverse Cotangent of angle >> 𝑎𝑐𝑜𝑡(45)
𝑥 (𝑥 in radians). ans =
0.0222
8 𝑎𝑐𝑜𝑡𝑑(𝑥) Inverse Cotangent of angle >> 𝑎𝑐𝑜𝑡𝑑(45)
𝑥 (𝑥 in degrees). ans =
1.2730
Rounding functions
S. No Function Description Example
1 𝑟𝑜𝑢𝑛𝑑(𝑥) Round to the nearest integer. >> 𝑟𝑜𝑢𝑛𝑑(1.4)
ans =
1
>> 𝑟𝑜𝑢𝑛𝑑(1.6)
ans =
2
2 𝑓𝑖𝑥(𝑥) Round toward zero. >> 𝑓𝑖𝑥(3.2)
ans =
3
>> 𝑓𝑖𝑥(3.7)
ans =
3
3 𝑐𝑒𝑖𝑙(𝑥) Round toward infinity. >> 𝑐𝑒𝑖𝑙(15.3)
ans =
16
>> 𝑐𝑒𝑖𝑙(15.9)
ans =
16
4 𝑓𝑙𝑜𝑜𝑟(𝑥) Round toward minus infinity. >> 𝑓𝑙𝑜𝑜𝑟(4.3)
ans =
4
8
>> 𝑓𝑙𝑜𝑜𝑟(−4.3)
ans =
-5
5 𝑟𝑒𝑚(𝑥, 𝑦) Returns the remainder after x >> 𝑟𝑒𝑚(15,5)
is divided by y. ans =
0
>> 𝑟𝑒𝑚(15,4)
ans =
3
Open the MATLAB editor by clicking the button. This will open a new M-file
in the editor window as shown in the figure.
Figure: 6
9
Type in the commands (or) entire program you want to execute
Save the file in a location accessible to MATLAB (usually the MATLAB work
directory or current working directory)
MATLAB file has an extension . 𝒎 (For example 𝒆𝒙𝒂𝒎𝒑𝒍𝒆. 𝒎)
Executing an 𝑴 − 𝑭𝒊𝒍𝒆:
There are three ways of executing an M-file.
❖ Type the name of the M-file in the Command Window and press Enter or Return. If
there is no error, MATLAB will display the required result.
❖ Press F5, it will prompt us to save the M-file in a specified directory. After the file has
been saved, it is executed for errors and the result will be displayed.
❖ Click save button to save the M-file. Then press Run button to execute the
file.
10
Output:
A=
1 2 3
4 5 6
7 8 9
2. Input: >> 𝑑𝑖𝑠𝑝(′𝐸𝑛𝑡𝑒𝑟 𝑡ℎ𝑒 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑛′)
Examples:
1. Input:
syms x y
x = 23;
y = sqrt(x);
fprintf (‘The value of 𝑦 is %f ’, y);
Output:
The value of 𝑦 is 4.795832
2. Input:
syms x y z
x = 2;
11
y = x^2;
z = exp(y)+ 2*y;
fprintf (‘The value of z is %f ’, z);
Output:
The value of 𝑧 is 62.598150
Looping Structure:
A loop is another method to alter the flow of a computer program. In a loop, the
execution of a command, or a group of commands, is repeated several times consecutively.
Each round of execution is called a pass. In each pass at least one variable, but usually more
than one, or even all the variables that are defined within the loop, are assigned new values.
MATLAB has two kinds of loops. A loop can be terminated at any time with the break
command.
𝒇𝒐𝒓 loop
A for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.
Syntax:
The syntax of a for loop in MATLAB is as follows:
for index = initial: interval: final
statements
...
end
Here index - loop variable
initial - loop start value
interval - increment value
final - loop end value
The following are some other forms of for loop index conditions.
1. initval:endval --- increments the index variable from 𝑖𝑛𝑖𝑡𝑣𝑎𝑙 to 𝑒𝑛𝑑𝑣𝑎𝑙 by 1, and
repeats execution of program statements until index is greater than 𝑒𝑛𝑑𝑣𝑎𝑙.
2. initval:step:endval --- increments index by the value step on each iteration, or
decrements when step is negative.
3. valArray --- creates a column vector index from subsequent columns of array
𝑣𝑎𝑙𝐴𝑟𝑟𝑎𝑦 on each iteration. For example, on the first iteration, 𝑖𝑛𝑑𝑒𝑥 =
𝑣𝑎𝑙𝐴𝑟𝑟𝑎𝑦(: ,1). The loop executes for a maximum of n times, where n is the number
12
of columns of valArray, given by 𝑛𝑢𝑚𝑒𝑙(𝑣𝑎𝑙𝐴𝑟𝑟𝑎𝑦, 1, ∶). The input val Array can be
of any MATLAB data type, including a string, cell array, or struct.
Example:
1. Input:
sum = 0;
for i=1:1:10
sum = sum+i;
end
fprintf("sum= %d",sum)
Output: Sum = 55
2. Input:
for i = [1,2,3,4]
disp(i^2)
end
Output:
1
4
9
16
Conditional Statements
A conditional statement is a command that allows MATLAB to make a decision
whether to execute a group of statements that follows the conditional statement, when the
condition is true of to skip these statements if the condition is false. There are many
conditional statements available in MATLAB.
if else loop
The if else is most important conditional statement widely used in MATLAB.
An if statement can be followed by one (or more) optional else if... and an else statement,
which is very useful to test various conditions.
Syntax
if conditional expression
..............................
............................... Group of statements - 1
..............................
else
..............................
............................... Group of statements - 2
..............................
end
13
The conditional expression is a condition that is to be tested for executing a group of
statements. When the expression is true the group of statements - 1 will be executed, otherwise
group of statements - 2 that follows the else will be executed.
Example:
Input:syms x y
x=7;
y=rem(x,2);
if(y==0)
fprintf('The given number is a Even number')
else
fprintf('The given number is a Odd number')
end
Output:
>> The given number is a Odd number
14
Example:
Input: marks=75;
if(marks>=80)
fprintf('The student has got A grade')
elseif((marks<80) &&(marks>=70))
fprintf('The student has got B grade')
else
fprintf('The student has got C grade')
end
Vectors: A vector is a one-dimensional array of numbers. MATLAB allows creating two types
of vectors.
• Row vectors
• Column vectors
Row Vectors: Row vectors are created by enclosing the set of elements in square brackets,
using space or comma to delimit the elements.
Example: r = [7 8 9 10 11]
MATLAB will execute the above statement and return the following result.
r=
7 8 9 10 11
Column Vectors: Column vectors are created by enclosing the set of elements in square
brackets, using semicolon to delimit the elements.
Output: ans = 3
Referencing the Elements of a Matrix: To reference an element in the mth row and
nth column, of a matrix 𝑨, we write 𝑨(𝒎, 𝒏);
Example: The element in the 2nd row and 5th column, of the matrix 𝐴, as created in the
previous example is obtained by
Input: a(2,5)
Output: ans = 6
To reference all the elements in the mth column we type 𝐴(: , 𝑚). Let us create a column
vector 𝑣, from the elements of the 4th row of the matrix 𝐴.
a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)
Output:
v=
4
5
6
7
16
Exercise – 1
1. Create a row vector x that contains the elements 2, 4,6,8,10,12. Convert it a column vector
named y.
2. Create a vector x1 that contains the first ten multiples of 8. Display it as a column vector y1.
3. Define the variables 𝑥 = 5 𝑎𝑛𝑑 𝑦 = 3 and then use them to create a row and column vector
(assign the variable named MATLAB01) that has the following elements: 𝑥/𝑦, 𝑥 + 𝑦, 𝑥 −
𝑦, 𝑥 × 𝑦, 𝑥 𝑦 , 𝑦 𝑥 , 𝑥 𝑥 , 𝑦 𝑦 .
4. Create a variable B that is a column vector with the following elements:
π
3
cos (3)
√(2.3)3 , 2.1 × 10−02 , (3 + 5.12 )cos60°, 35.70, √180, .
𝑡𝑎𝑛60°
5. Type this matrix in MATLAB and use MATLAB to carry out the following instructions.
3 7 −4 12
𝐴=[ −5 9 10 2 ].
6 13 8 11
15 5 4 1
(i) Create a vector v consisting of the elements in the second column of A.
(ii) Create a vector w consisting of the elements in the third row of A.
(iii) Create a 4 × 3 matrix B consisting of all elements in the second through fourth
columns of A.
(iv) Find the product of two matrices A and B.
7. Create a 6×6 matrix (assigned a variable M66) with first two rows 0’s and last four rows 1’s.
8. Create a matrix of your own of any one order 4×4, 5×5, 6×6, 7×7, 8×8, 10×10. Use the matrix
built-in functions to determine the upper triangular, lower triangular and diagonal matrices
and display the output. Also create the identity matrix, matrix of zeros and matrix of ones for
the same order.
17
3 4 6
(i) 𝑒 (−2.1) + 3.47𝑙𝑜𝑔(14) + √287 (ii) (2.4)7 𝑙𝑛(4) + √287
⌊−2.56⌋𝑒 −3.6
(iii) (iv) 6𝑙𝑜𝑔(8)
4!
11. Use MATLAB library functions to compute the output by rounding to the nearest integer,
rounding toward zero, rounding toward infinity for the values 3.3, 6.3, 99.99, 999.89, -3.3, -
6.3, -99.99,-999.89.
13. The distance between two points (𝑥1 , 𝑦1 ) and (𝑥2 , 𝑦2 ) on a Cartesian coordinate plane is
given by the equation
𝑑 = √(𝑥1 − 𝑥2 )2 + (𝑦1 − 𝑦2 )2
Write a program to calculate the distance between any two points (𝑥1 , 𝑦1 ) and (𝑥2 , 𝑦2 )
specified by the user. Use good programming practices in your program. Use the program to
calculate the distance between the points (3, 2) and (3, 6).
14. The steady-state current I flowing in a circuit that contains a resistance R = 5, capacitance
𝐸
C = 10, and inductance L = 4 in series is given by 𝐼 = 2
, where 𝐸 = 2,
√𝑅 2 +(2𝜋𝜔𝐿− 1 )
2𝜋𝜔𝐶
𝜔 = 2 are the input voltage and angular frequency respectively. Write a MATLAB script to
compute the value of 𝐼 and save it as current.m.
18
15. If C and F are Celsius and Fahrenheit temperatures, respectively, the formula for conversion
from Celsius to Fahrenheit is 𝐹 = 9𝐶/5 + 32. Write a script that will ask you for the Celsius
temperature and display the Fahrenheit equivalent with some sort of comment, such as
The Fahrenheit temperature is: ……….
Try it out on the following Celsius temperatures 0,100, −40, 37.
16. A mortgage bond (loan) of amount L is obtained to buy a house. The interest rate r is 15%.
The fixed monthly payment P that will pay off the bond loan over N years is given by the
formula
𝑟 12𝑁
𝑟𝐿 (1 + 12)
𝑃=
𝑟 12𝑁
12 [(1 + ) − 1]
12
Write a MATLAB program to compute and print P if N = 20 and the bond is for $50,000.
You should get $658.39.
19
20
Ex. No.: 1 Date:
Aim:
21
22
Problem No: 5 (or) 6
Input & Coding:
Problem No: 7
Input & Coding:
23
24
Problem No: 8
Input & Coding:
Problem No: 9
Input & Coding:
25
26
Problem No: 10
Input & Coding:
Problem No: 11
Input & Coding:
27
28
Problem No: 12/13/14/15/16
Input & Coding:
Aim 2
Viva 5
Total
Signature
29
30
2. COMPUTATION OF EIGENVALUES AND EIGEN VECTORS
Aim: To compute the eigenvalues and eigenvectors of a square matrix with real entries.
(1) Eigenvalues of a matrix: The function “eig” returns a column vector containing the
eigenvalues of square matrix A.
Syntax: = eig(A), where A is any square matrix.
2 −1 1
Example: Let us compute the Eigen values of the matrix A = −1 2 −1 .
1 −1 2
Input: Input:
>> A=[2 -1 1; -1 2 -1; 1 -1 2] >> eig(A)
Output: Output:
A= ans =
2 -1 1 1.0000
-1 2 -1 1.0000
1 -1 2 4.0000
Input: Output:
>> [V D]=eig(A)
Output: D=
V= 1.0000 0 0
0.4082 0.7071 -0.5774
0 1.0000 0
-0.4082 0.7071 0.5774
0 0 4.0000
-0.8165 0 -0.5774
31
Exercise - 2
6 −3 3
1. Identify the eigenvalues and Eigen vectors of the matrix −3 6 −3 by means of
3 −3 6
MATLAB.
1 −3 7
2. Find the eigenvalues and eigen vectors of the matrix 5 −7 0 .
2 8 −9
1 3 −7
3. Write MATLAB code to find the Eigenvalues and Eigenvectors of the matrix 5 7 9 .
1 3 0
6 3 −8 −4
0 10 6 7
4. Using MATLAB, determine the eigenvalues of A = .
0 0 6 −3
0 0 0 6
5. Create a 3 × 3 matrix having random integer values between 1 and 10. Call the matrix A.
Determine the eigenvalues and eigenvectors of A.
1 2 3 4
5 6 7 8
6. Using MATLAB, determine the eigenvalues and eigenvectors of B = .
9 0 1 2
3 4 5 6
7. Consider the electric circuit as shown in the figure in the following figure with 𝐶=
2 3
, 𝑅1 = 1, 𝑅2 = 5 and 𝐿 = 2. Determine the current 𝐼 and the voltage 𝑉 through the circuit.
3
Let us consider the spring-mass problem with two masses 𝑚1 = 𝑚2 = 1 and three
Determine the Eigen values and Eigen vectors of the resulting equations.
32
Ex. No.: 2 Date:
Aim:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
33
34
3. PLOTTING AND VISUALIZING SINGLE VARIABLE FUNCTIONS
Coding: Coding:
y = sin(x); y = sin(x);
plot(x,y) plot(x,y)
xlabel('x') xlabel('x')
ylabel('sin(x)') ylabel('sin(x)')
Output:
Figure: 7
35
Exercise - 3
( x + 2)
2
1. Write a MATLAB code to plot the function f ( x ) = for −2 x 6.
3 + 2 x2
cos( 0.5 x )
2. Use plot command to plot the function f ( x ) = e 4sin ( 3x ) in the domain
−20 x 20.
4. Plot the function y = sin x cos ( 2 x ) using MATLAB for − x with dashed line.
5. An electrical circuit that includes a voltage source vs with an internal resistance rs , and
vs 2 RL
a load resistance RL . The power dissipated in the load is given by P = . Plot
( RL + rs )
2
7. Thepositionxasafunctionoftimeofaparticlethatmovesalongastraightlineisgiven
by:
𝑥(𝑡) = −0.1𝑡 4 + 0.8𝑡 3 + 10𝑡 − 70
The velocity v(t) of the particle is determined by the derivative of x(t) with
respect to t. Use the plot command to make the plot of the velocity. Label the
axes appropriately with the correct units.
8. The area of the aortic value 𝐴𝜐 in cm2, can be estimated by the equation (Hakki
Formula :)
𝑄
𝐴𝜈 = ,
√𝑃𝐺
where 𝑄 is the cardiac output in L/min, and PG is the difference between the left
ventricular systolic pressure and the aortic systolic pressure (in mm Hg). Using
MATLAB make the plot curve of 𝐴𝜐 , versus PG for 2 ≤ 𝑃𝐺 ≤ 60 mmHg with
𝑄 = 4𝐿/𝑚𝑖𝑛. Label the axes and use a legend.
36
Ex. No.: 3 Date:
Aim:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
37
38
4. SOLVING FIRST AND SECOND ORDER ORDINARY
DIFFERENTIAL EQUATIONS
Aim: To compute the solution of first order and second order ordinary differential equations
with constant coefficients.
(1) Solution of First order ordinary differential equation: First order differential
equation can be solved by using “dsolve” function. The differential equation to be
solved must be specified by using “= =” and represent differentiation by using
the diff function.
Syntax:
(i) dsolve(eqn), where “eqn” is the equation to be solved.
(ii) dsolve(eqn, cond), where “eqn” is the equation to be solved and “cond” is the
initial condition.
𝑑𝑦
Example: 1 Solve 𝑑𝑥 + 𝑦𝑡𝑎𝑛𝑥 = 𝑐𝑜𝑠𝑥 without and with initial condition 𝑦(0) = 1.
Coding: Coding:
syms y(x)
syms y(x)
ode = diff(y)+y*tan(x) == cos(x);
ode = diff(y)+y*tan(x) == cos(x); cond = y(0) == 1;
y(x) = dsolve(ode) y(x) = dsolve(ode, cond)
Output: Output:
>> Linear >> Linear1
y(x) = cos(x) + x*cos(x)
y(x) = C1*cos(x) + x*cos(x)
(2) Solution of Second order ordinary differential equation: Second order differential
equations can also be solved using the dsolve with the above syntax.
Input: Input:
syms y(x) syms y(x)
ode = diff(y,2)+3*diff(y,1)+2*y ode = diff(y,2)+5*diff(y,1)+4*y == x^2+7*x+9;
== 0;
y(x) = dsolve(ode) y(x) = dsolve(ode);
y(x)=simplify(y(x))
Output:
>> Second Output:
y(x) = >> Second4
C1*exp(-x) + C2*exp(-2*x) y(x) = C1*exp(-x) + C2*exp(-4*x) + x^2/4 +
(9*x)/8 + 23/32
39
Exercise – 4
dy y
1. Write MATLAB code to solve − = e x ( x + 1) .
dx x + 1
dy y
2. Solve − = x3 − 3 by means of MATLAB.
dx x
dy
3. Write MATLAB program to solve the ODE + y sec x = tan x.
dx
dy sin 2 x
4. Solve + y cos x = by using dsolve function.
dx 2
dy
5. Develop a MATLAB code using dsolve function to solve sec x = y + sin x.
dx
dy
6. Solve x log x + y = 2 log x by means of MATLAB.
dx
dx x 2 y cos y
7. Solve the first order ODE + = by MATLAB program.
dy cos y 1 + sin y
8. The current in a circuit containing a resistance ‘𝑅’, inductance ‘𝐿’ and voltage 𝐸𝑠𝑖𝑛𝑤𝑡
𝑑𝑖
is given by 𝐿 𝑑𝑡 + 𝑅𝑖 = 𝐸𝑠𝑖𝑛𝑤𝑡. If 𝑖 = 0 at 𝑡 = 0, then find current flowing through
the circuit.
9. A radioactive substance obeys the equation 𝑦′ = 𝑘𝑦 where 𝑘 < 0 and ‘𝑦’ is the mass of
the substance at time 𝑡. Suppose that initially, the mass of the substance is 𝑦(0) = 𝑀 >
0. Determine the mass of the substance at any time.
10. The equation governing the buildup of charge, 𝑞(𝑡), on the capacitor of an RC circuit
𝑑𝑞 1
is 𝑅 𝑑𝑡 + 𝐶 𝑞 = 𝑣0 , where 𝑣0 is the constant d.c. voltage. Initially, the circuit is relaxed
and the circuit is then ‘closed’ at 𝑡 = 0 and so 𝑞(0) = 0 is the initial condition for
the charge. Solve the differential equation for 𝑞(𝑡).
Figure 8
2
d y dy
11. Create a script file to solve 2
− 3 + 2 y = e3 x .
dx dx
d2y dy
12. Write MATLAB code to determine the solution of 2 − 6 + 9 y = e3 x .
dx dx
d2y dy
13. Create a m-file and find the solution of 2
− 7 + 6 y = 2sin 3 x.
dx dx
40
14. Open a m-file and write the MATLAB code and run it to obtain the solution of ODE
( D 2 + D) y = x 2 + 2 x + 4
15. Write a MATLAB program in a script file the produces the solution of the second order
ODE ( D − D − 2) y = 2 x + 5.
2 2
16. Solve the following second order ordinary differential equation by means of MATLAB
program
y '' − 2 y ' + 10 y = 20 x 2 + 2 x − 8.
18. The deflection of a strut of length 𝑙 with one end (𝑥 = 0) built-in and the other end
𝑑2 𝑦 𝑎2 𝑅(𝑙−𝑥)
supported and subjected to end thrust ′𝑃′ satisfies the equation + 𝑎2 𝑦 = .
𝑑𝑥 2 𝑃
𝑑𝑦
Assume 𝑦 = = 0 at 𝑥 = 0. Find the deflection curve.
𝑑𝑥
19. A particle is executing an SHM about the origin ‘𝑂’ from which the distance ‘𝑥’ of the
particle is measured. Initially 𝑥 = 20 and velocity is zero and the equation of motion is
𝑥̈ + 𝑥 = 0. Solve for 𝑥.
20. A condenser of capacity ‘𝐶’ is discharge through the inductance ‘𝐿’ and a resistance
‘𝑅’ in series and the charge ‘𝑞’ at any time ‘𝑡’ satisfies the equation
𝑑2 𝑞 𝑑𝑞 𝑞
𝐿 𝑑𝑡 2 + 𝑅 𝑑𝑡 + 𝐶 = 0. Given that 𝐿 = 0.25 Henry, 𝑅 = 250 Ohms, = 2 ×
𝑑𝑞
10−6 Farads and when 𝑡 = 0, the charge 𝑞 is 0.002 Coulumbs and the current = 0.
𝑑𝑡
41
42
Ex. No.: 4 Date:
Aim:
Problem No:
Input & Coding:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
43
44
5. SOLUTION OF SIMULTANEOUS FIRST ORDER ODES
Aim: To find solution of simultaneous first order linear differential equations with constant
coefficients.
Code: Simultaneous first order differential equation with constant coefficients can be solved
by using “dsolve” function.
Output Output:
45
Exercise - 5
dx dy
1. Use MATLAB to solve the system of equations + 4 x + 3 y = t , + 2 x + 5 y = et .
dt dt
dx dy
2. Solve the simultaneous equations + 5 x + y = et , + x + 5 y = e5t in MATLAB.
dt dt
dx dy
3. Use MATLAB to solve the system of equations + 2 x + 3 y = 0, + 2 y = 2e2t .
dt dt
4. Write MATLAB code to solve the following system of simultaneous first order equations
dx dy dx
+ + 3x = sin t , − x + y = cos t.
dt dt dt
5. Create a script file “simultaneous” to solve the system of equations
( D − 1) x + Dy = 2t + 1, ( 2D + 1) x + 2Dy = t.
dx dy
6. Use MATLAB to solve the system of equations = y + t , = −2 x + 3 y + 1.
dt dt
dx dy
7. Create a m-file to solve the system of equations + y − 1 = sin t , + x = cos t.
dt dt
dx dy dx
8. Use MATLAB to solve the system of equations + + 3x = sin t , + y − x = cos t.
dt dt dt
9. Consider the 𝑅𝐿 (resistance/inductance) circuit with a voltage 𝑣(𝑡) applied as shown in Figure
9. If 𝑖1 𝑎𝑛𝑑 𝑖2 denote the currents in each loop we obtain, using Kirchhoff’s voltage law:
𝑑𝑖1
(i) In the right loop: 𝐿1 + 𝑅2 (𝑖1 − 𝑖2 ) + 𝑅1 𝑖1 = 𝑣(𝑡)
𝑑𝑡
𝑑𝑖
(ii) In the left loop: 𝐿2 𝑑𝑡2 + 𝑅2 (𝑖2 − 𝑖1 ) = 0.
Suppose, in the above circuit, that 𝐿1 = 0.8 ℎ𝑒𝑛𝑟𝑦, 𝐿2 = 1 ℎ𝑒𝑛𝑟𝑦, 𝑅1 = 1.4 Ω ,
𝑅2 = 1 Ω. Solve the problem.
Figure 9
10. Consider the vibrating system shown below:
Figure 10
The system consists of two equal masses, both 𝑚 = 1, and 3 springs of the same stiffness 𝑘 =
2. The governing differential equations can be obtained by applying Newton’s second law
𝑑 2 𝑦1 𝑑 2 𝑦2
which is given by 𝑚 = −𝑘𝑦1 + 𝑘(𝑦2 − 𝑦1 ); 𝑚 = −𝑘(𝑦2 − 𝑦1 ) − 𝑘𝑦2 . Solve the
𝑑𝑡 2 𝑑𝑡 2
differential equations and find 𝑦1 (𝑡)and 𝑦2 (𝑡).
46
Ex. No.: 5 Date:
Aim:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
47
48
6. SOLVING SECOND ORDER ODE BY VARIATION OF
PARAMETERS
Aim: To solve second order ordinary differential equation with constant coefficients by
applying the method of variation of parameters.
Example: Solve the equation (𝐷2 + 1)𝑦 = 𝑐𝑜𝑠𝑒𝑐𝑥 by the method of variation of parameters.
Coding:
syms y(x) a C1 C2 y1 y2 k1 k2
S = dsolve (heqn)
y1 = cfs1(trms1 == C1);
y2 = cfs2(trms2 == C2);
W = simplify (y1*diff(y2)-diff(y1)*y2);
R = 1/sin(x);
A = - int((R*y2)/W,x)
B = int((R*y1)/W,x)
sol = (A+k1)*y1+(B+k2)*y2
Output:
A = -x
B = log(sin(x))
49
Exercise - 6
𝑑2 𝑦
1. Use MATLAB to solve the + 𝑦 = sec 𝑥 by method variation of parameters.
𝑑𝑥 2
𝑑2 𝑦
2. Create a m-file to solve the 𝑑𝑥 2 + 𝑦 = tan 𝑥 by method variation of parameters.
d2 y 7𝑑𝑦
3. Solve the method variation of parameters dx2 + − 8𝑦 = 𝑒 2𝑥 in MATLAB.
𝑑𝑥
6. Develop a MATLAB code to solve the differential equation (𝐷2 + 1)𝑦 = sec 2 𝑥 by
applying the method of variation of parameters.
𝑑2 𝑦
7. Write a MATLAB Program to solve + 4𝑦 = 4𝑡𝑎𝑛2𝑥 by applying the method of
𝑑𝑥 2
variation of parameters.
8. Find the general solution of 𝑦 ′′ + 𝑛2 𝑦 = 𝑐𝑜𝑠𝑒𝑐𝑛𝑥 in MATLAB.
50
Ex. No.: 6 Date:
Aim:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
51
52
7. DETERMINING LAPLACE AND INVERSE LAPLACE
TRANSFORM OF BASIC FUNCTIONS
Aim: Compute Laplace and inverse Laplace Transform of basic functions by using built-in
functions.
Syntax:
(1) 𝑙𝑎𝑝𝑙𝑎𝑐𝑒(𝑓), where “𝑓” is the function of ‘𝑡’ for which Laplace transform is to be
determined.
(2) 𝑖𝑙𝑎𝑝𝑙𝑎𝑐𝑒(𝐹), where “𝐹” is the function of ‘𝑠’ for which inverse Laplace transform is to
be determined.
1
Example -1: Find the Laplace transform of .
√𝑡
Coding:
syms t
f = 1/sqrt(t);
laplace(f)
Output:
ans =pi^(1/2)/s^(1/2)
1
Example - 2: Find the inverse Laplace transform of .
𝑠2
syms a t
Coding:
f = exp(-a*t);
laplace(f)
syms s
F = 1/s^2;
ans =1/(a + s)
ilaplace(F)
Output:
ans = t
syms s
F = 1/s^2;
ilaplace(F)
ans=t
53
Exercise – 7
1. Use MATLAB to find 𝐿[5 − 3𝑒 −𝑡 + 2𝑠𝑖𝑛3𝑡].
2. Create a m-file to determine 𝐿[𝑒 −2𝑡 𝑡 2 ].
3. Find the Laplace Transformation cos 2 3𝑡 in MATLAB.
𝑠𝑖𝑛ℎ𝑡
4. Write MATLAB code to obtain the Laplace Transform of .
𝑡
𝑡
5. Use MATLAB to obtain the value of 𝐿 [∫0 𝑡𝑒 𝑡 𝑠𝑖𝑛𝑡 𝑑𝑡].
1
6. Create a m-file to determine 𝐿−1 [𝑠−3].
1
7. Determine L−1 [( ] using MATLAB.
𝑠+1)2 +1
2𝑠−5
8. Using MATLAB function to obtain the inverse Laplace transform of .
9𝑠2 −25
𝑠
9. Write MATLAB code to find the inverse Laplace Transformation (𝑠+2)2
.
1
10. Use MATLAB to find 𝐿−1 [𝑠(𝑠+3)].
Figure 11
54
Ex. No.: 7 Date:
Aim:
Problem No:
Input & Coding:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
55
56
8. SOLUTION OF SECOND ORDER ODE BY EMPLOYING
LAPLACE TRANSFORMS
Aim: To obtain the solution of second order linear differential equation with constant
coefficients by applying Laplace transform.
Coding:
syms s t Y
f = exp(-t);
F = laplace(f, t, s);
Y1 = s*Y – 4;
Y2 = s*Y1 – 5;
Sol = ilaplace(sol, s, t)
Output:
sol = (4*s+1/(s+1)+17)/(s^2+3*s+2)
Sol = 12*exp(-t)-8*exp(-2*t)+t*exp(-t)
57
Exercise – 8
𝑑2 𝑦 𝑑𝑦 𝑑𝑦
1. Solve 𝑑𝑥 2 + 4 𝑑𝑥 − 5𝑦 = 5, given that 𝑦 = 0, 𝑑𝑥 = 2, when 𝑡 = 0.
2. Using Laplace transform, obtain the solution of 𝑦 ′′ + 5𝑦 ′ + 6𝑦 = 2, given that 𝑦(0) = 0, 𝑦 ′ (0) = 0.
3. Using Laplace transform, obtain the solution of 𝑦 ′′ − 3𝑦 ′ + 2𝑦 = 𝑒 2𝑡 , given that 𝑦(0) =
−3, 𝑦 ′ (0) = 5.
4. Write a MATLAB code to solve (𝐷 2 + 𝐷)𝑦 = 𝑡 2 + 2𝑡, where 𝑦(0) = 0 and 𝑦 ′ (0) = −2 using
Laplace transform.
5. Develop a MATLAB code to solve the differential equation 𝑦 ′′ + 2𝑦 ′ − 3𝑦 = 𝑠𝑖𝑛𝑡, 𝑦(0) =
0, 𝑦 ′ (0) = 0.
𝑑2 𝑦 𝑑𝑦
6. Solve +4 + 3𝑦 = 10𝑠𝑖𝑛𝑡 by using Laplce transform, given that 𝑦(0) = 0, 𝑦 ′ (0) = 1.
𝑑𝑡 2 𝑑𝑥
𝐶 = 0.005 𝑓, 𝐸(𝑡) = 150𝑉, 𝑡 > 0, 𝑞(0) = 0 and 𝑖(0) = 0. What is the current of 𝑖(𝑡)?
11. Consider the battery of constant voltage 𝐸0 that charges the capacitor shown in figure 11.
𝑑2 𝑞 𝑑𝑞 1
The equation governing the above circuit is given by 𝐿 𝑑𝑡 2 + 𝑅 𝑑𝑡 + 𝐶 𝑞 = 𝐸(𝑡) and let 2𝜆 =
𝑅 1
𝐿
and 𝜔2 = 𝐿𝐶 . Use the Laplace transform to show that the solution 𝑞(𝑡) of 𝑞" + 2𝜆𝑞′ +
𝐸0
𝜔^2 = 𝐿
, subject to 𝑞(0) = 0, 𝑖(0) = 0.
58
Ex. No.: 8 Date:
Aim:
Problem No:
Input & Coding:
Aim 2
Viva 5
Total
Signature
59
RUBRICS FOR EVALUATING MATLAB COURSE
Execution is accurate
Execution is accurate, no Execution is halfway
but with some error
3.Execution and Result 5 error is (or) only warnings done correctly and Execution is not done.
and result is
and result is correct. result is not obtained.
incorrect or missing
Total 20
* The mark is to be valued for 80 % if the student is absent in the scheduled lab class.