Tuesday, December 2, 2014

Program mencari akar-akar persamaan suku banyak (metoda secant)


1.Persamaan yang dicari 5x^4 - 20x^3 + 12x^2 - 3 = 0


2.Code program:
         
#include<stdio.h>
#include<math.h>

double power(double, int);
double value(double);
double calculate(double, double);

main()
{
    double x1, x2, e;
    int it;
    printf ("Tebakan pertama     : ");
    scanf ("%lf", &x1);
    printf ("Tebakan kedua       : ");
    scanf ("%lf", &x2);
    printf ("Masukan eror        : ");
    scanf ("%lf", &e);
    printf ("Berapa kali iterasi : ");
    scanf ("%i", &it);
    printf ("%lf",e);

    int i=1;
    double xt, fxt;

    printf("\n");
    printf ("Iterasi ke-    xl      xb    xt        |f(xt)|  \n");
    for (i=1;i<it;i++)
    {
        xt = calculate(x1, x2);
        printf ("  %3i     %8.4lf%8.4lf %8.4lf %11.6lf \n", i, x1, x2, xt, cabs(value(xt)));
        fxt = cabs(value(xt));

        x1=x2;
        x2=xt;

    }

}

double power(double number, int power)
{
    double result = 1.00;

    if (power == 0.0)
    {
        return result;
    }

    int i;

    for (i = 1; i <= power; i++)
    {
        result *=number;
    }

    return result;
}

double value(double x)
{
    return ((5*(power(x,4))) - (20*(power(x,3))) + 12*(power(x,2))- 3);
}

double calculate(double xl, double xb)
{
    return (xb - ((xb-xl)/(value(xb)-value(xl)))*value(xb));
}


3.Hasil iterasi:




4.Hasil akhir:
   iterasi ke-12 -0.3834 seperti pada gambar diatas.




Nama  :   Doni Harianto
Nim    :  D1042131028





























No comments:

Post a Comment