Computational Methods
Computational Methods
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
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
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 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
#include<bits/stdc++.h>
#define EPSILON 0.01
using namespace std;
double func(double x)
{
return x*x*x - x*x + 2;
}
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
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
cout<< setprecision(6)<< fixed;
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);
12 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
13 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
#include<iostream>
using namespace std;
int main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;
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
16 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
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
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>
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
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));
}
}
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
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
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];
}
}
29 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
OUTPUT
30 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE],x_new[SIZE];
float temp, lambda_new, lambda_old, error;
int i,j,n, step=1;
31 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
}
}
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;
}
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
#define SIZE 50
using namespace std;
int fi,m;
double xi,xf,yi,T[SIZE];
35 REAZAUR RAHMAN
10115003121
IT-2 3rd Semester Computational Methods Lab
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