A
Amy Lee
Hello,
I major maths in mu university, and I wanna build an app to do
Steffensen's Acceleration Method. Here's my code:
/* Equation: x^3-x-1=0 */
#include <stdio.h>
#include <math.h>
int main(void)
{
float x_starter, x, y, z;
float eps1;
float eps2;
printf("Please enter x_starter: ");
scanf("%f", &x_starter);
printf("Please enter eps1 and eps2: ");
scanf("%f,%f", &eps1, &eps2);
if (fabs(x_starter-(powf(x_starter,3.0)-1)) < eps1)
{
printf("Error!\n");
}
while (fabs(x_starter-(powf(x_starter,3.0)-1)) > eps1)
{
y=powf(x_starter,3.0)-1;
z=powf(y,3.0)-1;
if (fabs(z-2*y+x_starter) < eps2)
{
x=x_starter-powf((y-x_starter),2.0)/(z-2*y+x_starter);
x_starter=x;
}
else
{
break;
}
}
printf("%f\n", &x);
return (0);
}
However, when I type 1.5 to have a test, eps1 and eps2 are 0.01, 0.001,
the answer is -1.9xxxxx. And when I change the input number, it still
shows -1.9xxxxx.
Where's the problem?
Thank you very much~
Amy Lee
I major maths in mu university, and I wanna build an app to do
Steffensen's Acceleration Method. Here's my code:
/* Equation: x^3-x-1=0 */
#include <stdio.h>
#include <math.h>
int main(void)
{
float x_starter, x, y, z;
float eps1;
float eps2;
printf("Please enter x_starter: ");
scanf("%f", &x_starter);
printf("Please enter eps1 and eps2: ");
scanf("%f,%f", &eps1, &eps2);
if (fabs(x_starter-(powf(x_starter,3.0)-1)) < eps1)
{
printf("Error!\n");
}
while (fabs(x_starter-(powf(x_starter,3.0)-1)) > eps1)
{
y=powf(x_starter,3.0)-1;
z=powf(y,3.0)-1;
if (fabs(z-2*y+x_starter) < eps2)
{
x=x_starter-powf((y-x_starter),2.0)/(z-2*y+x_starter);
x_starter=x;
}
else
{
break;
}
}
printf("%f\n", &x);
return (0);
}
However, when I type 1.5 to have a test, eps1 and eps2 are 0.01, 0.001,
the answer is -1.9xxxxx. And when I change the input number, it still
shows -1.9xxxxx.
Where's the problem?
Thank you very much~
Amy Lee