Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

PRACTICAL FILE- Numerical Computing (MCA407)

Submitted in Partial fulfilment of the requirement for the award of the degree

MASTERS OF COMPUTER APPLICATION

Submitted to: Ms. Megha (Asst. Prof. – Dept. of Computer


Science)
Submitted by: Aditya Agrawal - 1894094
Satyam Shivankur – 1894123

DEPARTMENT OF COMPUTER SCIENCE, DELHI UNIVERSITY


(BATCH 2018 – 2021)

INDEX
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
S.No Practical Date Page Teache
No. rSign
1. Bisection Method

2. Lagrange's Interpolation.

3. Euler Method

4. Regula Falsi Method.

5. Newton Raphson Method for solving


equations
6. Secant method.

7. Runge Kutta method for 2nd order


Differential Equations.
8. Runge Kutta method for 4th order
Differential Equations.
9. Taylor Method.

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
1. Bisection Method.
class Bisection{
static final float EPSILON = (float)0.01;

// The function is x^3 - x^2 + 2


static double func(double x) {
double value=x*x*x - x*x + 2;
value=(double)Math.round(value*100000)/100000;
return value;
}

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


static void bisection(double a, double b) {
a=(double)Math.round(a*100000)/100000;
b=(double)Math.round(b*100000)/100000;
if (func(a) * func(b) >= 0) {
System.out.println("You have not assumed right values for a and
b");
return;
}
int k=-1;
double c = a;
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s",
"k","x_k","x_k+1","x_m","f(x_m)","sign"));
System.out.println();
while ((b-a) >= EPSILON){
k++;
// Find middle point
c = (a+b)/2;
c=(double)Math.round(c*100000)/100000;

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

String sign=(func(c)>=0)?"+ve":"-ve";
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s",
k,a,b,c,func(c),sign));
}
String sign=(func(c)>=0)?"+ve":"-ve";
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s",
k+1,a,b,c,func(c),sign));
//prints value of c upto 4 decimal places
System.out.printf("\nThe value of root is : %.4f",c);
System.out.println();
}

// Driver program to test above function


public static void main(String[] args){
// Initial values assumed
double a =-200, b = 300;
bisection(a, b);
}

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
}
OUTPUT:

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
2. Lagrange's Interpolation.
class LagrangeInter {

static class Data {


int x, y;

public Data(int x, int y)


{
super();
this.x = x;
this.y = 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
static 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) / (f[i].x - f[j].x);
}

// Add current term to result


result += term;
}
return result;
}

// Driver code
public static void main(String[] args) {
// creating an array of 4 known data points
Data f[] = {new Data(0, 2), new Data(1, 3),new Data(2, 12), new Data(5, 147)};

// Using the interpolate function to obtain


// a data point corresponding to x=3
System.out.print("Value of f(3) is : " + (int)interpolate(f, 3, 4));
}
}

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
OUTPUT:

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
3. Euler Method.
class Euler{
// Consider a differential equation
// dy/dx=(x + y + xy)
float func(float x, float y)
{
float value = x + y + x * y;
value=(float)Math.round(value*100000)/100000;
return value;
}

// Function for Euler formula


void euler(float x0, float y, float h, float x)
{
int n=0;

// Iterating till the point at which we


// need approximation
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s",
"n","x_n","y_n","y'_n","y_n+1"));
while (x0 < x) {
float temp1 = y;
y = y + h * func(x0, y);
y=(float)Math.round(y*10000)/10000;

float temp2 = x0;


x0 = x0 + h;
x0=(float)Math.round(x0*10000)/10000;

System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s",


n,temp2,temp1,func(temp2, temp1),y));
}

// Printing approximation
System.out.println("\nApproximate solution at x = "+ x + " is " + y);
}

// Driver program
public static void main(String args[]) {
Euler obj = new Euler();
// Initial Values
float x0 = 0;
float y0 = 1;
float h = 0.025f;

// Value of x at which we need approximation


float x = 0.1f;

obj.euler(x0, y0, h, x);


}
}

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
OUTPUT:

4. Regula Falsi Method.

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
class RegFalsi{

static int MAX_ITER = 10;

// The function is x^3 - x^2 + 2


static double func(double x) {
double value=x*x*x - x*x + 2;
value=(double)Math.round(value*100000)/100000;
return value;
}

// Prints root of func(x)


// in interval [a, b]
static void regulaFalsi(double a, double b) {
a=(double)Math.round(a*100000)/100000;
b=(double)Math.round(b*100000)/100000;
if (func(a) * func(b) >= 0){
System.out.println("You have not assumed right a and b");
return;
}

// Initialize result
int k=0;
double c = a;
System.out.println(String.format("%-5s %-25s %-15s %-15s %-15s %-25s %-15s %-
30s", "k","x_k","x_k+1","f(x_k)","f(x_k+1)","x_k+2","f(x_k+2)","sign"));
System.out.println();
for (int i = 0; i < MAX_ITER; i++){
// Find the point that touches x axis
c = (a * func(b) - b * func(a)) / (func(b) - func(a));
c=(double)Math.round(c*100000)/100000;

// Check if the above found 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;

k++;
String sign=(func(c)>=0)?"+ve":"-ve";
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-15s
%-15s", i,a,b,func(a),func(b),c,func(c),sign));
}
String sign=(func(c)>=0)?"+ve":"-ve";
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-15s %-
15s", k+1,a,b,func(a),func(b),c,func(c),sign));
System.out.printf("\nThe value of root is : %.4f\n",c);
}

// Driver program
public static void main(String[] args) {
// Initial values assumed
double a = -200, b = 300;
regulaFalsi(a, b);
}
}
OUTPUT:

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
5. Newton Raphson Method for solving equations.
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
class NewtRaph{

static final double EPSILON = 0.001;

// The function is x^3 - x^2 + 2


static double func(double x) {
double value=x*x*x - x*x + 2;
value=(double)Math.round(value*100000)/100000;
return value;
}

// Derivative of the above function


// which is 3*x^x - 2*x
static double derivFunc(double x) {
double value=3*x*x - 2*x;
value=(double)Math.round(value*100000)/100000;
return value;
}

// Function to find the root


static void newtonRaphson(double x) {
x=(double)Math.round(x*100000)/100000;
double x1=0;
int k=0;
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s",
"k","x_k","f(x_k)","f'(x_k)","x_k+1"));
double h = func(x) / derivFunc(x);
while (Math.abs(h) >= EPSILON) {
h = func(x) / derivFunc(x);

// x(i+1) = x(i) - f(x) / f'(x)


x1 = x - h;
x1=(double)Math.round(x1*100000)/100000;
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s",
k,x,func(x),derivFunc(x),x1));
x=x1;
k++;
}
System.out.print("\nThe value of the root is : "+ x1+"\n");
}

// Driver code
public static void main (String[] args) {
// Initial values assumed
double x0 = -20;
newtonRaphson(x0);
}
}

OUTPUT:
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
6. Secant method.
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
class Secant{

static float func(float x){

// The function is x^3+x-1


float f = (float)Math.pow(x, 3) + x - 1;
f=(float)Math.round(f*100000)/100000;
return f;
}

static void secant(float x0, float x1, float E) {


x0=(float)Math.round(x0*100000)/100000;
x1=(float)Math.round(x1*100000)/100000;

System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-15s %-


15s", "k","x_k","x_k+1","f(x_k)","f(x_k+1)","x_k+2","f(x_k+2)","sign"));
System.out.println();

float n = 0, xm, x2=0, c;


if (func(x0) * func(x1) < 0)
{
do {

// calculate the intermediate value


x2 = (x0 * func(x1) - x1 * func(x0))/(func(x1) - func(x0));
x2=(float)Math.round(x2*100000)/100000;
String sign=(func(x2)>=0)?"+ve":"-ve";
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-
15s %-15s", (int)n,x0,x1,func(x0),func(x1),x2,func(x2),sign));

// check if x0 is root of
// equation or not
c = func(x1) * func(x0);

// update the value of interval


x0 = x1;
x1 = x2;

// update number of iteration


n++;

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


if (c == 0)
break;
xm = (x0 * func(x1) - x1 * func(x0)) / (func(x1) - func(x0));

// repeat the loop until the convergence


} while (Math.abs(xm - x2) >= E);
System.out.println("\nRoot of the given equation=" + x2);
System.out.println("No. of iterations = " + n);
}
else
System.out.print("Cannot find a root in the given inteval");
}
// Driver code
public static void main(String[] args) {
// initializing the values
float x1 = 0, x2 = 1, E = 0.0001f;
secant(x1, x2, E);
}
}

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
OUTPUT:

