Wednesday, 17 December 2014

Pin It

Widgets

Find root using Halley's Method


#include<stdio.h>

#define F(x) (x*x - x -6)
#define F_PRIME(x) (2*x - 1)
#define F_DOUBLE_PRIME(x) (2)
#define MAX_ITER 100

float Halley(float x0)
{
    int iter = 0;
    float x1;
    
    while(iter < MAX_ITER)
    {
        x1 = x0 - (2 * F(x0) * F_PRIME(x0)) / (2 * F_PRIME(x0) * F_PRIME(x0) - F(x0) * F_DOUBLE_PRIME(x0));
        x0 = x1;
        iter++;
    }
    return x0;
}

int main()
{
    printf("Finding root of a continuous function using Newton's method.\n");
    
    float x0 = 1;
    printf("Root of the given function is : %f\n", Halley(x0));
    return 0;
}

No comments: