Showing posts with label Number Theorem. Show all posts
Showing posts with label Number Theorem. Show all posts

Friday, May 31, 2013

Newton-Raphson Method (C code for finding a real root of an equation)

This article is about Newton's method for finding roots.

In numerical analysis, Newton's method (also known as the Newton–Raphson method), named after Isaac Newton and Joseph Raphson, is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function.
x : f(x) = 0 \,.
The Newton-Raphson method in one variable is implemented as follows:
Given a function ƒ defined over the reals x, and its derivative ƒ ', we begin with a first guess x0 for a root of the function f. Provided the function satisfies all the assumptions made in the derivation of the formula, a better approximation x1 is
x_{1} = x_0 - \frac{f(x_0)}{f'(x_0)} \,.
Geometrically, (x1, 0) is the intersection with the x-axis of a line tangent to f at (x0, f (x0)).
The process is repeated as
x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \,
until a sufficiently accurate value is reached.
This algorithm is first in the class of Householder's methods, succeeded by Halley's method. The method can also be extended to complex functions and to systems of equations.

C code for finding a real root of  the equationf(x)= x*x*x-2*x-5 using Newton-Raphson Method

#include
//#include
#include
int main(){
    //clrscr();
    int i;
    float x,fx,fdx,t;
    printf("Enter x0: ");
    scanf("%f",&x);
    //freopen("file.txt","w",stdout);
    for(;;)
    {
        t=x;
        fx=pow(x,3)-(2*x)-5;
        fdx=3*pow(x,2)-2;
        x=t-(fx/fdx);
        printf("\nthe value of x is::%f\n",x);
        if(fabs(x-t)<=0.0001)
        {
            printf("The root is: %f",x);
            break;
        }
    }
    //fclose(stdout);
    getch();
    return 0;
}

sample output:

Enter x0: 2

the value of x is::2.100000

the value of x is::2.094568

the value of x is::2.094552
The root is: 2.094552

Implementation of Iterative Method


In computational mathematics, an iterative method is a mathematical procedure that generates a sequence of improving approximate solutions for a class of problems. A specific implementation of an iterative method, including the termination criteria, is an algorithm of the iterative method. An iterative method is called convergent if the corresponding sequence converges for given initial approximations. A mathematically rigorous convergence analysis of an iterative method is usually performed; however, heuristic-based iterative methods are also common.
In the problems of finding the root of an equation (or a solution of a system of equations), an iterative method uses an initial guess to generate successive approximations to a solution. In contrast, direct methods attempt to solve the problem by a finite sequence of operations. In the absence of rounding errors, direct methods would deliver an exact solution (like solving a linear system of equations Ax = b by Gaussian elimination). Iterative methods are often the only choice for nonlinear equations. However, iterative methods are often useful even for linear problems involving a large number of variables (sometimes of the order of millions), where direct methods would be prohibitively expensive (and in some cases impossible) even with the best available computing power.

C code to find a real root of the equation 2x=cosx+3 using Iterative method:

  /***********************************************************
* You can use all the programs on  www.engineercse.blogspot.com
* for personal and learning purposes.
* contact azam.ruet10@gmail.com
* To find more C programs, do visit www.engineercse.blogspot.com
* and browse!
*
*                            Coding is poetry!!!

***********************************************************/
#include
#include
#include
#include

int main(){
    int i;
    float f,a,b,x,t;
    scanf("%f",&x);
    //x=(a+b)/2;
    if((sin(x)/2)<1 br="">        for(i=0;i<2000 br="" i="">            t=f;
            //x=0;
            //f=0;
            f=(cos(x)+3)/2;
            printf("%f\n",x);
            //t=f;

            x=f;
            if(fabs(f-t)<=0.0001)
                break;
            //t=f;
        }
        printf("\nThe Root is: %f",x);
    }
    else
        printf("\nThe Method cannot be applied..");

    getch();
}


sample output: 90

90.000000
1.275963
1.645290
1.462788
1.553900
1.508448
1.531154
1.519816
1.525479
1.522651
1.524063
1.523358
1.523710
1.523534

The Root is: 1.523622

Thursday, September 13, 2012

A program that shows GCD & LCM of two numbers

#include


void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;


printf("Enter two Integer numbers\n");
scanf("%d %d", &num1,&num2);

if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = num1 % num2;
while(remainder !=0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d \n", num1,num2,gcd);
printf("LCM of %d and %d = %d \n", num1,num2,lcm);
} /* End of main() */