Вы находитесь на странице: 1из 16

NUMERICAL Part – I

1. Create user interface to calculate the following matrix operations:


PROJECT
• Matrix addition
• Matrix subtraction
• Matrix multiplication
• Matrix transpose
• Matrix determinant
• Matrix inverse

SOURCE CODE :-

clear all; close all; clc;


fprintf('\t--Matrix Operations--\n');
fprintf('-----------------MENU-----------------\n\t1. Matrix addition\n\t2. Matrix subtraction');
fprintf('\n\t3. Matrix multiplication\n\t4. Matrix transpose\n\t5. Matrix determinant');
fprintf('\n\t6. Matrix inverse\n');
ch = input('Enter Your Choice(1-6) : ');

if ch == 1
fprintf('\nFirst Matrix :-\n');
m1 = input('No. Of Rows : ');
n1 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m1
for j = 1 : n1
a(i, j) = input('');
end
end
a = reshape(a, m1, n1);

fprintf('\nSecond Matrix :-\n');


m2 = input('No. Of Rows : ');
n2 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m2
for j = 1 : n2
b(i, j) = input('');
end
end

b = reshape(b, m2, n2);

if m1 ~= m2 || n1 ~= n2
fprintf('\n\tMatrix Addition Not Possible!!\n');
else
c = a + b;
fprintf('\nResultant Matrix :-\n');
disp(c);
end

elseif ch == 2
fprintf('\nFirst Matrix :-\n');
m1 = input('No. Of Rows : ');
n1 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m1
for j = 1 : n1
a(i, j) = input('');
end
end
a=reshape(a, m1, n1);

fprintf('\nSecond Matrix :-\n');


m2 = input('No. Of Rows : ');
n2 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m2
for j = 1 : n2
b(i, j) = input('');
end
end

b = reshape(b, m2, n2);

if m1 ~= m2 || n1 ~= n2
fprintf('\n\tMatrix Subtraction Not Possible!!\n');
else
c = a - b;
fprintf('\nResultant Matrix :-\n');
disp(c);
end

elseif ch == 3
fprintf('\nFirst Matrix :-\n');
m1 = input('No. Of Rows : ');
n1 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m1
for j = 1 : n1
a(i, j)=input('');
end
end
a = reshape(a, m1, n1);

fprintf('\nSecond Matrix :-\n');


m2 = input('No. Of Rows : ');
n2 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m2
for j = 1 : n2
b(i, j) = input('');
end
end

b=reshape(b,m2,n2);

if m1 ~= m2 || n1 ~= n2
fprintf('\n\tMatrix Multiplication Not Possible!!\n');
else
c = a .* b;
fprintf('\nResultant Matrix :-\n');
disp(c);
end

elseif ch == 4
fprintf('\nMatrix :-\n');
m1 = input('No. Of Rows : ');
n1 = input('No. Of Columns : ');
disp('Enter The Elements :-');
for i = 1 : m1
for j = 1 : n1
a(i, j)=input('');
end
end
a = reshape(a, m1, n1);

b = a';
fprintf('\nResultant Transpose Matrix :-\n');
disp(b);

elseif ch == 5
fprintf('\nMatrix :-\n');
m1 = input('Order Of The Matrix : ');
disp('Enter The Elements :-');
for i = 1 : m1
for j = 1 : m1
a(i, j)=input('');
end
end
a = reshape(a, m1, m1);

b = det(a);
fprintf('\nDeterminant :');
disp(b);

elseif ch == 6
fprintf('\nMatrix :-\n');
m1 = input('Order Of The Matrix : ');
disp('Enter The Elements :-');
for i = 1 : m1
for j = 1 : m1
a(i, j)=input('');
end
end
a = reshape(a, m1, m1);

b = inv(a);
fprintf('\nResultant Inverse Matrix :-\n');
disp(b);
else
disp('ERROR!! Wrong Input!');
end

OUTPUTS :-

