Circular problem. Incorrect cycle result

CSDN Q & A 2022-01-15 01:49:15
The phenomenon and background of the problem

Incorrect cycle result , The last output a,b,x incorrect

#include "stdio.h"

#include "math.h"

double f(double x)

{
return (2xx-3);// function
}

void main()

{
double a,b,e,z1,z2,x,x1,x2;

a=-2;
b=3;

x1=b-0.618*(b-a);
x2=a+0.618*(b-a);

printf("x1=%f\n",x1);
printf("x2=%f\n",x2);
e=0.001;// Convergence accuracy

z1=f(x1);
z2=f(x2);
do
{
if (z1>=z2)
{
a=x1 ;
printf("a=%f\n",a);
x1=x2;
z1=z2;

x2=a+0.618*(b-a);z2=f(x2);}else{ b=x2;x2=x1;z2=z1;x1=b-0.618*(b-a);z1=f(x1);}

}

while (fabs(((b - a) / b)) >= e && fabs(((z2 - z1) / z2)) >= e);
x=(a+b)/2;
printf("a=%d\n",a);
printf("b=%d\n",b);

printf("x=%d\n",x);

}

Operation results and error reporting contents

x1=-0.090000
x2=1.090000
a=-0.819620
a=-0.368805
a=1694732160
b=518353760
x=-1010596864
Press any key to continue

My solution ideas and tried methods

I don't know what's wrong , I've tried many methods that are wrong

What I want to achieve

Can get the right x Value


thank
Similar articles

2022-01-15

2022-01-15

2022-01-15