Help: About Steffensen's Acceleration Method

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
 
E

Eric Sosman

Amy Lee wrote On 10/05/07 11:00,:
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?

One problem is the final printf call: You want to
display the value of x but you're displaying its address
instead. (Actually, there's really no telling what can
happen here: The "%f" must match a `double' argument,
but you're providing a `float*', so all bets are off.)
Get rid of the `&'.

Other issues: When you call scanf() you should test
the value it returns, to see whether it succeeded or
not. I'm particularly suspicious of the second one,
because if you don't provide a comma when you enter the
two numbers the scanf() will never get as far as trying
to read the second one. At the very least, print the
values of the variables you read, just to make sure they
were read as you intended.
 
A

Army1987

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: ");
Note that this isn't guaranteed to appear until you print a
newline character or call fflush(stdout).
scanf("%f", &x_starter);
printf("Please enter eps1 and eps2: ");
scanf("%f,%f", &eps1, &eps2);
This will try to read a float from stdin into eps1, if it succeeds
will try to read the next character, and if it is a comma it will
try to read another float into eps2. The function returns the
number of elements assigned, or a negative value if there's an IO
error before any element is assigned. Check its result, if there
is no comma between the two values it will return 1 at best, and
eps2 will stay uninitialized.
if (fabs(x_starter-(powf(x_starter,3.0)-1)) < eps1)
{
printf("Error!\n");
I'd use a more descriptive message, and would print it to stderr,
which will usually still be the terminal when stdout is redirected
to a disk file.
}
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);
These parentheses are useless. (But they do no harm, either.)
}

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?

Very likely, with scanf. It is a very difficult function to use
properly, usually it is better to read lines with fgets and
interpreting them with the strto* family of functions. If you do
use scanf, at least check the return value.
 
C

CBFalconer

Amy said:
I major maths in mu university, and I wanna build an app to do
Steffensen's Acceleration Method. Here's my code:

'mu' may be a typo, but 'wanna' cannot be. How can you possibly be
in a university with such a lack of spelling knowledge?
 
D

Default User

CBFalconer said:
'mu' may be a typo, but 'wanna' cannot be. How can you possibly be
in a university with such a lack of spelling knowledge?

Are you really gonna complain about that?




Brian
 
C

CBFalconer

Default said:
Are you really gonna complain about that?

It's an indication of the sloppy writing that is all too
prevalent. How, for example, is a user in Sumatra, who is relying
on a Sumatran/English dictionary, to make sense of such things? To
make it correct requires one more character and one more blank. Is
this too much to ask?
 
U

user923005

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?

Besides what is already mentioned, your function and it's derivative
look wrong.

/* The function: */
double f(double x)
{
return x * x * x - x - 1.0;
}

/* The first derivative needed for Steffensen's Acceleration */
double f1(double x)
{
return 3 * x * x - 1.0;
}
 
U

user923005

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?
/*
You can compare your results with my implementation:
*/
#include<math.h>
#include<float.h>
#include<stdlib.h>
#include<stdio.h>
typedef double(*q0)(double);double steffen(q0 q2,q0 q3,double q4,
double q5,double q6,long*q7,long*q8)??<double q9,q10,q11,q12,q13,q14,
q15,q16,q17
;double q18;double q19;long
q20;q16=q4;q20=0;*q8=0;q18=q4;q12=q4+1;q11
=q4+2;while(q20<*q7&&*q8==0)??<q4=q18;q14=q3(q4);if(q14!=0.0)??<q11=q4-
q2(q4)/q14;??>else??<*q8=1;q19=q18-q12;q18=q4;??>q15=q3(q11);if(q15
==
0.0)??<*q8=1;q19=q11-q4;q18=q11;printf("\120\162\157\142\154\145\155"
"\72\40\123\154\157\160\145\40\157\146\40\164\150\145\40\144\145"
"\162\151\166\141\164\151\166\145\40\151\163\40\172\145\162\157"
"\40\141\164\40\45\147\40\41"
,q11);??>else??<q12=q11-q2(q11)/q15;q9=(q11-q4)*(q11-q4);q10=q12-q11*
2
+q4;if(q10==0.0)??<*q8=1;q19=q12-q11;q18=q12;??>else??<q18=q4-q9/q10;
q19=q18-q12;??>q13=q2(q18);q17=fabs(q19)/(fabs(q18)+DBL_EPSILON);
if(q17<q5)??<printf(
"\122\145\154\141\164\151\166\145\40\145\162\162\157\162\40\157\146"
"\40\45\147\40\151\163\40\154\145\163\163\40\164\150\141\156\40\163"
"\165\160\160\154\151\145\144\40\166\141\154\165\145\40\146\157\162"
"\40\144\145\154\164\141\40\157\146\40\45\147\56\n"
,q17,q5);*q8=2;??>if(fabs(q13)<q6)??<*q8=3;printf(
"\124\150\145\40\166\141\154\165\145\40\157\146\40\164\150\145\40"
"\146\165\156\143\164\151\157\156\40\50\45\147\51\40\141\164\40\45"
"\147\40\151\163\40\143\154\157\163\145\162\40\164\157\40\172\145"
"\162\157\40\164\150\141\156\40\164\150\145\40\163\165\160\160\154"
"\151\145\144\40\145\160\163\151\154\157\156\40\157\146\40\45\147\n"
,q13,q18,q6);if(*q8==2)??<*q8=4;??>??>??>++q20;??>*q7=q20;return q4;
??>

#ifdef UNIT_TEST
double f(double x)
{
return x * x * x - x - 1.0;
}

double f1(double x)
{
return 3 * x * x - 1.0;
}

char string[32767];
int main(void)
{
double guess = 1.3;
double delta = 1.0e-13;
double eps = 1.0e-13;
long Max = 100;
long cond = 0;
printf("Intial guess of %g was used.\n", guess);
printf("Root is about %g.\n",
steffen(f, f1, guess, delta, eps, &Max, &cond));
printf("Root achieved in %ld iterations.\n", Max);
printf("Condition returned was %ld\n", cond);
puts("Give me your guess:");
fflush(stdout);
fgets(string, sizeof string, stdin);
guess = atof(string);
Max = 100;
printf("Intial guess of %g was used.\n", guess);
printf("Root is about %g.\n",
steffen(f, f1, guess, delta, eps, &Max, &cond));
printf("Condition returned was %ld\n", cond);
printf("Root achieved in %ld iterations.\n", Max);
return 0;
}
#endif
 
D

Default User

CBFalconer said:
It's an indication of the sloppy writing that is all too
prevalent.

I dunno about that. I betcha you could find lots of examples. I just
gotta feeling.




Brian
 
M

Martin Ambuhl

CBFalconer said:
It's an indication of the sloppy writing that is all too
prevalent. How, for example, is a user in Sumatra, who is relying
on a Sumatran/English dictionary, to make sense of such things? To
make it correct requires one more character and one more blank. Is
this too much to ask?

I think it is more important that this kind of sloppiness in using the
natural language English may be a symptom of a deeper problem with
either thinking or discipline. That disorder is far too likely to show
up in a similar lack of care with the artificial languages used in
computer programming.
 
M

Mark McIntyre

I dunno about that. I betcha you could find lots of examples. I just
gotta feeling.

Joking aside, if you wrote an exam answer like that, or wrote your CV
like that, or wrote an email to your boss like that, what would your
chances of passing/getting hte job / being taken seriously be?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top