Regula-Falsey Algorithm?

I am trying to implement the Regula-Falci algorithm to solve the equation, 2(x^3)-x-2 but the problem is that the value of the variable cremains constant and does not change, even if my code needs to change it.

#include<math.h>
#include<stdio.h>


float fonc(float x)
{
    int result;
    result=2*(pow(x,3))-x-2;
    return result;
}

int main(void)
{
    float eps=pow(10,-4);
    int i=0;
    float a,b,c;
    a=1;
    b=2;
    do
    {
        c=((a*fonc(b))-(b*fonc(a)))/((fonc(b)-fonc(a)));
        if(fonc(c)*fonc(a)<0)
        {
            b=c;
        }
        else
        {
            a=c;
        }
        i++;    
        printf("\n%f",c);
    }
    while(fabs(b-c)>eps);

    printf("le nombre d'itération %d",i);
}
+4
source share
3 answers

Added preface: . What could go wrong with the algorithm as presented, even if all data types were suitable?

Unlike the bisection method, pure regulatory falsification will not force the interval length to be zero. If a monotone and convex function is used, then the iteration stops when only one side of the interval with geometric convergence changes.

, c . , c , , .

, . . - .

c = |f(b)|/(|f(a)|+|f(b)|) * a + |f(a)|/(|f(a)|+|f(b)|) * b

- f (a) f (b) . b , f (b), .. .


Illulais regula falsi ( ). 2.2e-6 6e-7 6 .

#include<math.h>
#include<stdio.h>


float fonc(float x)
{
   return (2*x*x-1)*x-2;
}

int main(void)
{
    float eps=1e-6;
    int i=0;
    float a=1, fa = fonc(a);
    float  b=2, fb = fonc(b);

    printf("\na=%10.7f b=%10.7f  fa=%10.7f  fb=%10.7f\n------\n",a,b, fa,fb);

    if(signbit(fb)==signbit(fa)) {
        printf("Attention, les valeurs initiales de 'fonc' n'ont pas de signe opposeés!\n");

    }
    do
    {
        float c=(a*fb-b*fa)/(fb-fa), fc = fonc(c);

        if( signbit(fc)!=signbit(fa) )
        {
            b=a; fb=fa;
            a=c; fa=fc;
        }
        else
        {
            a=c; fa=fc;
            fb *= 0.5;
        }
        i++;  
        printf("\na=c=%10.7f b=%10.7f  fa=fc=%10.7f  fb=%10.7f",c,b, fc,fb);

        if(fabs(fc)<eps) break;
    }
    while(fabs(b-a)>eps);

    printf("\nle nombre d'itération %d\n",i);

    return 0;
}

 a= 1.0000000 b= 2.0000000  fa=-1.0000000  fb=12.0000000
 ------

 a=c= 1.0769231 b= 2.0000000  fa=fc=-0.5789710  fb= 6.0000000
 a=c= 1.1581569 b= 2.0000000  fa=fc=-0.0512219  fb= 3.0000000
 a=c= 1.1722891 b= 1.1581569  fa=fc= 0.0497752  fb=-0.0512219
 a=c= 1.1653242 b= 1.1722891  fa=fc=-0.0003491  fb= 0.0497752
 a=c= 1.1653727 b= 1.1722891  fa=fc=-0.0000022  fb= 0.0248876
 a=c= 1.1653733 b= 1.1653727  fa=fc= 0.0000020  fb=-0.0000022
 le nombre d'itération 6
+2

, , , , :

result int float . :

    while ( fabs( b - c ) > eps );

, , b c 0.0001, , , , , .

b c . , fonc( c ) = 2*c*c*c - c - 2 eps. , , c . :

    while ( fabs( fonc( c ) ) > eps );

- , . , int --> float , 14 .

+3

, result - int. float double, fonc() .

+2

Source: https://habr.com/ru/post/1530768/


All Articles