MATRIX ADDITION MATRIX SUBTRACTION MATRIX MULTIPLICATION

MATRIX TRANSPOSE MATRIX DETEMINANT MATRIX INVERSE


2. Create using user interface to calculate the root of an algebraic or transcendental
equation :
• Bisection method
• Newton-Raphson method
• Regula-Falsi method

SOURCE CODE :-

clear all; close all; clc;


format long;
fprintf('\t--Root Finding--\n');
fprintf('-----------------MENU-----------------\n\t1. Bisection Method');
fprintf('\n\t2. Newton-Raphson Method\n\t3. Regula-Falsi method\n');
ch = input('Enter Your Choice(1-3) : ');

if ch == 1
% Change here for different functions
f = @(x) (x*x*x)-(4*x)-1;

e = input('Enter Desired Accuracy : ');


fprintf('Enter The Interval a & b :-\n');
a = input('');
b = input('');

fprintf('\nThe value of X.............\n');


while abs(b-a) > e
x=(a+b)/2;
if(f(a)*f(x)<0)
b=x;
else
a=x;
end
disp(x);
end
disp('The Root is : ');
disp(x);

elseif ch == 2
%Change here for different functions
f=@(x) cos(x)-(2*x);
%The derivative of the above function
df=@(x) -sin(x)-2;

e = input('Enter Desired Accuracy : ');


a = input('Enter The Intial value of X : ');
fprintf('\nThe value of X.............\n');
b = a;
x = a-f(a)/df(a);
disp(x);
a = x;
while abs(b-a) > e
b=a;
x=a-f(a)/df(a);
disp(x);
a=x;
end
disp('The Root is : ');
disp(x);

elseif ch == 3
% Change here for different functions
f = @(x) (x*x*x)-(5*x)-7;

e = input('Enter Desired Accuracy : ');


fprintf('Enter The Interval a & b :-\n');
a = input('');
b = input('');

fprintf('\nThe value of X.............\n');


while abs(b-a) > e
x= a -((b-a)*f(a))/(f(b)-f(a));
if(f(a)*f(x)<0)
b=x;
else
a=x;
end
disp(x);
end
disp('The Root is : ');
disp(x);
else
disp('ERROR!! Wrong Input!');
end

OUTPUTS :-

BISECTION METHOD NEWTON-RAPHSON METHOD REGULA-FALSI METHOD


3. Create user interface to calculate f(x) using the appropriate method from the
following based on user's input (Equal/unequal interval and value of x) :
• Newton's forward interpolation
• Backward interpolation
• Lagrange's interpolation
• Newton's divided difference interpolation
________________________________________________________________

SOURCE CODE :-

clear all; close all; clc;


format long;
fprintf('\t\t--Calculation Of f(x)--\n');
fprintf('--------------------------MENU--------------------------\n\t1. Newton Forward Interpolation');
fprintf('\n\t2. Newton Backward Interpolation\n\t3. Lagrange Interpolation');
fprintf('\n\t4. Newton Divided Difference Interpolation\n');
ch = input('Enter Your Choice(1-4) : ');

if ch == 1
n = input('Enter the number of terms : ');
disp('Enter The X Terms : ');
for i = 1:n
x(i) = input('');
end
y = zeros(n, n);
disp('Enter The Y Terms : ');
for i = 1:n
y(i, 1) = input('');
end

z = input('Enter The Required Value Of X : ');


h = x(2) - x(1);
u = (z - x(1))/h;
sum = y(1, 1);
for j = 2:n
for i = 1:(n-j)+1
y(i, j) = y(i+1, j-1) - y(i, j-1);
end
end
disp(y);

for i = 2:n-1
product = y(1, i);
for j = 1:i-1
product = product * (u-j+1)/j;
end
sum = sum + product;
end
disp('The Result Is : ');
disp(sum);

elseif ch == 2
n = input('Enter the number of terms : ');
disp('Enter The X Terms : ');
for i = 1:n
x(i) = input('');
end
y = zeros(n, n);
disp('Enter The Y Terms : ');
for i = 1:n
y(i, 1) = input('');
end

z = input('Enter The Required Value Of X : ');


h = x(2) - x(1);
u = (z - x(n))/h;
sum = y(n, 1);
for j = 2:n
for i = 2:n
y(i, j) = y(i, j-1) - y(i-1, j-1);
end
end
disp(y);

for i = 2:n-1
product = y(n, i);
for j = 1:i-1
product = product * (u+j-1)/j;
end
sum = sum + product;
end
disp('The Result Is : ');
disp(sum);

elseif ch == 3
n = input('Enter the number of terms : ');
disp('Enter The X Terms : ');
for i = 1:n
x(i) = input('');
end
y = zeros(n, n);
disp('Enter The Y Terms : ');
for i = 1:n
y(i) = input('');
end

z = input('Enter The Required Value Of X : ');


sum = 0.0;
for i = 1:n
product = 1.0;
for j = 1:n
if j~=i
product=product*(z-x(j))/(x(i)-x(j));
end
end
sum = sum + product*y(i);
end
disp('The Result Is : ');
disp(sum);

elseif ch == 4
n = input('Enter the number of terms : ');
disp('Enter The X Terms : ');
for i = 1:n
x(i) = input('');
end

k = 1;

y = zeros(n, n);
disp('Enter The Y Terms : ');
for i = 1:n
y(i, 1) = input('');
end

z = input('Enter The Required Value Of X : ');

sum = y(1, 1);


for j = 2:n
for i = 1:(n-j)+1
y(i, j) = (y(i+1, j-1) - y(i, j-1))/(x(i+k)-x(i));
end
k = k + 1;
end
disp(y);

for i = 2:n
product = y(1, i);
for j = 1:i-1
product = product * (z-x(j));
end
sum = sum + product;
end
disp('The Result Is : ');
disp(sum);
else
disp('ERROR!! Wrong Input!');
end
OUTPUTS :-

NEWTON’S FORWARD INTERPOLATION NEWTON’S BACKWARD INTERPOLATION

LAGRANGE’S INTERPOLATION NEWTON’S DIVIDED DIFFERENCE INTERPOLATION


2
4. Create user interface to find x3 dx using the appropriate method from the
0
following based on user's input (Based on the value of n):
• Trapezoidal rule
• Simpson's 1/3rd rule
• Weddle's rule
________________________________________________________________

SOURCE CODE :-

clear all; close all; clc;


format long;
fprintf('\t\t--Integral Calculation--\n');
fprintf('--------------------------MENU--------------------------\n\t1. Trapezoidal Rule');
fprintf('\n\t2. Simpson 1/3rd Rule\n\t3. Weddle Rule\n');
ch = input('Enter Your Choice(1-3) : ');

% Change here for different functions


f = @(x) (x*x*x);

if ch == 1
a = input('Enter The Lower Limit : ');
b = input('Enter The Upper Limit : ');
n = input('Enter Number Of Interval : ');
sum = 0.0;
h = (b-a)/n;

fprintf('\n----------------------------------');
fprintf('\n\tX\t\tf(X)');
fprintf('\n----------------------------------');

while a<b
sum = sum + (h/2) * (f(a) + f(a+h));
fprintf('\n%f\t\t%f',a,f(a));
a = a+h;
end
fprintf('\n%f\t\t%f',b,f(b));
fprintf('\n\nThe Integral Value is:%f\n',sum);

elseif ch == 2
a = input('Enter The Lower Limit : ');
b = input('Enter The Upper Limit : ');
n = input('Enter Number Of Interval : ');
j = 1;
sum = 0.0;
k = 0;
h=(b-a)/n;
sum = sum + f(a);
fprintf('-----------------------------------\n');
fprintf('Iteration\tX\tf(X)\n');
fprintf('-----------------------------------\n');

