Saturday, April 5, 2014

Java Program to Implement Iterative Method

import java.util.Scanner;

public class IterativeProcess {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        float a, x, b, c, sum;

        float result[] = new float[100];

        System.out.println("for the quadratic equation:ax^2+bx+c");
        System.out.println("enter the value of a:?");
        a = input.nextFloat();

        System.out.println("enter the value of b:?");
        b = input.nextFloat();

        System.out.println("enter the value of c:?");
        c = input.nextFloat();

        System.out.println("enter the starting value of x:?");
        x = input.nextFloat();
        x = (-c - a * x * x) / b;
       
        result[0]=x;
        System.out.printf("The equation is: %.2fx^2 + (%.2f)x + %f = 0 \n", a, b, c);

        //   for(int i=x;i<=10;i++){ // loop starts here

        for (int i =0;; i++) { // loop starts here
         
           // System.out.printf("The value at SN:=%d is:%f \n", i+1, result[i]); 
           
            sum =  (-c - a * x * x) / b;
            result[i+1] = sum;

            if (result[i+1] == x) {
                break;
            }
             System.out.printf("The value at SN:=%d is:%f \n", i+1, result[i]); 
            x = sum;
           


        }// loop ends here
        if ((result[1] - result[2]) >(result[2] - result[3])) {
            System.out.println("\n........................... The function is Converging...........................");
        } else {
            System.out.println("\n ........................ ..The function is Diverging..........................");
        }
    }
}

Java Program to Implement N-R (NewtonRaphson) METHOD

import java.util.Scanner;
public class NewtonRaphson2 {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
       
        System.out.println("guess the initial value:");
        float x= input.nextFloat();
        float err=0;
        System.out.println("enter the no: of iteration:");  
        int time= input.nextInt();
        System.out.println("\n Iteration \t\t X \t\t\t Error(%)");
       
        for(int i =1;i<=time;i++)
        {
            float temp =x;
            x= x-(((float)Math.exp(-x))-x)/((-(float)Math.exp(-x))-1);
            err =Math.abs(((x-temp)/x)*100);
                   
            if(err<0.0001)
            {
                System.out.println(i-1 + "\t\t" + x + "\t\t" +err+ "%" /*+ "\n This is done....."*/);
                break;
            }
           
            else if (i<=2)
                System.out.println(i-1 + "\t\t" + x + "\t\t" + err +"%");
            else
                System.out.println(i + "\t\t" + x + "\t\t" + err+ "%");
       
        if(err<0.0001)
        {
            System.out.println(i + "\t\t" + x + "\t\t" + err +"%");
        }
        else
             System.out.println(i-1 + "\t\t" + x + "\t\t" + err +"%");
       
    }
}
}

C Program to Implement N-R (NewtonRaphson) METHOD

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int user_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;

void main()
{

    clrscr();

    printf("\n\n\t\t\t PROGRAM FOR NEWTON RAPHSON GENERAL");

    printf("\n\n\n\tENTER THE TOTAL NO. OF POWER:::: ");
    scanf("%d",&user_power);

    for(i=0;i<=user_power;i++)
    {
        printf("\n\t x^%d::",i);
        scanf("%d",&coef[i]);
    }

    printf("\n");

    printf("\n\t THE POLYNOMIAL IS ::: ");
    for(i=user_power;i>=0;i--)//printing coeff.
    {
        printf(" %dx^%d",coef[i],i);
    }

    printf("\n\tINTIAL X1---->");
    scanf("%f",&x1);

    printf("\n ******************************************************");
    printf("\n ITERATION    X1    FX1    F'X1  ");
    printf("\n **********************************************************");

    do
    {
            cnt++;
            fx1=fdx1=0;
            for(i=user_power;i>=1;i--)
            {
                fx1+=coef[i] * (pow(x1,i)) ;
            }
            fx1+=coef[0];
            for(i=user_power;i>=0;i--)
            {
                fdx1+=coef[i]* (i*pow(x1,(i-1)));
            }
            t=x2;
            x2=(x1-(fx1/fdx1));

            x1=x2;

            printf("\n %d         %.3f  %.3f  %.3f ",cnt,x2,fx1,fdx1);

    }while((fabs(t - x1))>=0.0001);
    printf("\n\t THE ROOT OF EQUATION IS %f",x2);
    getch();
}





















/*
This program have written for the course named Numerical Method
        Author : Fazle Rabbi
        My social site's link
______________________________________________________________
https://www.facebook.com/FazleRabbi.CSE
https://twitter.com/Fazle_Rabbi_CSE
http://lnkd.in/bzuUfbM
https://plus.google.com/u/0/106347450745435143158/posts
http://www.youtube.com/channel/UC4Ni4G1TIydmdXNFxeqskvQ
http://www.flickr.com/photos/108286097@N08/
https://vimeo.com/user22555382
http://www.pinterest.com/fazlerabbicse/
https://foursquare.com/user/71317652
            Website:
______________________________________________________________
www.fazlerabbicse.blogspot.com
www.awesome-fazle-rabbi.tumblr.com

            Email me
______________________________________________________________
fazlerabbicse@gmail.com
fazlerabbi@ojooo.com
*/