You are on page 1of 34

INDEX

S.NO PROGRAM DATE SIGN

1. To deduce error in polynomial equation.

2. To find the roots of non-linear equation using Bisection


method.
3. To find the roots of non-linear equation using Secant
method.
4. To find the roots of non-linear equation using
Newtons method.
5. To implement numerical integration using Trapezoidal
rule.
6. To implement numerical integration using Simpson 1/3
rule.
7. To implement numerical integration using Simpson 3/8
rule.
8. To solve system of linear equations using Gauss-
elimination method.
9. To implement Lagranges Interpolation formula.

10. To find numerical solution of ordinary differential


equations by Eulers method.

11. To find numerical solution of ordinary differential


equations by Runge Kutta method.
1) To deduce error in polynomial equation.

#include<iostream>

#include<math.h>

using namespace std;

int main()

double abs_err,rel_err,p_rel_err,t_val,a_val;

cout<<"\nEnter true value ";

cin>>t_val;

cout<<"\nEnter approximate value ";

cin>>a_val;

abs_err=fabs(t_val-a_val);

rel_err=abs_err/t_val;

p_rel_err=rel_err*100;

cout<<"\nAbsolute Error : "<<abs_err;

cout<<"\nRelative Error : "<<rel_err;

cout<<"\nPercentage Relative Error : "<<p_rel_err;

}
OUTPUT:
2) To find the roots of non-linear equation using Bisection method.

#include<bits/stdc++.h>

using namespace std;

#define EPSILON 0.01

#define e 2.71828

// The function is x*(e)^x-1

double func(double x)

return x*pow(e,x)-1;

// Prints root of func(x) with error of EPSILON

void bisection(double a, double b)

if (func(a) * func(b) >= 0)

cout << "\nYou have not assumed right a and b\n";

return;

double c = a;

while ((b-a) >= EPSILON)

// Find middle point

c = (a+b)/2;
// Check if middle point is root

if (func(c) == 0.0)

break;

// Decide the side to repeat the steps

else if (func(c)*func(a) < 0)

b = c;

else

a = c;

cout << "\nThe value of root is : " << c;

// Driver program to test above function

int main()

double a,b;

cout<<"\nEnter initial values a and b";

cin>>a>>b;

bisection(a, b);

return 0;

}
OUTPUT:
3) To find the roots of non-linear equation using Secant Method.

#include <bits/stdc++.h>

using namespace std;

// function takes value of x and returns f(x)

float f(float x)

// we are taking equation as x^4-x-10

float f = pow(x, 4) - x - 10;

return f;

void secant(float x1, float x2, float E)

float n = 0, xm, x0, c;

if (f(x1) * f(x2) < 0) {

do {

// calculate the intermediate value

x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));

// check if x0 is root of equation or not

c = f(x1) * f(x0);

// update the value of interval

x1 = x2;
x2 = x0;

// update number of iteration

n++;

// if x0 is the root of equation then break the loop

if (c == 0)

break;

xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));

} while (fabs(xm - x0) >= E); // repeat the loop

// until the convergence

cout << "Root of the given equation=" << x0 << endl;

cout << "No. of iterations = " << n << endl;

} else

cout << "Can not find a root in the given inteval";

// Driver code

int main()

float x1 , x2 , E = 0.0001;

cout<<\nEnter assumed values : ;

cin>>x1>>x2;

secant(x1, x2, E);

return 0;
}

OUTPUT:
4) To find the roots of non-linear equation using Newtons Method.

#include<bits/stdc++.h>

#define EPSILON 0.001

using namespace std;

// The function is 2x^3 - 2.5x - 5

double func(double x)

return 2*x*x*x - 2.5*x - 5;

// Derivative of the above function which is 6*x^x - 2.5

double derivFunc(double x)

return 6*x*x - 2.5;

// Function to find the root

void newtonRaphson(double x)

double h = func(x) / derivFunc(x);

while (abs(h) >= EPSILON)

h = func(x)/derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)

x = x - h;

cout << "\nThe value of the root is : " << x;

// Driver program to test above

int main()

double x0;

cout<<"\nEnter initial value";

cin>>x0;

newtonRaphson(x0);

return 0;

}
OUTPUT:
5) To implement numerical integration using Trapezoidal rule.

#include<iostream>

#include<math.h>

using namespace std;

// A function to return f(x) = sqrt(x*x+1)

float y(float x)

return sqrt(x*x + 1);

// Function to evalute the value of integral

