/**************************************/

/*      Numerical Analysis Homework #1       */

/*      Department of Computer Science       */

/*      2003721181, Lee Seong woo              */

/**************************************/

#include <stdio.h>

#include <math.h>


double logB(double x, double base);

double func(double p);

int getIterations(double tolerance, double startPoint, double endPoint);


int main(void)

{

        double a = 1.0, b = 2.0, tolerance = 0.0005;

        int i, repeat;

       

        // 주어진 Tolerance를 이용해서 필요한 반복횟수를 구함

        repeat = getIterations(tolerance, a, b);

       

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

        printf("n            an              bn              pn             f(pn)n");

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

        for(i = 0; i < repeat; i++)

        {

                if(func(a) * func(a + ((b - a) / 2)) < 0)

                {

                        printf("%dt%.10ft%.10ft%.10ft%.10fn",

                              i + 1, a, b, a + ((b - a) / 2), func(a + ((b - a) / 2)));

                        b = a + ((b - a) / 2);

                }

                else

                {

                        printf("%dt%.10ft%.10ft%.10ft%.10fn",

                              i + 1, a, b, a + ((b - a) / 2), func(a + ((b - a) / 2)));

                        a = a + ((b - a) / 2);

                }

        }

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


        return 0;

}


// 밑을 임의로 정할 수있는 로그함수

double logB(double x, double base)

{

        return log(x) / log(base);

}


// 해를 구하기 위한 함수

double func(double p)

{

        return pow(p, 3) + 4 * pow(p, 2) - 10;

}


// Tolerance에 따른 반복횟수를 구함

int getIterations(double tolerance, double startPoint, double endPoint)

{

        return ceil(logB((endPoint - startPoint) / tolerance, 2));

}

+ Recent posts