Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

Name: UTTAM KUAMR DEWAN Roll Number: 21MPBS407031

Introduction to Scientific
Programming Laboratory
B.Sc. (Hons)

1st Semester

Name : SAINATH S JEEVANGI


Roll Number : 21MPBS407021
Department : Mathematics & Statistics

Faculty of Mathematical & Physical Sciences


Ramaiah University of Applied Sciences
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Ramaiah University of Applied Sciences


Private University Established in Karnataka State by Act No. 15 of 2013

Faculty Mathematical and Physical Science

Programme B.Sc. (Hons) Mathematics

Year/Semester 1st Semester

Name of the Laboratory Introduction to Scientific Programming

Laboratory Code 18MPL107A

List of Laboratory Activities

1. Introduction to MATLAB and Basic arithmetic operations in MATLAB


2. Matrix operations in MATLAB
3. Command line functions
4. Scripts and functions
5. Relational and logical operators and Control structures in MATLAB
6. Looping structures in MATLAB
7. Symbolic computations: Differentiation and integration
8. 2D and 3D plots
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Index Sheet

Sl. Total Marks Awarded


Lab Experiment
No. (Max. Marks 25)

1
Introduction to MATLAB and Basic arithmetic operations in MATLAB

2
Matrix operations in MATLAB

3
Command line functions

4
Scripts and functions

5
Relational and logical operators and Control structures in MATLAB

6
Looping structures in MATLAB

7
Symbolic computations: Differentiation and integration

8
2D and 3D plots

9
Term Test marks

Total marks: (Max. Marks 225)

Component 1 (Lab Internal Marks): (Max. Marks 25)

Signature of the Staff In-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 1
Title of the Laboratory Exercise: Introduction to MATLAB and Basic arithmetic operations

1. Introduction and Purpose of Software


MATLAB is a high-performance language for technical computing. In this laboratory
exercise, students get into the MATLAB. In this introduction, students understand the
simple MATLAB operations also get familiar with simple arithmetic operations.

2. Aim and Objectives

Aim
 To understand basic features of MATLAB
 To perform arithmetic operations in MATLAB

Objectives

At the end of this lab, the student will be able to:

 How to log on
 Invoke MATLAB
 Do a few simple calculations
 How to quit MATLAB
 Perform addition, subtraction, multiplication and division for numbers
3. Experimental Procedure
Students are expected to work the following commands in Command window:
i. MATLAB as a Scientific calculator
ii. Creating MATLAB variables and overwriting variable
iii. Entering multiple statements per line
iv. Floating point number
v. Getting help
vi. Miscellaneous commands
vii. Arithmetic operations: Addition, subtraction, Multiplication and division
of numbers
viii. Mathematical functions: trigonometric, logarithmic and exponential

4. Calculations/Computations/Algorithms.
 Scientific calculator :
25 1 −1
i. Evaluate 25−1 and compare with (1 − 25)
√5−1
ii. Evaluate 3 2
−1
(√5+1)

iii. Find, Area = 𝜋𝑟2 with 𝑟 = 𝜋1/3 − 1


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

 Creating MATLAB variables and overwriting variable


i. Assign 𝑥 = 𝑒3, 𝑦 = ln 3, perform 𝑎 = 𝑥 + 𝑦, 𝑏 = 𝑥 − 𝑦, 𝑐 = 𝑥 ∗ 𝑦,
𝑥
𝑑=
𝑦

ii. Overwriting 𝑎 = log10 𝑒3 and 𝑏 = log10(10 + 1.23(𝑒3 − 2)), perform


𝑎
𝑥 = 𝑎 + 𝑏, 𝑦 = 𝑎 − 𝑏, 𝑧 = 𝑎 ∗ 𝑏, 𝑤 =
𝑏

 Entering multiple statements per line


𝜋 𝜋 𝜋
i. Enter 𝑥 = sin , 𝑦 = cos and 𝑧 = tan in a line
3 5 7
𝜋 𝜋 𝜋 𝜋 𝜋
ii. Enter 𝑎 = sin2 + cos2 , 𝑏 = csc + sec − cot in a line
4 3 3 3 4

iii. Evaluate 𝑦 = cosh2 𝑥 − sinh2 𝑥 with 𝑥 = 32𝜋 in a line


 Floating point number: Evaluate the following in the given format
i. Format short
𝑥
𝑥 = 𝑒3, 𝑦 = ln 3, perform 𝑎 = 𝑥 + 𝑦, 𝑏 = 𝑥 − 𝑦, 𝑐 = 𝑥 ∗ 𝑦, 𝑑 =
𝑦

ii. Format long


𝑎 = log10 𝑒3 and 𝑏 = log10(10 + 1.23(𝑒3 − 2)), perform 𝑥 = 𝑎 +
𝑎
𝑏, 𝑦 = 𝑎 − 𝑏, 𝑧 = 𝑎 ∗ 𝑏, 𝑤 =
𝑏

iii. Format short and long


𝜋 𝜋 𝜋
𝑥 = sin , 𝑦 = cos and 𝑧 = tan
3 5 7

𝜋 𝜋 𝜋 𝜋 𝜋
𝑎 = sin2 + cos2 , 𝑏 = csc + sec − cot
4 3 3 3 4

 Complex numbers
1+3𝑖
i. Assign 1−3𝑖
to the variable 𝑧 and display the output
𝑖𝜋
ii. Assign 𝑒 3 to the variable 𝑧 and display the output
𝑖𝜋 𝜋
iii. Execute the commands exp ( 2 ) and exp ( 2𝑖 ). Explain the difference

between the two results?


5. Presentation of Results
 Scientific calculator :
1. >> 2^5/((2^5)-1)
ans = 1.0323

>> (1-(1/2^5))^-1
ans = 1.0323
2. >> 3*((sqrt(5)-1)/(sqrt(5)+1)^2)-1
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

ans = -0.6459
3. >> r=((pi)^1/3)-1;
>> a= pi*r*r
a = 0.0070

 Creating MATLAB variables and overwriting variable:


i. >> x=exp(3);
>> y=log(3);

>> a=x+y
a = 21.1841
>> b=x-y
b = 18.9869
>> c=x*y
c = 22.0662
>> d=x/y
d = 18.2826

ii. >> a=log10(exp(3));


>> b=log10(10+(1.2^3)*(exp(3)-2));

>> x=a+b
x = 2.9183
>> y=a-b
y = -0.3126
>> z=a*b
z = 2.1047
>> w=a/b
w = 0.8065

 Entering multiple statements per line:


i. >> x=sin(pi/3),y=cos(pi/5), z=tan(pi/7)
x = 0.8660
y = 0.8090
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

z = 0.4816
ii. >> a=(sin(pi/4).^2)+(cos(pi/3).^2), b=csc(pi/3)+sec(pi/3)-cot(pi/4)
a = 0.7500
b = 2.1547

iii. >> x=32*pi, y=(cosh(x).^2)-(sinh(x).^2)


x = 100.5310
y= 0

 Floating point number: Evaluate the following in the given format


i. Format short
>> x=exp(3); y=log(3);
>> format short
>> a=x+y
a = 21.1841
>> b=x-y
b = 18.9869
>> c=x*y
c = 22.0662
>> d=x/y
d = 18.2826

ii. Format long


>> a=log10(exp(3));
>> b=log10(10+(1.2^3)*(exp(3)-2));
>> format long
x = 2.918326431366127
>> y=a-b
y = -0.312559539946616
>> z=a*b
z = 2.104733923499629
>> w=a/b
w = 0.806517752268663
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

iii. Format short and long


>> format short
>> x=sin(pi/3),y=cos(pi/5), z=tan(pi/7)
x = 0.8660
y = 0.8090
z = 0.4816

>> format long


>> x=sin(pi/3), y=cos(pi/5), z=tan(pi/7)
x = 0.866025403784439
y = 0.809016994374947
z = 0.481574618807529

>> format short


>> a=(sin(pi/4).^2)+(cos(pi/3).^2), b=csc(pi/3)+sec(pi/3)-cot(pi/4)
a = 0.7500
b = 2.1547

>> format long


>> a=(sin(pi/4).^2)+(cos(pi/3).^2), b=csc(pi/3)+sec(pi/3)-cot(pi/4)
a = 0.750000000000000
b = 2.154700538379252

 Complex numbers:
i. >> z=(1+3i)/(1-3i)
z = -0.8000 + 0.6000i

ii. >> z=exp(i*pi/3)


z = 0.5000 + 0.8660i

iii. >> exp((i*pi)/2)


ans = 0.0000 + 1.0000i
>> exp(pi/(2*i))
ans = 0.0000 - 1.0000i
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

6. Analysis and Discussions

7. Conclusions

In this activity, we learned to use MATLAB as a Scientific calculator. We created some of the MATLAB
variables and tried to overwrite their values. We then learnt how to enter multiple statements in a
line using semi colon operator. We tried using the ‘format’ command to change the floating-point
value – short, long. Also performed basic arithmetic operations like addition, subtraction,
multiplication, division of numbers and basic mathematical functions like trigonometric, logarithmic
and exponential functions – built-in functions like pi, log, exp, sin, csc etc.

8. Comments

Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 2
Title of the Laboratory: Matrix operations in MATLAB

1. Introduction and Purpose of Experiment


Matrices are the basic elements of the MATLAB environment. In this laboratory exercise,
students get familiar with the different operations on matrices. They will also get familiar
built-in commands of matrices.

2. Aim and Objectives

Aim

 To perform matrix operation in MATLAB

Objectives

At the end of this lab, the student will be able to:

 Create vector and matrices


 Perform arithmetic operations of matrices
 Memorise built-in commands of matrices

3. Experimental Procedure
Students are expected to create a document. Also students are expected perform the
following in command window:
i. Elementary matrices: zeros, ones, rand, eye and diag commands
ii. Entering a vector
iii. Entering a matrix
iv. Matrix indexing
v. Colon operator and linear spacing operator
vi. Creating a sub-matrix
vii. Deleting row or column
viii. Addition, Subtraction, division of matrices and
ix. Elementwise operations
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

4. Calculations/Computations/Algorithms.
 Elementary matrices: zeros, ones, rand, eye and diag commands. Execute the
following and observe
i. A=zeros(3), B=zeros(3,2)
ii. A=ones(4), B=ones(3,4)
iii. A=rand(3)
iv. A=eye(3)
v. D=diag(A)

 Entering a vector
i. Use Colon operator to enter the vectors
a) [2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 43 ]
b) [5, 1, -3, -7, -11, -15, -19, -23, -27, -31, -35]

ii. Use linspace to obtain 6 equally spaced points from 2 to 13 (including 2 and
13)
3
iii. Enter : 𝐴 = [1 4 5], 𝐵 = [6]. Perform 𝐴 ∗ 𝐵,
8

 Addition, subtraction, multiplication and division of a Matrix

2 3 −2 3
1. Let 𝐴 = [ ] and 𝐵 = [ ]. Perform 𝐴𝐵 + 𝐼2 and assign it to 𝐶.
4 5 4 −5

2 1 −1 4
2. 𝐴 = [3 −1] and 𝐵 = [ 2 3 ].
4 2 1 −1

i) Obtain element wise multiplication of 𝐴 and 𝐵.


ii) Extract the element in third row and second column of 𝐴.
4
iii) Extract [ ] from 𝐵.
3

3 −1 2 1 4 6
𝐴=[ ] and 𝐵=[ ].
3 1 2 1 3 −1

i) Obtain 𝐴−1 +𝐵+


2 3 and commeC=A9JNJAJMNnt
6 on it.
ii) Extract [ ] from 𝐴 and [ ] from 𝐵, then form −1 2 6]
[
1 2 −1 1 2 −1

1 2 −3 3 −1 2
3. 𝐴 = [5 0 2 ] and 𝐵 = [4 2 5].
1 −1 1 2 0 3

i) Determine 𝐴3𝐵2 + 𝐵−1𝐴−1 and determinant of 𝐴.


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

−3 0
ii) Replace [ 2 ] in 𝐴 by [0]
1 0
iii) Replace −1 in 𝐵 by 𝜋.

4 3 4 4 2 3 3 3
4. 𝐴 = [ 14 1 4 2] and 𝐵 = [4 1 4 1]
−1 2 1 4 4 4 3 3
4 3 4 1 4 4 2 1
i) Obtain 𝐴 + 5𝐵𝐴 + 3𝐼4
3 −1

3 4
4 1 1
ii) Extract the submatrix [ ] from 𝐵 and [1 2
] from 𝐴
4 4 1 2 4
3 2
4 4 4 3
iii) Rearrange 𝐴 as [4 2 14 1]
1 4 −1 2
4 1 4 3

4 2 −3 −1 −1 2
5. 𝐴 = [5 0 2 ] and 𝐵 = [ 4 2 5 ]. Determine 𝐴/𝐵 and 𝐴𝐵−1
1 −1 6 2 0 −3
and comment on the output.

 Transpose :
5 6 7
1. 𝐵 = [2 3 7], 𝐴=[ ]. Determine 𝐵𝐴′
8 9 10

5 3 4 4 2 3 3 3
4 1 8 2] and 𝐵 = [4 −1 4 −1
2. 𝐴 = [ ]. Verify that (𝐴𝐵)′ = 𝐵′𝐴′
1 2 1 4 4 4 3 3
7 3 4 1 4 4 2 1

 Appending a row or column


2
If 𝐴 = 𝑒𝑦𝑒(3), 𝑢 = [5 6 7] and 𝑣 = [3].
4
i) Concatenate 𝑢′ and 𝑣



4 5 6 7 5 6
1 2 3 1 8 3
1. 𝐴 = [ ] and 𝐵 = [ ]
5 5 7 5 5 9
9 8 12 5 4 10

i) Obtain 𝐴.∗ 𝐵 an 𝐴 ∗ 𝐵 and comment on it.


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

ii) Comment on the output of 𝐴𝐵 + 3 and 𝐴𝐵 + 3𝐼4

4 2 −3 −1 −1 2
2. 𝐴 = [5 0 2 ] and 𝐵 = [ 4 2 5 ]. Determine 𝐴./𝐵 and 𝐴. ^3
1 −1 6 2 0 −3
and comment on it.

5. Presentation of Results

 Elementary matrices: zeros, ones, rand, eye and diag commands.


i. >> A=zeros(3), B=zeros(3,2)
A= 0 0 0
0 0 0
0 0 0
B= 0 0
0 0
0 0

ii. >> A=ones(4), B=ones(3,4)


A= 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

B= 1 1 1 1
1 1 1 1
1 1 1 1

iii. >> A=rand(3)


A = 0.9649 0.9572 0.1419
0.1576 0.4854 0.4218
0.9706 0.8003 0.9157

iv. >> A=eye(3)


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

A= 1 0 0
0 1 0
0 0 1

v. >> D=diag(A)
D= 1
1
1
 Entering a vector
i. Use Colon operator to enter the vectors:
a) >> x=2:3:43
x = Columns 1 through 12
2 5 8 11 14 17 20 23 26 29 32 35
Columns 13 through 14
38 41

b) >> x=5:-4:-35
x= 5 1 -3 -7 -11 -15 -19 -23 -27 -31 -35

ii. >> x=linspace(2,13,6)


x = 2.0000 4.2000 6.4000 8.6000 10.8000 13.0000

iii. >> A=[1 4 5];


>> B=[3; 6; 8];
>> A*B
ans = 67

 Addition, subtraction, multiplication and division of a Matrix


1. >> A=[2 3; 4 5];
>> B=[-2 3; 4 -5];
>> C=A*B+(eye(2))
C = 9 -9
12 -12
2.
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

i) >> A.*B
ans = -2 4
6 -3
4 -2
ii) >> A(3,2)
ans = 2

iii) >> B(1:2,2)


ans = 4
3
3.
i) >> A=[3 -1 2; 3 1 2];
>> B=[1 4 6; 1 3 -1];
>> A+B+3
ans = 7 6 11
7 7 4
Here, first the matrices A and B get added then
3 is added to every element of the resultant matrix

ii) >> x=A(1:2,2:3);


>> y=B(1:2,3);
>> z=[x y]
z= -1 2 6
1 2 -1
4.
i) >> A=[1 2 -3; 5 0 2;1 -1 1];
>> B=[3 -1 2; 4 2 5; 2 0 3];
>> (A^3)*(B^2)+(inv(B)*inv(A))
ans = 627.3636 -123.0682 603.4773
900.1212 -134.0227 894.8258
5.6061 -3.8636 10.3788

>> det(A)
ans = 11
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

ii) >> A=[1 2 -3; 5 0 2;1 -1 1];


>> B=[3 -1 2; 4 2 5; 2 0 3];
>> A(:, 3)=[0; 0; 0]
A= 1 2 0
5 0 0
1 -1 0

iii) >> A=[1 2 -3; 5 0 2;1 -1 1];


>> B=[3 -1 2; 4 2 5; 2 0 3];
>> B(1,2)=pi
B = 3.0000 3.1416 2.0000
4.0000 2.0000 5.0000
2.00 0 3.0000
5.
i) >> A=[4 3 4 4; 14 1 4 2; -1 2 1 4; 4 3 4 1];
>> B=[2 3 3 3; 4 1 4 1; 4 4 3 3; 4 4 2 1];
>> (A^3)+(5*B*(inv(A)))+3*(eye(4))
ans = 1.0e+03 *
0.8927 0.4671 0.6392 0.5871
1.3415 0.6516 0.8882 0.7688
0.3375 0.1988 0.2755 0.2836
0.6940 0.3934 0.5451 0.4858

ii) >> A=[4 3 4 4; 14 1 4 2; -1 2 1 4; 4 3 4 1];


>> B=[2 3 3 3; 4 1 4 1; 4 4 3 3; 4 4 2 1];
>> B([2 4],[1 2 4])
ans = 4 1 1
4 4 1
>> A(1:4,2:3)
ans = 3 4
1 4
2 1
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

3 4
iii) >> A=[4 3 4 4; 14 1 4 2; -1 2 1 4; 4 3 4 1];
>> B=[2 3 3 3; 4 1 4 1; 4 4 3 3; 4 4 2 1];
>> C=A(:, [3 4 1 2])
C= 4 4 4 3
4 2 14 1
1 4 -1 2
4 1 4 3

6. >> A=[4 2 -3; 5 0 2; 1 -1 6];


>> B=[-1 -1 2; 4 2 5; 2 0 -3];

>> A/B
ans = -1.3333 0.3333 0.6667
1.5833 0.7917 1.7083
2.1667 0.5833 0.4167

>> A*(inv(B))
ans = -1.3333 0.3333 0.6667
1.5833 0.7917 1.7083
2.1667 0.5833 0.4167

Both the syntax display the same answer as A*B is same as A*B-1

 Transpose
