c·c++/c 프로그래밍

Newton-Raphson Method

바로이순간 2012. 10. 13. 01:35

#include <stdio.h> 

#include <stdlib.h>

#include <conio.h> 

#include <math.h> 

#define F(x)(x*x-x-3.0) 

#define FD(x)(2*x-1.0) 

#define MAXIT 20 

int main() { 

    int count; 

    double x0,x1,fx,fdx; 

    system("cls"); 

    printf("NEWTON-RAPHSON METHOD\n"); 

    printf("---------------------\n"); 

    printf("initial value:"); 

    scanf("%lf",&x0); 

    count=1; 

    while(count<MAXIT) {

        fx=F(x0); 

        fdx=FD(x0); 

        x1=(x0-(fx/fdx)); 

        if(fabs((x1-x0)/x1)<0.000000001) { 

            printf("The root is:%.14f\n",x1); 

            printf("Iteration is:%d\n",count);

            break; 

        } 

        else { 

            x0=x1; 

        } 

        count=count+1; 

    }

    return 0;

}

'c·c++ > c 프로그래밍' 카테고리의 다른 글

피보나치-어린토끼,어른토끼  (0) 2012.10.20
간단한 계산기  (0) 2012.10.19
진법변환  (0) 2012.10.11
달력출력  (0) 2012.10.10
원형큐  (0) 2012.10.08