Professional Documents
Culture Documents
21MPBS407005
21MPBS407005
Introduction to Scientific
Programming Laboratory
B.Sc. (Hons)
1st Semester
Index Sheet
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
Laboratory Activity 1
Title of the Laboratory Exercise: Introduction to MATLAB and Basic arithmetic operations
Aim
To understand basic features of MATLAB
To perform arithmetic operations in MATLAB
Objectives
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)
𝜋 𝜋 𝜋 𝜋 𝜋
𝑎 = 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
>> (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
>> a=x+y
a = 21.1841
>> b=x-y
b = 18.9869
>> c=x*y
c = 22.0662
>> d=x/y
d = 18.2826
>> x=a+b
x = 2.9183
>> y=a-b
y = -0.3126
>> z=a*b
z = 2.1047
>> w=a/b
w = 0.8065
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
Complex numbers:
i. >> z=(1+3i)/(1-3i)
z = -0.8000 + 0.6000i
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
Laboratory Activity 2
Title of the Laboratory: Matrix operations in MATLAB
Aim
Objectives
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
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
3 −1 2 1 4 6
𝐴=[ ] and 𝐵=[ ].
3 1 2 1 3 −1
1 2 −3 3 −1 2
3. 𝐴 = [5 0 2 ] and 𝐵 = [4 2 5].
1 −1 1 2 0 3
−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
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
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
B= 1 1 1 1
1 1 1 1
1 1 1 1
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
i) >> A.*B
ans = -2 4
6 -3
4 -2
ii) >> A(3,2)
ans = 2
>> det(A)
ans = 11
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
>> 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
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
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.
>> (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.
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
Laboratory Activity 3
Title of the Laboratory Exercise: Command line functions
Aim
To understand inline/anonymous and symbolic functions
Objectives
4. Calculations/Computations/Algorithms.
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
5. Presentation of Results
(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
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
Laboratory Activity 4
Title of the Laboratory Exercise: Scripts and functions
Aim
Objectives
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:
𝑢 = 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:
Output:
>> matrixxx
Enter any matrix : [5 3 4 4; 4 1 8 2; 1 2 1 4; 7 3 4 1;]
5
1
1
1
45.0000
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
Laboratory Activity 5
Title of the Laboratory Exercise: Relational and logical operators and Control structures in
MATLAB
Aim
Objectives
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:
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 = ~𝑥&(𝑦|𝑥)
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))]);
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
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 –
Year= 2001
v.
end
x = 0.2785
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021
y =0.5469
z = 0.9575
ans =0.2785
8. Comments
Marks
Particulars
Maximum Awarded
Results 15
Procedures/Steps /Write up 10
Total 25
Laboratory Activity 6
Title of the Laboratory Exercise: Looping structures in MATLAB
Aim
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021
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
FACTORIAL:
Code:
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
a:n: x
Name: SAINATH S JEEVANGI Roll Number: 21MPBS407021
ans = 0 2 4 6 8 10
GP
s = a*r. ^ (0:9)
Output:
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
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
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
Laboratory Activity 7
Title of the Laboratory Exercise: Symbolic computations: Differentiation and integration
Aim
Objectives
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
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
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
Laboratory Activity 8
Title of the Laboratory Exercise: 2D and 3D plots
Aim
Objectives
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]
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')
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')
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