7. Runge Kutta method for 2nd order Differential Equations.


Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
class RK2nd {

// A sample differential equation


// "dy/dx = x + y - 2"
static double dydx(double x, double y) {
return (x + y - 2);
}

// Finds value of y for a given x


// using step size h
// and initial value y0 at x0.
static double rungeKutta(double x0, double y0, double x, double h)
{
// Count number of iterations
// using step size or
// step height h
int n = (int)((x - x0) / h);

double k1, k2, k;

// Iterate for number of iterations


double x1=0, y1 = 0;

System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-15s",


"n","x_n","y_n","k","y_n+1","h","x_n+1"));
for (int i = 1; i <= n; i++) {
// Apply Runge Kutta Formulas
// to find next value of y
k1 = h * dydx(x0, y0);
k2 = h * dydx(x0 + 0.5 * h, y0 + 0.5 * k1);
k = (k1 + 2 * k2)/6;
// Update next value of y
y1 = y0 + k;
k=(double)Math.round(k*10000)/10000;
y1=(double)Math.round(y1*10000)/10000;
// Update next value of x
x1 = x0 + h;
x1=(double)Math.round(x1*10000)/10000;
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-
15s", i,x0,y0,k,y1,h,x1));
x0=x1;
y0=y1;
}

return y1;
}

// Driver Code
public static void main (String[] args) {
double x0 = 0, y = 1, x = 2, h = 0.2;
System.out.println("\nThe value of y at x=2 is : "+ rungeKutta(x0, y, x, h));
}
}

OUTPUT:
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
8. Runge Kutta method for 4th order Differential Equations.
class RK4th{
double dydx(double x, double y) {
return ((x - y) / 2);
}

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


// and initial value y0 at x0.
double rungeKutta(double x0, double y0, double x, double h) {
RK4th d1 = new RK4th();
// Count number of iterations using step size or
// step height h
int n = (int)((x - x0) / h);

double k1, k2, k3, k4, k;

// Iterate for number of iterations


double x1=0, y1 = 0;

System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-15s",


"n","x_n","y_n","k","y_n+1","h","x_n+1"));

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


// Apply Runge Kutta Formulas to find
// next value of y
k1 = h * (d1.dydx(x0, y0));
k2 = h * (d1.dydx(x0 + 0.5 * h, y0 + 0.5 * k1));
k3 = h * (d1.dydx(x0 + 0.5 * h, y0 + 0.5 * k2));
k4 = h * (d1.dydx(x0 + h, y0 + k3));
k = (k1 + 2 * k2 + 2 * k3 + k4)/6.0;

// Update next value of y


y1 = y0 + k;
k=(double)Math.round(k*10000)/10000;
y1=(double)Math.round(y1*10000)/10000;
// Update next value of x
x1 = x0 + h;
x1=(double)Math.round(x1*10000)/10000;
System.out.println(String.format("%-5s %-15s %-15s %-15s %-15s %-15s %-
15s", i,x0,y0,k,y1,h,x1));
y0=y1;
x0=x1;
}
return y1;
}

public static void main(String args[]) {


RK4th d2 = new RK4th();
double x0 = 0, y = 1, x = 2, h = 0.2;

System.out.println("\nThe value of y at x=2 is : "+ d2.rungeKutta(x0, y,


x, h));
}
}

Aditya Agrawal 1894094 MCA- IV


Satyam Shivankur 1894123 MCA-IV
OUTPUT:

9. Taylor Method.
class
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV
Aditya Agrawal 1894094 MCA- IV
Satyam Shivankur 1894123 MCA-IV

You might also like