You are on page 1of 23

INDEX

S.No. Aim of the program Date Page No.

Write a program to perform various


1 5/04/21 1
algebric operations on matrices.
Write a program to calculate factorial of a
2 5/04/21 4
number
Write a program to print the Fibonacci
3 12/04/21 5
series.
Write a program to plot a circle of given
4 12/04/21 6
radius.

5 Implement calculator using matlab. 19/04/21 7

6 Table Of 11,13,15,17,19 19/04/21 11

Write a program to implement Trapezoidal


7 19/04/21 12
rule.
Write a program to implement Simpson’s
8 17/05/21 13
one third rule.
Write a program to implement Simpson’s
9 17/05/21 14
three eight rule.
Solution of initial value problem using
10 31/05/21 15
Euler’s method.
Solve the ordinary partial differential
equation dy/dx = x+y given y=1 when x=0
11 31/05/21 16
at x=0.2 using Runge-Kutta method of
order 4.
Solution of algebric and transcendental
12 7/06/21 17
equation using Bisection method.
Fit a second degree parabola to the
13 7/06/21 18
following data.

14 Fit a straight line to the following data. 7/06/21 20

15 Plotting of unit step function in MATLAB. 7/06/21 22


DATE: 5/04/21 Page Number-1

Program Number – 1
Aim: Write a program to perform various algebraic operations on matrices.
Program:
clc;
close all;
clear all;

A = input('Enter the Matrix A=')


B = input('Enter the Matrix B=')

Sum = A+B
Diff = A-B
Product = A*B

DeterminantOfA = det(A)
InverseOfA = inv(A)
TraceOfA = trace(A)
RankOfA = rank(A)
TransposeOfA = A'

DeterminantOfB = det(B)
InverseOfB = inv(B)
TraceOfB = trace(B)
RankOfB = rank(B)
TransposeOfB = B'

Output:
Page Number-2
Page Number-3
DATE: 5/04/21 Page Number-4

Program Number – 2
Aim: Write a program to calculate factorial of a number
Program:
Num = input('Enter num=');
fact = 1;
for i=Num:-1:2
fact = i * fact;
end
fprintf('Factoial = %d',fact)

Output:
DATE: 12/04/21 Page Number-5

Program Number – 3
Aim: Write a program to print the Fibonacci series.
Program:
num = input('Enter Num = ');

a = 0;
b =1;
c = 1;

for i = 0:num
fprintf('\t%d',c);
c = a+b;
a = b;
b = c;
end

Output:
DATE: 12/04/21 Page Number-6

Program Number – 4
Aim: Write a program to plot a circle of given radius.
Program:
r = input('Enter the radius:');
t = linspace(0, 2*pi,100);
x = r*cos(t);
y = r*sin(t);

plot(x,y)
axis('equal')
xlabel('x-axis')
ylabel('y-axis')
title('circle')

Output:
DATE: 19/04/21 Page Number-7