float trapezoidal(float a, float b, float n)

// Grid spacing

float h = (b-a)/n;

// Computing sum of first and last terms

float s = y(a)+y(b);

// Adding middle terms in above formula

for (int i = 1; i < n; i++)

s += 2*y(a+i*h);

// h/2 indicates (b-a)/2n. Multiplying h/2 with s


return (h/2)*s;

// Driver program to test above function

int main()

// Range of definite integral

float x0,xn;

cout<<"\nEnter range";

cin>>x0>>xn;

int n;

cout<<"\nEnter number of grids";

cin>>n;

cout<<"\nValue of integral is "<<trapezoidal(x0, xn, n);

return 0;

}
OUTPUT:
6) To implement numerical integration using Simpson 1/3 rule.

#include<iostream>

using namespace std;

float f(float x)

return(1/(1+x));

int main()

int i,n;

float x0,xn,h,y[20],so,se,ans,x[20];

cout<<"\n Enter values of x0,xn,h:";

cin>>x0>>xn>>h;

n=(xn-x0)/h;

if(n%2==1)

n=n+1;

h=(xn-x0)/n;

cout<<"\n Refined value of n and h are: "<<n<<" "<<h;

cout<<"\n\n Y values: \n";

for(i=0; i<=n; i++)

x[i]=x0+i*h;
y[i]=f(x[i]);

cout<<" "<<y[i]<<"\n";

so=0;

se=0;

for(i=1; i<n; i++)

if(i%2==1)

so=so+y[i];

else

se=se+y[i];

ans=h/3*(y[0]+y[n]+4*so+2*se);

cout<<"\n Final integration is "<<ans;

}
OUTPUT:
7) To implement numerical integration using Simpson 3/8 rule.

#include<iostream>

#include<math.h>

using namespace std;

double f(double x){

return x*x;

int main(){

int n,i;

double a,b,h,x,sum=0,integral;

cout<<"\nEnter the no. of sub-intervals(MULTIPLE OF 3): ";

cin>>n;

cout<<"\nEnter the initial limit: ";

cin>>a;

cout<<"\nEnter the final limit: ";

cin>>b;

h=fabs(b-a)/n;

for(i=1;i<n;i++){

x=a+i*h;

if(i%3==0){

sum=sum+2*f(x);

else{
sum=sum+3*f(x);

integral=(3*h/8)*(f(a)+f(b)+sum);

cout<<"\nThe integral is: "<<integral;

OUTPUT:
8) To solve the system of linear equations using Gauss-elimination method.

#include<bits/stdc++.h>

using namespace std;

#define N 3 // Number of unknowns

//Returns a value to indicate whether matrix is singular or not

int forwardElim(double mat[N][N+1]);

//Function to calculate the values of the unknowns

void backSub(double mat[N][N+1]);

//Function to get matrix content

void gaussianElimination(double mat[N][N+1])

/* reduction into r.e.f. */

int singular_flag = forwardElim(mat);

/* if matrix is singular */

if (singular_flag != -1)

cout<<"Singular Matrix\n";

/* if the RHS of equation corresponding to zero row is 0,

system has infinitely many solutions, else inconsistent*/

if (mat[singular_flag][N])

cout<<"Inconsistent System";
else

cout<<"May have infinitely many solutions";

return;

// get solution to system and print it using backward substitution

backSub(mat);

//Function for elementary operation of swapping two rows

void swap_row(double mat[N][N+1], int i, int j)

for (int k=0; k<=N; k++)

double temp = mat[i][k];

mat[i][k] = mat[j][k];

mat[j][k] = temp;

//Function to reduce matrix to r.e.f.

int forwardElim(double mat[N][N+1])

for (int k=0; k<N; k++)

// Initialize maximum value and index for pivot


int i_max = k;

int v_max = mat[i_max][k];

// find greater amplitude for pivot if any

for (int i = k+1; i < N; i++)

if (abs(mat[i][k]) > v_max)

v_max = mat[i][k], i_max = i;

/* if a prinicipal diagonal element is zero, it denotes that matrix is singular, and

will lead to a division-by-zero later. */

if (!mat[k][i_max])

return k; // Matrix is singular

/* Swap the greatest value row with current row */

if (i_max != k)

swap_row(mat, k, i_max);

for (int i=k+1; i<N; i++)

/* factor f to set current row kth elemnt to 0,

* and subsequently remaining kth column to 0 */

double f = mat[i][k]/mat[k][k];

/* subtract fth multiple of corresponding kth row element*/

for (int j=k+1; j<=N; j++)

mat[i][j] -= mat[k][j]*f;
/* filling lower triangular matrix with zeros*/

mat[i][k] = 0;

return -1;

//Function to calculate the values of the unknowns

void backSub(double mat[N][N+1])

double x[N]; // An array to store solution

/* Start calculating from last equation up to the first */

for (int i = N-1; i >= 0; i--)

/* start with the RHS of the equation */

x[i] = mat[i][N];

/* Initialize j to i+1 since matrix is upper triangular*/

for (int j=i+1; j<N; j++)

/* subtract all the lhs values except the coefficient of the variable

whose value is being calculated */

x[i] -= mat[i][j]*x[j];

}
/* divide the RHS by the coefficient of the unknown being calculated */

x[i] = x[i]/mat[i][i];

cout<<"\nSolution for the system:\n";

for (int i=0; i<N; i++)

cout<<"\n"<<x[i];

// Driver program

int main()

double mat[N][N+1];

cout<<"Enter the augmented matrix\n";

for(int i=0;i<N;i++)

for(int j=0;j<N+1;j++)

cin>>mat[i][j];

gaussianElimination(mat);

return 0;

}
OUTPUT:
9) To implement Lagranges interpolation formula.

#include<bits/stdc++.h>

using namespace std;

// To represent a data point corresponding to x and y = f(x)

struct Data

double x, y;

};

// function to interpolate the given data points using Lagrange's formula

// xi corresponds to the new data point whose value is to be obtained

// n represents the number of known data points

double interpolate(Data f[], int xi, int n)

double result = 0; // Initialize result

for (int i=0; i<n; i++)

// Compute individual terms of above formula

double term = f[i].y;

for (int j=0;j<n;j++)

if (j!=i)

term = term*(xi - f[j].x)/double(f[i].x - f[j].x);


}

// Add current term to result

result += term;

return result;

// driver function to check the program

int main()

// creating an array of 4 known data points

Data f[] = {{9.3,11.40}, {9.6,12.80}, {10.2,14.70}, {10.4,17.00}, {10.8,19.80}};

int x;

cout<<"\nEnter value of x ";

cin>>x;

cout << "Value of f(x) is : " << interpolate(f, x, 5)<<"\n";

return 0;

}
OUTPUT:
10) To find numerical solution of ordinary differential equations by Eulers method.

#include<iostream>

#include <math.h>

#define F(x,y) (x)*(y)

#include<iomanip>

using namespace std;

int main()

double y1,y2,x1,a,n,h;

int j;

cout<<"\nEnter the value of range: ";

cin>>a>>n;

cout<<"\nEnter the value of y1: ";

cin>>y1;

cout<<"\n\nEnter the h: ";

cin>>h;

cout<<"\n\n y1 = "<<y1;

for(x1=a,j=2; x1<=n+h; x1=x1+h,j++)

y2= y1 + h * F(x1,y1);

cout<<"\n\n x"<<j<<" = "<<fixed<<setprecision(3)<<x1<<" =>" <<" y"<<j<<" =


"<<fixed<<setprecision(3)<<y2;

y1=y2;

}
OUTPUT:
11) To find numerical solution of ordinary differential equations by Runge Kutta
method.

#include<iostream>

using namespace std;

// A sample differential equation "dy/dx = (x - y)/2"

float dydx(float x, float y)

return((x - y)/2);

// Finds value of y for a given x using step size h

// and initial value y0 at x0.

float rungeKutta(float x0, float y0, float x, float h)

// Count number of iterations using step size or

// step height h

int n = (int)((x - x0) / h);

float k1, k2, k3, k4, k5;

// Iterate for number of iterations

float y = y0;

for (int i=1; i<=n; i++)

// Apply Runge Kutta Formulas to find


// next value of y

k1 = h*dydx(x0, y);

k2 = h*dydx(x0 + 0.5*h, y + 0.5*k1);

k3 = h*dydx(x0 + 0.5*h, y + 0.5*k2);

k4 = h*dydx(x0 + h, y + k3);

// Update next value of y

y = y + (1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4);;

// Update next value of x

x0 = x0 + h;

return y;

// Driver method

int main()

float x0,y,x,h;

cout<<"\nEnter values of x0 , y , x and h ";

cin>>x0>>y>>x>>h;

cout<<"\nThe value of "<<y<<" at "<<x<<" is : "<<rungeKutta(x0, y, x, h);

return 0;

}
OUTPUT: