Sie sind auf Seite 1von 34

# INDEX

## 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;

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<<"\nPercentage Relative Error : "<<p_rel_err;

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

#include<bits/stdc++.h>

## #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;

return;

double c = a;

## // Find middle point

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

if (func(c) == 0.0)

break;

b = c;

else

a = c;

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>

float f(float x)

return f;

do {

## // check if x0 is root of equation or not

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

x1 = x2;
x2 = x0;

n++;

if (c == 0)

break;

} else

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

// Driver code

int main()

float x1 , x2 , E = 0.0001;

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>

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

double func(double x)

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

double derivFunc(double x)

## // Function to find the root

void newtonRaphson(double x)

## while (abs(h) >= EPSILON)

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

x = x - h;

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>

float y(float x)

## 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);

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

return (h/2)*s;

int main()

## // Range of definite integral

float x0,xn;

cout<<"\nEnter range";

cin>>x0>>xn;

int n;

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];

cin>>x0>>xn>>h;

n=(xn-x0)/h;

if(n%2==1)

n=n+1;

h=(xn-x0)/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>

## double f(double x){

return x*x;

int main(){

int n,i;

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

cin>>n;

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>

## int singular_flag = forwardElim(mat);

/* if matrix is singular */

if (singular_flag != -1)

cout<<"Singular Matrix\n";

## system has infinitely many solutions, else inconsistent*/

if (mat[singular_flag][N])

cout<<"Inconsistent System";
else

return;

backSub(mat);

## double temp = mat[i][k];

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

mat[j][k] = temp;

int i_max = k;

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

if (!mat[k][i_max])

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

if (i_max != k)

swap_row(mat, k, i_max);

## * and subsequently remaining kth column to 0 */

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

## 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;

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

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

## 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];

## 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>

struct Data

double x, y;

};

if (j!=i)

}

result += term;

return result;

int main()

int 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;

cin>>a>>n;

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>

## float dydx(float x, float y)

return((x - y)/2);

// step height h

float y = y0;

## // Apply Runge Kutta Formulas to find

// next value of y

k1 = h*dydx(x0, y);

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

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

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;

return 0;

}
OUTPUT: