By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,730 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,730 IT Pros & Developers. It's quick & easy.

Help: About Steffensen's Acceleration Method

P: n/a
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
Oct 5 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.

--
Er*********@sun.com

Oct 5 '07 #2

P: n/a
On Fri, 05 Oct 2007 23:00:09 +0800, Amy Lee wrote:
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.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 5 '07 #3

P: n/a
Amy Lee wrote:
>
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?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 5 '07 #4

P: n/a
CBFalconer wrote:
Amy Lee wrote:

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?
Are you really gonna complain about that?


Brian

Oct 5 '07 #5

P: n/a
On Oct 5, 8:00 am, Amy Lee <openlinuxsou...@gmail.comwrote:
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;
}

Oct 5 '07 #6

P: n/a
On Oct 5, 8:00 am, Amy Lee <openlinuxsou...@gmail.comwrote:
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);i f(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\17 2\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\1 57\162\40\157\146"
"\40\45\147\40\151\163\40\154\145\163\163\40\164\1 50\141\156\40\163"
"\165\160\160\154\151\145\144\40\166\141\154\165\1 45\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\15 7\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

Oct 5 '07 #7

P: n/a
Default User wrote:
CBFalconer wrote:
>Amy Lee wrote:
>>>
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?

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?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 6 '07 #8

P: n/a
CBFalconer wrote:
Default User wrote:
CBFalconer wrote:
Amy Lee wrote:

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?
Are you really gonna complain about that?

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
Oct 6 '07 #9

P: n/a
On 6 Oct 2007 00:42:34 GMT, in comp.lang.c , "Default User"
<de***********@yahoo.comwrote:
>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
Oct 6 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.