IT-2 3rd Semester Computational Methods Lab
Ques No. 1 Program to perform Arithmetic operations on two matrices and to
find transpose of a Matrix.
CODE
#include<iostream>
using namespace std;
int main()
{
int m, n;
cout<<"Enter row & column :\n";
cin >> m >> n;
int i, j, ch;
int k,sum=0;
int mat1[m][n], mat2[m][n], mat3[m][n];
cout<<"Select the Matrix Operation to be performed. "<<endl;
cout<<"1. Addition."<<endl;
cout<<"2. Subtraction."<<endl;
cout<<"3. Multiplication."<<endl;
cout<<"4. Transpose."<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the First Matrix :\n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}
cout<<"Enter the Second Matrix :\n";
for(i = 0; i < n; i++)
1 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
{
for(j = 0; j < n; j++)
cin >> mat2[i][j];
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
cout<<"Matrix after Addition.\n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << mat3[i][j] << “ ”;
cout << endl;
}
break;
case 2:
cout<<"Enter the First Matrix :\n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}
cout<<"Enter the Second Matrix :\n";
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
cin >> mat2[i][j];
}
for(i = 0; i < m; i++)
2 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
{
for(j = 0; j < n; j++)
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
cout<<"Matrix after Subtraction.\n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout<<mat3[i][j]<<" ";
cout << endl;
}
break;
case 3:
cout<<"Enter the First Matrix :\n";
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}
cout<<"Enter the Second Matrix :\n";
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
cin >> mat2[i][j];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
for (k = 0; k < m; k++)
{
3 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
sum = sum + mat1[i][k]*mat2[k][j];
}
mat3[i][j] = sum;
sum = 0;
}
}
cout<<"\n Matrices after Multiplication.\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cout<<mat3[i][j]<<" ";
cout<<endl;
}
break;
case 4:
cout<<"Enter the elements of Matrix";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>mat1[i][j];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
mat3[j][i] = mat1[i][j];
}
}
cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < n; ++i)
{
4 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
for (int j = 0; j < m; ++j)
{
cout << " " << mat3[i][j];
if (j == i - 1)
cout << endl << endl;
}
}
break;
default:
cout<<"Wrong choice";
break;
}
return 0;
}
5 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
6 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 2 Program for finding roots of f(x)=0 Newton Raphson method.
CODE
#include<bits/stdc++.h>
#define EPSILON 0.001
using namespace std;
double func(double x){
return x*x*x - x*x + 2;
}
double derivFunc(double x)
{
return 3*x*x - 2*x;
}
void NewtonRaphson(double x)
{
double h = func(x) / derivFunc(x);
while (abs(h) >= EPSILON)
{
h = func(x)/derivFunc(x);
x = x - h;
}
cout << "The value of the root is : " << x;
}
int main()
{
double a = -20;
NewtonRaphson(a);
return 0;
}
7 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
8 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 3 Program for finding roots of f(x)=0 by Bisection method.
CODE
#include<bits/stdc++.h>
#define EPSILON 0.01
using namespace std;
double func(double x)
{
return x*x*x - x*x + 2;
}
void Bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout<<"You have not assumed right a and b\n";
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
c = (a+b)/2;
if (func(c) == 0.0)
break;
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout<<"The value of root is : "<<c;
}
int main()
{
double a = -200, b = 300;
Bisection(a, b);
return 0;
}
9 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
10 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 4 Program for finding roots of f(x)=0 by Secant method.
CODE
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define f(x) (x*x*x)-(2*x)-5
using namespace std;
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
cout<< setprecision(6)<< fixed;
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
cout<< endl<<"**************"<< endl;
cout<<"Secant Method"<< endl;
cout<<"**************"<< endl;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}
x2 = x1 - (x1 - x0) * f1/(f1-f0);
f2 = f(x2);
cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<" and f(x2) = "<< setw(10)<<
f(x2)<< endl;
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
11 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
step = step + 1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}while(fabs(f2)>e);
cout<< endl<<"Root is: "<< x2;
return 0;
}
12 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
13 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 5 To implement Langrange’s Interpolation formula.
CODE
#include<iostream>
using namespace std;
int main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;
cout<<"Enter number of data: ";
cin>>n;
cout<<"Enter data:"<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"] = ";
cin>>x[i];
cout<<"y["<< i<<"] = ";
cin>>y[i];
}
cout<<"Enter Interpolation Point: ";
cin>>xp;
for(i=1;i<=n;i++)
{
p=1;
for(j=1;j<=n;j++)
{
if(i!=j)
{
p = p* (xp - x[j])/(x[i] - x[j]);
}
}
yp = yp + p * y[i];
}
cout<< endl<<"Interpolated value at "<< xp<< " is "<< yp;
return 0;
}
14 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
15 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 6 To implement Newton’s Divided Difference formula.
CODE
#include<iostream>
#include<iomanip>
using namespace std;
float proterm(int i, float value, float x[])
{
float pro = 1;
for (int j = 0; j < i; j++) {
pro = pro * (value - x[j]);
}
return pro;
}
void dividedDiffTable(float x[], float y[][10], int n)
{
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
y[j][i] = (y[j][i - 1] - y[j + 1][i - 1]) / (x[j] - x[i + j]);
}
}
}
float applyFormula(float value, float x[],
float y[][10], int n)
{
float sum = y[0][0];
for (int i = 1; i < n; i++) {
sum = sum + (proterm(i, value, x) * y[0][i]);
}
return sum;
16 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
void printDiffTable(float y[][10],int n){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
cout <<setprecision(4) <<y[i][j] << "\t ";
}
cout << "\n";
}
}
int main(){
int n = 4;
float value, sum, y[10][10];
float x[] = { 5, 6, 9, 11 };
y[0][0] = 12;
y[1][0] = 13;
y[2][0] = 14;
y[3][0] = 16;
dividedDiffTable(x, y, n);
printDiffTable(y,n);
value = 7;
cout << "\nValue at " << value << " is "<< applyFormula(value, x, y, n) << endl;
return 0;
}
17 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
18 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 7 Program for solving numerical Integration by trapezoidal rule.
CODE
#include<iostream>
#include<math.h>
#define f(x) 1/(1+pow(x,2))
using namespace std;
int main(){
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;
stepSize = (upper - lower)/subInterval;
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++){
k = lower + i*stepSize;
integration = integration + 2 * (f(k));
}
integration = integration * stepSize/2;
cout<< endl<<"Required value of integration is: "<< integration;
return 0;
}
19 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
20 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 8 Program for solving numerical integraton by Simpson’s 1/3 rule.
CODE
#include<iostream>
#include<math.h>
#define f(x) 1/(1+pow(x,2))
using namespace std;
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;
stepSize = (upper - lower)/subInterval;
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%2==0)
{
integration = integration + 2 * (f(k));
}
else
21 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
{
integration = integration + 4 * (f(k));
}
}
integration = integration * stepSize/3;
cout<< endl <<"Required value of integration is: "<< integration;
return 0;
}
22 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
23 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 9 To implement Numerical Integration Simpson 3/8 rule.
CODE
#include<iostream>
#include<math.h>
#define f(x) 1/(1+pow(x,2))
using namespace std;
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;
stepSize = (upper - lower)/subInterval;
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%3==0)
{
integration = integration + 2 * (f(k));
}
24 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
else
{
integration = integration + 3 * (f(k));
}
}
integration = integration * stepSize*3.0/8.0;
cout<< endl <<"Required value of integration is: "<< integration;
return 0;
}
25 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
26 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No. 10 Inverse of a system of linear equations using Gauss-Jordan
method.
CODE
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
using namespace std;
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
cout<< setprecision(3)<< fixed;
cout<<"Enter order of matrix: ";
cin>>n;
cout<<"Enter coefficients of Matrix: " << endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"a["<< i<<"]["<< j<<"]= ";
cin>>a[i][j];
}
}
for(i=1;i<=n;i++)
{
27 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
for(j=1;j<=n;j++)
{
if(i==j)
{
a[i][j+n] = 1;
}
else
{
a[i][j+n] = 0;
}
}
}
for(i=1;i<=n;i++)
{
if(a[i][i] == 0.0)
{
cout<<"Mathematical Error!";
exit(0);
}
for(j=1;j<=n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=2*n;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
28 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
for(i=1;i<=n;i++)
{
for(j=n+1;j<=2*n;j++)
{
a[i][j] = a[i][j]/a[i][i];
}
}
cout<< endl<<"Inverse Matrix is:"<< endl;
for(i=1;i<=n;i++)
{
for(j=n+1;j<=2*n;j++)
{
cout<< a[i][j]<<"\t";
}
cout<< endl;
}
return(0);
}
29 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
30 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No.11 Find the Eigen values using Power method.
CODE
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<math.h>
#define SIZE 10
using namespace std;
int main()
{
float a[SIZE][SIZE], x[SIZE],x_new[SIZE];
float temp, lambda_new, lambda_old, error;
int i,j,n, step=1;
cout<< setprecision(3)<< fixed;
cout<<"Enter Order of Matrix: ";
cin>>n;
cout<<"Enter Tolerable Error: ";
cin>>error;
cout<<"Enter Coefficient of Matrix: "<< endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"a["<< i<<"]["<< j<<"]= ";
cin>>a[i][j];
31 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
}
}
cout<<"Enter Initial Guess Vector: "<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"]= ";
cin>>x[i];
}
lambda_old = 1;
up:
for(i=1;i<=n;i++)
{
temp = 0.0;
for(j=1;j<=n;j++)
{
temp = temp + a[i][j]*x[j];
}
x_new[i] = temp;
}
for(i=1;i<=n;i++)
{
x[i] = x_new[i];
}
lambda_new = fabs(x[1]);
for(i=2;i<=n;i++)
{
if(fabs(x[i])>lambda_new)
32 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
{
lambda_new = fabs(x[i]);
}
}
for(i=1;i<=n;i++)
{
x[i] = x[i]/lambda_new;
}
cout<< endl<< endl<<"STEP-"<< step<< endl;
cout<<"Eigen Value = "<< lambda_new<< endl;
cout<<"Eigen Vector: "<< endl;
cout<<"[";
for(i=1;i<=n;i++)
{
cout<< x[i]<<"\t";
}
cout<<"]";
if(fabs(lambda_new-lambda_old)>error)
{
lambda_old=lambda_new;
step++;
goto up;
}
return(0);
}
33 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
34 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
Ques No.12 Program for solving ordinary differential equation by Runge-Kutta
Method.
CODE
#include<iostream>
#include <stdio.h>
#include <math.h>
#define SIZE 50
using namespace std;
int fi,m;
double xi,xf,yi,T[SIZE];
double fp(double x, double y)
{
return 4*x*(y+sqrt(y))/(1+x*x);
}
void Affiche(double *t,int m,double xi,double xf)
{
double h,x; int i;
h=(xf-xi)/(m-1);
x=xi-h;
cout<<"\n X Y \n";
cout<<"------------------------\n";
for (i=1; i<m+1; i++)
{
x+=h;
cout<<"\t"<<x<<"\t\t\t"<<t[i];
cout<<"\n";
}
}
35 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
void Equadif1(double *t,double xi,double xf, double yi,int m,int fi)
{
int i,j,ni;
double a,b,c,d,h,x,y;
if (fi < 1) return;
h = (xf - xi) / fi / (m-1);
y = yi;
t[1] = yi;
for (i = 1; i<m+1; i++)
{
ni = (i - 1) * fi - 1;
for (j = 1; j<fi+1; j++)
{
x = xi + h * (ni + j);
a = h * fp(x,y);
b = h * fp(x+h/2,y+a/2);
c = h * fp(x+h/2,y+b/2);
x = x + h;
d = h * fp(x,y+c);
y = y + (a + b + b + c + c + d) / 6;
}
t[i+1] = y;
}
Affiche(t,m,xi,xf);
}
int main()
{
cout<<"\n DIFFERENTIAL EQUATION WITH 1 VARIABLE OF ORDER 1\n";
cout<<" of type y' = f(x,y)\n\n";
cout<<" begin value x : ";
36 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
cin>>xi;
cout<<" end value x : ";
cin>>xf;
cout<<" y value at x0 : ";
cin>>yi;
cout<<" number of points: ";
cin>>m;
cout<<" finesse : ";
cin>>fi;
Equadif1(T,xi,xf,yi,m,fi);
return 0;
}
37 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
38 REAZAUR RAHMAN
10115003121