Program Number – 5
Aim: Implement calculator using MATLAB.
Program:
n = input('\n Press 1 for addition \n Press 2 for substraction \n Press 3 for
Multiplication \n Press 4 Division \n Press 5 for sine \n Press 6 for cosine
\n Press 7 for Log \n Press 8 for exponential\n');
switch n
case 1
a = input('First Number ');
b = input('Second Number ');
c = a + b;
fprintf('Sum = %d', c);
case 2
a = input('First Number ');
b = input('Second Number ');
c = a - b;
fprintf('Difference = %d', c);
case 3
a = input('First Number ');
b = input('Second Number ');
c = a * b;
fprintf('Product = %d', c);
case 4
a = input('First Number ');
b = input('Second Number ');
c = a / b;
fprintf('Division = %d', c);
case 5
a = input('Enter angel ');
b = sin(a * (pi/180));
fprintf('%f' , b);
case 6
a = input('Enter angel ');
b = cos(a * (pi/180));
fprintf('%f' , b);
case 7
a = input('Enter number ');
b = log(a);
fprintf('%f' , b);
case 8
a = input('Enter number ');
c = exp(a)
Page Number-8

fprintf('%f' , c);
end

Output:
Page Number-9
Page Number-10
DATE: 19/04/21 Page Number-11

Program Number – 6
Aim: Table of 11, 13,15,17,19.
Program:
clc;
close all;
clear all;
for i=11:2:19
for j = 1:10
fprintf('\n%d * %d = %d',i,j,(i*j));
end
end

Output:
DATE: 19/04/21 Page Number-12

Program Number – 7
Aim: Write a program to implement Trapezoidal rule.
Program:
clc;
close all;
clear all;
a = input('Enter lower limit');
b = input('Enter upper limit');
n = input('Enter number of intervals');
h = (b-a)/n;
r=0;
a=0;
s=0;
func = inline('1/(1 + x^2)');
for i=a:h:b
if i==a || i==b
r = r + func(i);
else
s = s + func(i);
end
end

answer = (h/2)*(r+2*s);

fprintf('Answer = %f', answer);

Output:
DATE: 17/05/21 Page Number-13

Program Number – 8
Aim: Write a program to implement Simpson’s one third rule.
Program:
clc;
close all;
clear all;
a = input('Enter lower limit:');
b = input('Enter upper limit:');
n = input('Enter number of intervals:');
h = (b-a)/n;
r = 0;
s = 0;
t = 0;

func = inline('1/(1+x^2)');

for i=a:h:b
if (i==a || i==b)
r = r + func(i);
else if (rem(i,2) == 0)
s = s + func(i);
else
t = t + func(i);
end
end
end

answer = (h/3)*(r+2*s+4*t);

fprintf('Answer = %f', answer);

Output:
DATE: 17/05/21 Page Number-14

Program Number – 9
Aim: Write a program to implement Simpson’s three eight rule.
Program:
clc;
close all;
clear all;
a = input('Enter lower limit:');
b = input('Enter upper limit:');
n = input('Enter number of intervals:');
h = (b-a)/n;
r = 0;
s = 0;
t = 0;

func = inline('1/(1+x^2)');

for i=a:h:b
if (i==a || i==b)
r = r + func(i);
else if (rem(i,3) == 0)
s = s + func(i);
else
t = t + func(i);
end
end
end

answer = (3*h/8)*(r+2*s+3*t);

fprintf('Answer = %f', answer);

Output:
DATE: 31/05/21 Page Number-15

Program Number – 10
Aim: Solution of initial value problem using Euler’s method.
Program:
clc;
close all;
clear all;

x0=input('Enter the initial value of x:');


y0=input('Enter the initial value of y:');
n=input('Enter the number of intervals:');

xn=input('Enter the value of xn:');

h=(xn-x0)/n;
func = inline('x+y');
x=x0; y=y0;

for i=1:n
y=y+h*func(x,y);
x=x0+i*h;
end
fprintf('y(%d)=%f\n',x,y);

Output:
DATE: 31/05/21 Page Number-16

Program Number – 11
Aim: Solve the ordinary partial differential equation dy/dx = x+y given y=1
when x=0 at x=0.2 using Runge-Kutta method of order 4.

Program:
clc;
close all;
clear all;

f=inline('x+y');
x=input('enter x0 : ');
y=input('enter y0 : ');
h=input('enter the number h : ');

k1=h*f(x,y);
k2=h*f((x+h/2),(y+k1/2));
k3=h*f((x+h/2),(y+k2/2));
k4=h*f((x+h),(y+k3));
k=1/6*(k1+2*k2+2*k3+k4);
y=y+k;

fprintf(' y=%f \n ',y);

Output:
DATE: 7/06/21 Page Number-17

Program Number – 12
Aim: Solution of algebric and transcendental equation using Bisection
method.

Program:
clc;
a = input('Enter a: ');
b = input('Enter b: ');

func = inline('x^3-4*x-9');

for i = 1:15
x1 = (a+b)/2;
if (func(x1) < 0)
a = x1;
else
b = x1;
end
end

fprintf('Root by bisection method: %f',x1);

Output:
DATE: 7/06/21 Page Number-18

Program Number – 13
Aim: Fit a second degree parabola to the following data.
X 0 1 2 3 4
Y 1 1.8 1.3 2.5 6.3
Program:
clc;
n = input('Enter number of observations:');
fprintf('enter the values of x:');
for i=1:n
x(i) = input('');
end

fprintf('enter the values of y:');


for i=1:n
y(i) = input('');
end

sumx=0;
sumx2=0;
sumx3=0;
sumx4=0;
sumy = 0;
sumxy=0;
sumx2y=0;

for i=1:n
sumx = sumx + x(i);
sumx2 = sumx2 + (x(i)*x(i));
sumx3 = sumx3 + (x(i)*x(i)*x(i));
sumx4 = sumx4 + (x(i)*x(i)*x(i)*x(i));
sumy = sumy + y(i);
sumxy = sumxy + (y(i)*x(i));
sumx2y = sumx2y + (y(i)*x(i)*x(i));
end

A = [n, sumx, sumx2; sumx, sumx2, sumx3; sumx2, sumx3, sumx4]


B = [sumy;sumxy;sumx2y]

Z = inv(A);
W = Z*B;
fprintf('The parabola equation is %3.3fx^2 %3.3fx + %3.3f',W(1),W(2),W(3));
fprintf('\nGurman Singh 11113202719\n')
Page Number-19

Output:
DATE: 7/06/21 Page Number-20

Program Number – 14
Aim: Fit a straight line to the following data.
X 6 7 7 8 8 8 9 9 10
Y 5 5 4 5 4 3 4 3 3
Program:
clc;
n=input('Enter the value of n:');

for i=1:n
fprintf('Enter the value of x%d',i);
x(i)=input('.');
end

for i=1:n
fprintf('Enter the value of y%d',i);
y(i)=input('.');
end

sumx=0;sumx2=0;sumxy=0;sumy=0;

for i=1:n
sumx=sumx+x(i);
sumx2=sumx2+x(i)*x(i);

sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
end

A=[n,sumx;sumx,sumx2]
B=[sumy;sumxy]

z=inv(A);
w=z*B;

fprintf('straight y=%f+%.3fx\n',w(1),w(2));
Page Number-21

Output:
DATE: 7/06/21 Page Number-22

Program Number – 15
Aim: Plotting of unit step function in MATLAB.
Program:
clc;
clear all;
close all;
t=-5:0.0001:5;
y=[t>=0];
plot(t,y,'r');
title('Unit Step function');
xlabel('t');
ylabel('u(t)');

Output:

You might also like