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

/*      Numerical Analysis Homework #2     */

/*      Department of Computer Science     */

/*      2003721181, Lee Seong woo          */

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

#include <stdio.h>

#include <math.h>


#define CAPACITY 10


double func(double p);


int main(void)

{

        double p[CAPACITY];

        int i = 1;

        double tolerance = 0.0005;   // Tolerance - 오차 한계 값

        p[0] = 1.0; p[1] = 2.0;


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

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

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


        // p[i] 값과값 차이의 절대값이 Tolerance보다 작을 때 프로그램 종료

        while(fabs(p[i] - p[i - 1]) >= tolerance)

        {

                i++;

                p[i] = p[i-1] - (func(p[i-1]) * (p[i-1] - p[i-2])) / (func(p[i-1]) - func(p[i-2]));

                printf("%dt%.10ft%.10fn", i, p[i], func(p[i]));

        }

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


        return 0;

}


// 해를 구하기 위한 함수

double func(double p)

{

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

}


----
2007/09/20 - [놀이] - [과제] Numerical Analysis - C로 구현한 Bisection Method

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

/*      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