for i = 1:n-1
c = a+h;
if k == 0
sum = sum + 4*f(c);
k = 1;
else
sum = sum + 2*f(c);
k = 0;
end
fprintf('%d\t%f\t%f\n',j,c,f(c));
j = j + 1;
a = c;
end
fprintf('%d\t%f\t%f\n',j,b,f(b));
j = j + 1;
sum = sum + f(b);
sum = sum * (h/3);
fprintf('\n\nThe value of integral is = %f \n',sum);
elseif ch == 3
a = input('Enter The Lower Limit : ');
b = input('Enter The Upper Limit : ');
n = input('Enter Number Of Interval(Multiple Of 6) : ');
sum = 0.0;
k = 1;
h = (b-a)/n;

fprintf('\n----------------------------------');
fprintf('\n\tX\t\tf(X)');
fprintf('\n----------------------------------\n');

c = a;

while a<b
sum = sum + (3*h/10)*(f(a)+5*f(a+h)+f(a+2*h)+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)+f(a+6*h));
a = a + 6*h;
end

while c<=b
fprintf('%d\t%f\t%f\n',k,c,f(c));
k = k + 1;
c = c + h;
end
fprintf('\n\nThe value of integral is= %f \n',sum);
else
disp('ERROR!! Wrong Input!');
end

OUTPUTS :-

TRAPEZOIDAL RULE SIMPSON’S 1/3 rd RULE WEDDLE’S RULE


5. Create user interface to solve the system of linear equations using:
• Gauss elimination
• Gauss-Seidal
________________________________________________________________

SOURCE CODE :-

clear all; close all; clc;


format long;
fprintf('\t--Solving System of Linear Equations--\n');
fprintf('--------------------------MENU--------------------------\n\t1. Gauss Elimination Method');
fprintf('\n\t2. Gauss-Seidal Method\n');
ch = input('Enter Your Choice(1-2) : ');

if ch == 1
n = input('Enter The Order Of The Matrix : ');
fprintf('Enter The Elements Of Co-Efficient Matrix :-\n');
for i = 1 : n
for j = 1 : n
a(i, j) = input('');
end
end
a = reshape(a, n, n);
fprintf('\nCo-Efficient Matrix :-\n');
disp(a)

fprintf('Enter The Elements Of Right Hand Side Vector :-\n');


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

for k = 1:n-1
for i = k+1:n
z = a(i, k)/a(k, k);
for j = 1:n
a(i, j) = a(i, j) - z*a(k, j);
end
b(i) = b(i) - z*b(k);
end
end

x(n) = b(n)/a(n, n);

for k = n-1:-1:1
sum = b(k);
for j = n:-1:k+1
sum = sum - a(k, j)*x(j);
end
x(k) = sum/a(k, k);
end

fprintf('\nThe Solution Of The System Of Linear Equations Is :- \n');


for i = 1:n
fprintf('x[%d]=%f\t\n',i,x(i));
end

elseif ch == 2
n = input('Enter The Order Of The Matrix : ');
fprintf('Enter The Elements Of Co-Efficient Matrix :-\n');
for i = 1 : n
for j = 1 : n
a(i, j) = input('');
end
end
a = reshape(a, n, n);
fprintf('\nCo-Efficient Matrix :-\n');
disp(a)

fprintf('Enter The Elements Of Right Hand Side Vector :-\n');


for i = 1 : n
b(i) = input('');
end
fprintf('\n');
e = input('Enter The Desired Accuracy : ');
for i = 1:n
sum=0.0;
for j = 1:n
if i ~= j
sum = sum + abs(a(i, j));
end
if abs(a(i, i)) < sum
fprintf('\nThe System Is NOT Diagonally Dominant!');
end
end
end
fprintf('\nThe System Is Diagonally Dominant.');

for i = 1:n
x0(i) = 0.0;
end

t = 0;
while t==0
for i = 1:n
x(i) = x0(i);
sum = b(i);
for j = 1:n
if j ~= i
sum = sum - a(i, j)*x0(j);
end
end
x0(i) = sum/a(i, i);
end

for i = 1:n
if abs(x0(i)-x(i))>e
t=0;
break;
else
t=1;
end
end
end

fprintf('\n\nThe Solution Of The System Is......\n');


for i = 1:n
fprintf('x[%d]=%f\n',i,x(i));
end
else
disp('ERROR!! Wrong Input!');
end
OUTPUTS :-

GAUSS ELIMINATION METHOD GAUSS-SEIDAL METHOD


Part - II

Project 6: Write a menu driven program to calculate the determinant of a 3x3


matrix using one of the following method based on user's choice:
• Sarrus method
• Cramer's rule

--C-PROGRAM--
SOURCE CODE :-

#include<stdio.h>

void crammer()
{
int a[3][3], d, i, j;

printf("Enter The Elements Of A 3x3 Matrix :-\n");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

d = (a[0][0]*a[1][1]*a[2][2])-(a[0][0]*a[1][2]*a[2][1]) + (a[0][1]*a[1][2]*a[2][0]) - (a[0]


[1]*a[1][0]*a[2][2]) + (a[0][2]*a[1][0]*a[2][1])-(a[0][2]*a[1][1]*a[2][0]);

printf("Determinant = %d\n",d);

void sarrus()
{
int a[3][3], s=0,d=0,i,j;
printf("Enter The Elements Of A 3x3 Matrix :-\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
for(i=0;i<3;i++)
{
s = s + a[0][i] * a[1][(i+1)%3] * a[2][(i+2)%3] ;
d = d + a[2][i] * a[1][(i+1)%3] * a[0][(i+2)%3] ;
}
printf("Determinant = %d\n",(s-d));
}

int main()
{
int ch;
printf("\t\t--Calculation Of Deteminant--\n");
printf("--------------------------MENU--------------------------\n\t1. Sarrus Method");
printf("\n\t2. Cramer's rule\n");
printf("Enter Your Choice(1-2) : ");
scanf("%d",&ch);

switch(ch)
{
case 1 : sarrus();
break;

case 2 : crammer();
break;

default : printf("ERROR!! Wrong Input!");


}

return 0;
}
OUTPUTS :-

SARRUS METHOD CRAMER’S RULE

--MATLAB--
SOURCE CODE :-

clear all; close all; clc;


format long;
fprintf('\t--Calculation Of Deteminant--\n');
fprintf('---------------------MENU---------------------\n\t1. Sarrus Method');
fprintf('\n\t2. Cramer Rule\n');
ch = input('Enter Your Choice(1-2) : ');

if ch == 1
s = 0;
d = 0;
fprintf('Enter The Elements Of A 3x3 Matrix :-\n');
for i = 1 : 3
for j = 1 : 3
a(i, j) = input('');
end
end
a = reshape(a, 3, 3);

for i = 1:3
s = s + a(1, i) * a(2, mod((i+1),3) + 1) * a(3, mod((i+2), 3) + 1) ;
d = d + a(3, i) * a(2, mod((i+1),3) + 1) * a(1, mod((i+2), 3) + 1) ;
end

fprintf('\nDeterminant = %d\n',(s-d));

elseif ch == 2
fprintf('Enter The Elements Of A 3x3 Matrix :-\n');
for i = 1 : 3
for j = 1 : 3
a(i, j) = input('');
end
end
a = reshape(a, 3, 3);

d = (a(1, 1)*a(2, 2)*a(3, 3))-(a(1, 1)*a(2, 3)*a(3, 2)) + (a(1, 2)*a(2, 3)*a(3, 1)) - (a(1, 2)*a(2, 1)*a(3, 3)) + (a(1,
3)*a(2, 1)*a(3, 2))-(a(1, 3)*a(2, 2)*a(3, 1));

fprintf('\nDeterminant = %d\n',d);
else
disp('ERROR!! Wrong Input!');
end
OUTPUTS :-

SARRUS METHOD CRAMER’S RULE

____________________________________________________________________________________________________

Вам также может понравиться