1. >> A=[5 6 7; 8 9 10];
>> B=[2 3 7];
>> B*(A')
ans = 77 113

2. >> A=[5 3 4 4; 4 1 8 2; 1 2 1 4; 7 3 4 1];


>> B=[2 3 3 3; 4 -1 4 -1; 4 4 3 3; 4 4 2 1];
>> (A*B)'
ans = 54 52 30 46
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

44 51 21 38
47 44 22 47
28 37 8 31

>> (B')*(A')
ans = 54 52 30 46
44 51 21 38
47 44 22 47
28 37 8 31
Thus, verified

 Appending row or column


>> x=[u' v]
x= 5 2
6 3
7 4

 Elementwise operations
1. >> A=[4 5 6; 1 2 3; 5 5 7; 9 8 12];
>> B=[7 5 6; 1 8 3; 5 5 9; 5 4 10];
>> A.*B
ans = 28 25 36
1 16 9
25 25 63
45 32 120
>> A*B
Error using *
Inner matrix dimensions must agree.

Here, A.*B multiplies matrices elementwise, whereas A*B is like a vector


cross product. But since A has only 3 columns but B has 4 rows, A*B will
show a dimension error.
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

>> (A.*B)+3
ans = 31 28 39
4 19 12
28 28 66
48 35 123
>> (A.*B)+3*(eye(4))
Error using +
Matrix dimensions must agree.

In the first case, A is multiplied elementwise with B and 3 is added to all


elements. But in the second case, identity matrix of order 4 is being added
to the product of A and B. Therefore, we get an error for this as the
dimensions don’t match.

2. >> A=[4 2 -3; 5 0 2; 1 -1 6];


>> B=[-1 -1 2; 4 2 5; 2 0 -3];
>> A./B
ans = -4.0000 -2.0000 -1.5000
1.2500 0 0.4000
0.5000 -Inf -2.0000
>> A.^3
ans = 64 8 -27
125 0 8
1 -1 216

A./B divides elementwise elements of A with B and A.^3 is cubing every


element of A.
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

6. Analysis and Discussions

7. Conclusions

In this activity, we learnt the basic commands of matrices like zeros, ones, rand, eye and
diag. We also entered vectors, matrices, tried their indexing and created their sub-
matrices. We used colon operator and linear spacing (linspace) operators too. We learnt
how to delete rows and columns and perform basic mathematical operations along with
elementwise operations, in this activity.

8. Comments

Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 3
Title of the Laboratory Exercise: Command line functions

1. Introduction and Purpose of Experiment


In this laboratory exercise, students get familiar anonymous and symbolic functions

2. Aim and Objectives

Aim
 To understand inline/anonymous and symbolic functions

Objectives

At the end of this lab, the student will be able to:

 Create anonymous and symbolic functions


 Evaluate functions for values in matrix
3. Experimental Procedure
Students are expected to create a document. Also students are expected perform the
following in command window:
i. anonymous functions
ii. Symbolic functions

4. Calculations/Computations/Algorithms.

 Create inline/Anonymous functions evaluate

i. Create 𝑓(𝑥) = 𝑥2 sin(𝑥) and evaluate 𝑓(1.5), 𝑓(3.1), 𝑓(5), 𝑓(−2) and at 𝑋 =
[−9, 4, 11]
1
ii. Create 𝑔(𝑡) = sin(2𝑡) + 𝑒𝑡(𝑡3 + 1) + , and evaluate 𝑔(0.1), 𝑔(10), 𝑔(2𝜋)
𝑡2−1
iii. Create 𝑓(𝑥, 𝑦) = 𝑥2 + 𝑦2 + 2𝑥𝑦 + 4, and evaluate𝑓(1, 2), 𝑓(−1, 1), 𝑓(𝜋, 3)
iv. Create 𝑔(𝑢, 𝑣) = log(𝑢2 + 𝑣2) − 𝑒𝑢−𝑣 − (𝑢2 + 𝑣2), find 𝑔(−2, 1), 𝑔(0.1, 1.5)

 Symbolic functions

i. If 𝑓(𝑥) = 𝑥2 − 2𝑥 + 1 then perform (a) simplify, (b) evaluate 𝑓(2), 𝑓(−3)


ii. If 𝑓(𝑥) = (𝑥 − 1)(𝑥 − 2)(𝑥 + 3) then perform (a) expand, (b) evaluate 𝑓(−1),
𝑓(2)
iii. If 𝑓(𝑥, 𝑦) = 𝑥2 + 𝑦2 + 2𝑥𝑦 + 4, then evaluate 𝑓(1, 2), 𝑓(−1, 1),
𝑓(0.5, 3.4), 𝑓(𝜋, 3)

 Evaluate without using symbolic and anonymous function


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

i. Evaluate 𝑦 = sin2 𝑥 on 𝑥 = [𝜋, 𝜋 , 2𝜋]


2
0 −2 5
ii. Evaluate 𝑦 = 𝑥𝑒𝑥 − tan 𝑥 on the matrix 𝑋 = [𝜋 2𝜋 1]
1 3𝜋/2 6

5. Presentation of Results

Using Anonymous Function


(i) f=@(x) ((x^2)*sin(x))
f(1.5) = 2.2444
f(3.1) = 0.3996
f(5) = -23.9731
f(-2) = -3.6372
f(-9) = -33.3816
f(4) = -12.1088
f(11) = -120.9988
(ii) g=@(t) (sin(2*t)+(exp(t))*(t^3+1)+1/(t^2-1))
g(0.1) = 0.2948
g(10) = 2.2048e+07
g(2*pi) = 1.3336e+05
(iii) f=@(x,y) (x^2+y^2+2*x*y+4)
f(1,2) = 13
f(-1,1) = 4
f(pi,3) = 41.7192
(iv) g=@(u,v) ((log(u^2+v^2))-exp(u-v)-(u^2+v^2))
g(-2,1) = -3.4403
g(0.1,1.5) = -1.6912

Using Symbolic Function


(i) syms f(x)
f(x)=x^2-2*x+1
(a) f(x)=simplify(f(x))
f(x) = (x - 1)^2
(b) f(2)= 1
f(-3)= 16
(ii) syms f(x)
f(x)=(x-1)*(x-2)*(x+3)
(a) f(x)=expand(f(x))
f(x) = x^3 - 7*x + 6
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

(b) f(-1) = 12
f(2) = 0
(iii) syms f(x,y)
f(x,y)=x^2+y^2+2*x*y+4
f(1,2) = 13
f(-1,1) = 4
f(0.5,3.4) = 19.21
f(pi,3) = pi^2 + 6*pi + 13

Evaluate without using symbolic and anonymous function


(i) x=[pi,pi/2,2*pi]
y=(sin(x)).^2
y = 0.0000 1.0000 0.0000
(ii) x=[0,-2,5;pi,2*pi,1;1,3*pi/2,6]
y=x*(exp(x))-tan(x)
y = 1.0e+15 *
-0.0000 -0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 -5.4437 0.0000
6. Analysis and Discussions

7. Conclusions

In this activity, we learnt to use symbolic and anonymous functions using user defined
functions (using @ symbol). We also used the function ‘syms x’, where we didn’t use
symbolic or anonymous functions. We also learnt to use 2 new commands – simplify and
expand, where simplify simplifies the equation to the simplest one and expand expands
the equation completely.

8. Comments

Marks
Particulars
Maximum Awarded
Results 15
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 4
Title of the Laboratory Exercise: Scripts and functions

1. Introduction and Purpose of Experiment


So far in these lab sessions, all the commands were executed in the Command Window.
The problem is that the commands entered in the Command Window cannot be saved
and executed again for several times.

2. Aim and Objectives

Aim

 To create a file with a list of commands table, save and run.

Objectives

At the end of this lab, the student will be able to:

 Understand editor window

 Create M-File Scripts

 Create M-File Functions


 Understand save and run programs

3. Experimental Procedure
Students are expected to create a document by creating the script file. Also students
are expected perform the following in editor window:
i. Array arithmetic operations
ii. Vector multiplications
iii. Simple programmes
iv. Inputs and outputs

4. Calculations/Computations/Algorithms.
Write MATLAB function to perform the following:

i. Input as time output as 𝑠(𝑡) = 𝑢𝑡 + 1 𝑎𝑡 2 where 𝑎 = 9.8 𝑚𝑠−2,


2

𝑢 = 2 𝑚𝑠−1
5 3 4 4
ii. If input as 𝐴 = [
4 1 8 2] then output as
1 2 1 4
7 3 4 1
a. 𝑎 = 𝐴−1,
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

b. b=diagonal of 𝐴,
c. c= determinant of 𝐴 and

d. d=𝐴3 − 2𝐴2 + 4𝐴 − 5

iii. Enter the input as rectangular coordinates (−1,1, √6) display the
output both spherical and cylindrical coordinates.
5. Presentation of Results

i. Function:
function [u,a,s]=distance(t)
u=2;
a=9.8;
s=((u*t)+(1/2*a*(t^2)));
end

Main Code:
t=input('Enter time to be calculated : ');
[u,a,s]=distance(t);
fprintf('The distance with given time is :')
disp(s)

Output:
>> distance1
Enter time to be calculated : 0
The distance with given time is : 0
>> distance1
Enter time to be calculated : 1
The distance with given time is : 6.9000
>> distance1
Enter time to be calculated : 5
The distance with given time is : 132.5000
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

ii. Function:

function [a,b,c,d]=matrixx(A)
a=inv(A);
b=diag(A);
c=det(A);
d=((A^3)-(2*(A^2))+(4*A)-5);
end

Main Code:

A=input('Enter any matrix : ');


[a,b,c,d]=matrixx(A);
disp(a)
disp(b)
disp(c)
disp(d)

Output:

>> matrixxx
Enter any matrix : [5 3 4 4; 4 1 8 2; 1 2 1 4; 7 3 4 1;]

1.6667 -0.3333 -1.3333 -0.6667


-3.3333 0.4667 2.6667 1.7333
-0.7778 0.2889 0.5556 0.3111
1.4444 -0.2222 -0.8889 -0.7778

5
1
1
1

45.0000

760 442 711 573


693 338 659 365
350 232 346 349
754 419 667 490
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

6. Analysis and Discussions

7. Conclusions

In all the previous activities we performed all commands directly in the command window.
But from this activity onwards, we have started to learn how to create a script file, create
script file functions, using function() command, save them and run. In that, we have
covered array arithmetic operations, vector multiplications and simple programmes
consisting of taking the input values from the user.

8. Comments
Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 5
Title of the Laboratory Exercise: Relational and logical operators and Control structures in
MATLAB

1. Introduction and Purpose of Experiment


MATLAB is also a programming language. Like other computer programming languages,
MATLAB has some decision making structures for control of command execution. A
relational operator compares two numbers by determining whether a comparison is true
or false.

2. Aim and Objectives

Aim

 To understand relational and logical operator and To execute specific group of


commands

Objectives

At the end of this lab, the student will be able to:

 Illustrate relational and logical operators

 Apply relational and logical operators


 Control structures of MATLAB

3. Experimental Procedure
 Students are expected to create a document by writing a program using relational
and logical operator. Also students are expected perform the following in editor
window:

i. Compare a scalar value with an array


ii. Compare two arrays
iii. Write the programmes using logical operators and if…elseif structure
iv. Other control statements: return, continue, switch
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

4. Calculations/Computations/Algorithms.
i. If 𝑥 = [−1 2 1 0 −3] and 𝑦 = [3 2 0 −8 −7] then
perform the following
a. 𝑘1 = 𝑥 < 𝑦 a. 𝑙1 = (𝑥 > 𝑦)&(𝑥 > 2)
b. 𝑘2 = 𝑥 ≤ 𝑦 b. 𝑙2 = 𝑥|𝑦
c. 𝑘3 = 𝑥 > 𝑦 c. 𝑙3 = ~(𝑥|𝑦)
d. 𝑘4 = 𝑥 == 𝑦 d. 𝑙4 = (𝑦&𝑥)|(𝑥&𝑦)
e. 𝑘5 = 𝑥 ≥ 𝑦 e. 𝑙5 = ~𝑥&(𝑦|𝑥)

ii. Roots of quadratic equation


iii. To determine if a quadratic equation has two distinct real roots
iv. To determine if the specified year is a leap year
v. Create two random numbers x and y. Write an if-statement that
displays x if x is smaller than y. Now, write an if-else statement to
display the minimum of x and y. Create a third random number z.
Write an if-statement that displays the value of z if z is larger than x
or y.

5. Presentation of Results
i.

(I)
a)
x=[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
k1 = x<y

output –
x = -1 2 1 0 -3
y = 3 2 0 -8 -7

k1 =1 0 0 0 0

b)
x=[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
k2 = x<=y

output –
x =-1 2 1 0 -3
y =3 2 0 -8 -7
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

k2 =1 1 0 0 0

c)
x=[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
k3 = x>y
output –
x =-1 2 1 0 -3
y =3 2 0 -8 -7

K3 = 0 0 1 1 1

d)
x=[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
k4 = x==y

output-
x = -1 2 1 0 -3
y =3 2 0 -8 -7

K4 =0 1 0 0 0

e)
x=[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
k5 = x>=y

output-
x =-1 2 1 0 -3
y =3 2 0 -8 -7

K5 =0 1 1 1 1

a)
x =[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
l1=(x>y)&(x>2)

output-
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

x =-1 2 1 0 -3
y=3 2 0 -8 -7

l1 = 0 0 0 0 0

b)
x =[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
l2=x|y

output-
x = -1 2 1 0 -3
y=3 2 0 -8 -7

l2 =1 1 1 1 1

c)
x =[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
l3=~(x|y)
output-
x =-1 2 1 0 -3
y=3 2 0 -8 -7

l3 = 0 0 0 0 0

d)
x =[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
l4=(y&x)|(x&y)
output-
x =-1 2 1 0 -3
y=3 2 0 -8 -7

l4 =1 1 0 0 1

e)

x =[-1 2 1 0 -3]
y=[3 2 0 -8 -7]
l5=~x&(y|x)

output-
x = -1 2 1 0 -3
y= 3 2 0 -8 -7
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

l5 =0 0 0 1 0

ii.
a= input('Co-efficient of x^2: ');
b= input('Co-efficient of x: ');
c=input('Contant term: ');
d= b^2-4*a*c;
fx=[a b c];
x=roots(fx);
disp(['The Roots are:',num2str(x(1)),' ,',num2str(x(2))]);

command window output;


Co-efficient of x^2: 2
Co-efficient of x: 11
Contant term: 5
The Roots are:-5 ,-0.5

iii.
a= input('Co-efficient of x^2: ');
b= input('Co-efficient of x: ');
c=input('Contant term: ');
d= b^2-4*a*c;
fx=[a b c];
x=roots(fx);
if d>0
disp(['The equation has two roots ', num2str(x(1)),' ,',num2str(x(2))]);
elseif d==0
disp(['The equation has one root ', num2str(x(1)),',',num2str(x(2))]);
else
disp('The equation has no real roots.');
end
command window output;

Co-efficient of x^2: 10

Co-efficient of x: 5
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Constant term: 25

The equation has no real roots.

iv.
function year
year = input (' Enter the year : ')
if mod(year,4) == 0 disp ('This is a leap year')
elseif mod(year,100) == 0 disp ('This is not a leap year')
elseif mod(year,400) == 0 disp ('This is a leap year')
else disp ('This is not a leap year')
end

output –

Enter the year : 2001

Year= 2001

This is not a leap year

v.

function [x, y, z]= small ()


x= rand()
y= rand()
z=rand ()
if x<y
fprintf('x is the smallest number')
disp(x)
else
fprintf('y is the smallest number')
disp (y)
end
if z>x
if x>y
fprintf('\nz is the greatest number')
disp (z)
end
end

end
x = 0.2785
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

y =0.5469

z = 0.9575

x is the smallest number 0.2785

ans =0.2785

6. Analysis and Discussions


7. Conclusions
In this activity, we have learnt about various relational and logical operators like =, ~=, |,
etc. We did perform various sums using these operators. We also performed programmes
using the control structures that is the if…else looping structure. We also used functions
like rand(), fprintf, mod(), input() etc.

8. Comments
Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge

Laboratory Activity 6
Title of the Laboratory Exercise: Looping structures in MATLAB

1. Introduction and Purpose of Experiment


MATLAB allows to create user defined programmes. In this laboratory exercise, students
get familiar with the writing of own programmes.

2. Aim and Objectives

Aim
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

 To create program using ‘for’ and ‘while’ loops

Objectives for the given data

At the end of this lab, the student will be able to:

 Create user defined programmes using ‘for’ loop

 Create user defined programmes using ‘while’ loop

3. Experimental Procedure
 Students are expected to create a document by writing a program using ‘if and else’.
Also students are expected perform the following in editor window:
i. Create a programmes using ‘for’ and ‘while’ loops
ii. In the programme include ‘break’ and ‘continue’ statements

4. Calculations/Computations/Algorithms.
i. Write a program to calculate the sum and factorial of real numbers.
ii. Calculate the mean and standard deviation of x.
iii. Write a MATLAB function that returns the minimum and maximum values of
a given vector
iv. Generate arithmetic and geometric series
v. Finding the smallest integer 𝑁 for which 2𝑁 is greater than one million.
vi. Create upper and lower triangular matrices

5. Presentation of Results

Sum;

Code:
r=input('Enter any real number :');
sum=0;
for i=0:r
sum=sum+i;
end
disp(sum)

Output:
>> sumfact
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Enter any real number :10


55
>> sumfact
Enter any real number :93
4371

FACTORIAL:
Code:

r=input('Enter any real number :');


fact=1;
for i=1:r
fact=fact*i;
end
disp(fact)

Output:
>> sumfact
Enter any real number :5
120
>> sumfact
Enter any real number :100
9.3326e+157
i. Code:
function meansd
A=input('Enter the vector to find out mean and standard deviation :');
sum=0;
for i=1:length(A)
sum=sum+A(i);
end
mean=sum/length(A);
sd=0;
x=0;
for i=1:length(A)
sd=(A(i)-mean)^2;
s=s^2;
x=x+s;
end
sd=sqrt(1/(length(A)-1)*(sd)*(x));
fprintf('Mean is')
disp(mean)
fprintf('Standard Deviation is')
disp(sd)
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Output:
>> meansd
Enter the vector to find out mean and standard deviation :[1,5]
Mean is 3
Standard Deviation is 2.8284
>> meansd
Enter the vector to find out mean and standard deviation :[1,2,3,4,5]
Mean is 3
Standard Deviation is 2.2361

ii. Code:
v=input('Enter a vector :');
n=length(v);
max=v(1);
min=v(1);
for k=2:n
if max<v(k)
max=v(k);
end
end
for k=2:n
if min>v(k)
min=v(k);
end
end
fprintf('Maximum value is')
disp(max)
fprintf('Minimum value is')
disp(min)

Output:
>>maxmin
Enter a vector : [34 55 1 5 6 90 76 834 67 256 1000]
Maximum value is 1000
Minimum value is 1

AP

iii.a= input (‘Enter first no. of AP sequence :’)d =

input ("Enter common difference :’)

x = input ('Enter last no. of AP sequence :’ )

a:n: x
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

>> Enter first no of AP sequence : 0

>> Enter common difference: 2

>> Enter last no. of AP sequence:

ans = 0 2 4 6 8 10

GP

a= input (‘Enter the first number of GP')

r= input (‘Enter the common ratio : ')

s = a*r. ^ (0:9)

Output:

>> input the first number of GP: 2

>> Enter the common ratio : 2

S = 2 4 8 16 32 64 128 256 512 1024

iv. Code:
n=1000000;
x=1;
while x<n
x=x*2;
end
if x>=n;
fprintf('Number that is just more than 1 million is')
disp(x);
end
p=(log(x)/log(2));
fprintf('Smallest integer N for which 2^N is greater than one million is')
disp(p)

Output:
>>smallest
Number that is just more than 1 million is 1048576

Smallest integer N for which 2^N is greater than one million is 20

v. Code:
m=input('Enter number of rows for matrix :');
n=input('Enter number of columns for matrix :');
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

l=tril(rand(m,n),-1);
u=triu(rand(m,n),1);
fprintf('Lower triangular matrix is\n')
disp(l)
fprintf('Upper triangular matrix is\n')
disp(u)

Output:
>> trimat
Enter number of rows for matrix :3
Enter number of columns for matrix :3
Lower triangular matrix is
0 0 0
0.9058 0 0
0.1270 0.0975 0

Upper triangular matrix is


0 0.9572 0.1419
0 0 0.4218
0 0 0
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

6. Analysis and Discussions


7. Conclusions

In this activity, we performed various programmes using the other control structures which are for
and while loops. We did some basic programmes like finding the sum, factorial, mean, smallest integer
greater than a particular number etc.

8. Comments
Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 7
Title of the Laboratory Exercise: Symbolic computations: Differentiation and integration

1. Introduction and Purpose of Experiment


We are now ready to see how calculus operations can be performed with MATLAB. In this
laboratory exercise, students get familiar to find differential and integration of a given
functions.

2. Aim and Objectives

Aim

 To perform symbolic computation in MATLAB

Objectives

At the end of this lab, the student will be able to:

 Perform symbolic differentiation of a function


 Perform symbolic integration of a function

 Perform symbolic algebraic operation

3. Experimental Procedure
 Students are expected to create a document by writing a script file and perform
the following:
i. Create a file for symbolic differentiation ‘diff’
ii. Create a file for symbolic integration ‘int’
iii. Create a file for symbolic root finding ‘solve’

4. Calculations/Computations/Algorithms.
i. Find the derivative of the following functions with respect to 𝑥
a. 3(𝑥 + 6) + 4(2𝑥 − 5) 𝑒2𝑥
f.
1+𝑒𝑥
b. (2𝑥 + 3)3
g. log(𝑥2 − 1)
c. 3𝑥3/2 − 9√𝑥 + 6/√𝑥
h. sin(2𝑥) + sin2 3𝑥
𝑥2+3𝑥+2
d. 𝑥2−𝑥−2
i. cos(log(2𝑥))
𝑥2

𝑥+1 j. sin−1(𝑥2) + cos−1(2𝑥)
e. 𝑥2−4 𝑥+2

ii. Integrate the following functions with respect to 𝑥


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

a. 3𝑥2 − 2𝑥 + 4 𝜋/4
f. ∫0 sec2 𝑥 tan2 𝑥 𝑑𝑥
1 1
b. 𝑥2
+ 𝑥2+1 4 √𝑥
g. ∫0 √𝑥+1
𝑑𝑥
𝑥+1
c. 𝑒
√𝑥 h. ∫1 ln 𝑥 𝑑𝑥
d. 2𝑥 𝜋/3
i. ∫0 cos 4𝑥 sin 5𝑥 𝑑𝑥
2
e. √1−4𝑥2 𝜋/2
j. ∫0 𝑒𝑥 cos 2𝑥 𝑑𝑥

5. Presentation of Results

(a) symsx
y=3*(x+6)+4*(2*x-5)
Dy=diff(y,x)
Dy=11

(b) symsx
y=(2*x+3)^3
Dy=diff(y,x)
Dy=6*(2*x+3)^2

(c) symsx
y=3*x^3/2-9*sqrt(x)+6/sqrt(x)
Dy=diff(y,x)
Dy=(9*x^2)/2-9/(2*x^(1/2))-3/x^(3/2)

(d) symsx
y=(x^2+3*x+2)/(x^2-x-2)
Dy=diff(y,x)
Dy=-(2*x+3)/(-x^2+x+2)-((2*x-1)*(x^2+3*x+2))/(-x^2+x+2)^2

(e) symsx
y=x^2/(x^2-4)-(x+1)/(x+2)
Dy=diff(y,x)
Dy=(x+1)/(x+2)^2-1/(x+2)+(2*x)/(x^2-4)-(2*x^3)/(x^2-4)^2

(f) symsx
y=exp(2*x)/(1+exp(x))
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Dy=diff(y,x)
Dy=(2*exp(2*x))/(exp(x)+1)-exp(3*x)/(exp(x)+1)^2

(g) symsx
y=log(x^2-1)
Dy=diff(y,x)
Dy=(2*x)/(x^2-1)

(h) symsx
y=sin(2*x)+(sin(3*x))^2
Dy=diff(y,x)
Dy=2*cos(2*x)+6*cos(3*x)*sin(3*x)

(i) symsx
y=cos(log(2*x))
Dy=diff(y,x)
Dy=-sin(log(2*x))/x

(j) symsx
y=asind(x^2)+acosd(2*x)
Dy=diff(y,x)
Dy=(360*x)/(pi*(1-x^4)^(1/2))-360/(pi*(1-4*x^2)^(1/2))

(a) symsx
y=3*x^2-2*x+4
I=int(y,x)
I=x*(x^2-x+4)

(b) symsx
y=1/(x^2+1)+1/x^2
I=int(y,x)
I=atan(x)-1/x

(c) symsx
y=(x+1)/(sqrt(x))
I=int(y,x)
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

I=(2*x^(1/2)*(x+3))/3

(d) symsx
y=2^x
I=int(y,x)
I=2^x/log(2)

(e) symsx
y=2/(sqrt(1-4*x^2))
I=int(y,x)
I=asin(2*x)

(f) symsx
y=((sec(x))^2)*((tan(x))^2)
I=int(y,x,0,pi/4)
I=1/3

(g) symsx
y=sqrt(x)/(1+sqrt(x))
I=int(y,x,0,4)
I=log(9)

(h).symsx
y=log(x)
I=int(y,x,1,2.71828)
I=(67957*log(67957/25000))/25000-42957/25000

(i).symsx
y=(cos(4*x))*(sin(5*x))
I=int(y,x,0,pi/3)
I=13/36

(j).symsx
y=(exp(x))*(cos(2*x))
I=int(y,x,0,pi/2)
I=-exp(pi/2)/5-1/5
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

6. Analysis and Discussions


7. Conclusions

In this activity, we have learnt the 2 types of symbolic computations that is symbolic differentiation
and integration. For this, we have used diff() and int() functions for differentiation and integration
respectively.

8. Comment
Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

Laboratory Activity 8
Title of the Laboratory Exercise: 2D and 3D plots

1. Introduction and Purpose of Experiment


MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of
computation is possible with very few commands. In this laboratory exercise, students get
familiar to create 2D graphs, subgraphs and 3D plots.

2. Aim and Objectives

Aim

 To present graphical results of the function

Objectives

At the end of this lab, the student will be able to:

 Present graphical solution of any function in 2D


 Present graphical solution of implicit function

 Display graphs of more than one variable functions in 3D


3. Experimental Procedure
 Students are expected to create a document by writing a script file and perform the
following:
i. Create a display 2D and 3D graphs of given functions
ii. Edit the graphs for change of display style
iii. Create sub-plots and multiple plots in a graph sheet

4. Calculations/Computations/Algorithms.
i. Plot the following functions in 2D graphs
a. 𝑦 = 𝑒𝑥 in [0, 10] Plot the given functions in a single figure
b. 𝑦 = sin 2𝑥 in [−2𝜋, 2𝜋] window using subplot
c. 𝑦 = 𝑥2 + 4𝑥 + 4 in [−5, 5] a. 𝑦 = ln 𝑥 in [0.5, 5]
Plot the following in one frame b. 𝑦 = 𝑒𝑥 ln 𝑥 in [0.5, 5]
d. 𝑦 = 𝑥3 − 3𝑥2 + 4𝑥 − 5 in [−5, 5] c. 𝑦 = 2sin(1/𝑥) in [−4, 4]
𝑦 = tan 2𝑥 in [−𝜋/2, 𝜋/2] d. 𝑦 = 𝑒𝑥 sin(1/𝑥) in [−2, 2]

ii. Plot the following functions in 3D space


Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

a. (cos 𝑡 , sin 𝑡 , 𝑡) , 𝑡 ∈ [−5𝜋, 5𝜋]


3𝜋 5𝜋
b. ((4 + sin 20𝑡) cos 𝑡 , (4 + sin 20 𝑡) sin 𝑡 , cos 20𝑡), 𝑡 ∈ [− , ]
2 2

c. (cos 𝑡 , − cos 𝑡 , sin 𝑡), 𝑡 ∈ [−12𝜋, 12𝜋]


d. (𝑡2, 𝑡, 2) 𝑡 ∈ [−2, 10]
e. (𝑡2, 𝑡4, 𝑡6) 𝑡 ∈ [−5, 5]

5. Presentation of Results

1.a) x=0:10;
y=exp(x) ;
plot(x, y)
xlabel(‘x’)
ylabel(‘e^x’)
title(‘plot of e^x’)

b) x=-2*pi:2*pi;
y=sin(2*x);
plot(x, y)
xlabel(‘x’)
ylabel(‘sin 2x’)
title(‘plot of sin 2x’)
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

c) x=-5:5;

y=x.^2+4*x+4;
plot(x,y)
xlabel(‘x’)
ylabel('y=x.^2+4*x+4')
title('plot of y=x^2+4*x+4,x={-5:5}')
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

d) x=-5:5;
y=x.^3-3*x.^2+4*x-5;
x1=-pi/2:pi/2;
y2=tan(2*x1);
plot(x,y,'--b*', x1,y1,':r*')
xlabel('x,x1')
ylabel('y,y1')
legend('y=x^3-3*x^2+4*x-5','y=tan(2*x)');
title('plot of y,y1')

e) Plot the given functions in a single figure window using subplot


a) x=0.5:5;
y=log(x);
b) x1=0.5:5;
y1=exp(x1).*log(x1);
c) x2=-4:4;
y2=2.*sin(1./x2);
d) x3=-2:2;
y3=exp(x).*sin(1./x3);
subplot(2,2,1);
plot(x,y,'--r*')
xlabel('x')
ylabel('y')
title('subplot of y')
subplot(2,2,2);
plot(x1,y1,':b*')
xlabel('x1')
ylabel('y1')
title('subplot of y1')
subplot(2,2,3);
plot(x2,y2,'--g*')
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

xlabel('x2')
ylabel('y2')
title('subplot of y2')
subplot(2,2,4);
plot(x3,y3,':m*')
xlabel('x3')
ylabel('y3')
title('subplot of y3')

3d plot
a) t=-5*pi:5*pi;

u=sin(t);
v=cos(t);
plot3(u,v,t,':r*')
xlabel('sin(t)')
ylabel('cos(t)')
zlabel('t')
title('3d plot of sin(t),cos(t) & t ')
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

b) t=(-3*pi)/2:(5*pi)/2;
u=(4+sin(20.*t)).*cos(t);
v=(4+sin(20.*t)).*sin(t);
w=cos(20.*t);
plot3(u,v,w,':b*')
xlabel('u')
ylabel('v')
zlabel('w')
title('3d plot of u,v & w')
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

c) t=-12*pi:12*pi;
u=cos(t);
v=-cos(t);
w=sin(t);
plot3(u,v,w,':r*')
xlabel('u')
ylabel('v')
zlabel('w')
title('3d plot of u,v & w')

d)
t=linspace(-2,10);
st=(t.^2);
ct=t;
tt=2;
plot3(st,ct,tt)
>> plots1
Error using plot3
Vectors must be the same lengths.
Error in plots1 (line 5)
plot3(st,ct,tt)

e) t=-5:5;
u=t.^2;
v=t.^4;
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021

w=t.^6;
plot3(u,v,w,':r*')
xlabel('u')
ylabel('v')
zlabel('w')
title('3d plot of u,v & w')

6. Analysis and Discussions


7. Conclusions

In this activity, we have learnt to plot the 2 types of graphs – 2d and 3d, where 2d is a surface plot and
3d is a curve plot. Using the command plot() and plot3(), we can plot 2d and 3d plots respectively

8. Comments
Marks
Particulars
Maximum Awarded
Results 15

Procedures/Steps /Write up 10

Total 25

Signature of Staff in-charge

You might also like