473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help: About Steffensen's Acceleration Method

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
9 2694
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: BuddyWork | last post by:
Hello, If you copy the source code from this post into seperate files as stated, and compile the code. Now to reproduce run the code in debug. 1. Press the Client button. 2. Put the cursor...
2
by: PJ6 | last post by:
Anyone know if MS has plans to *ever* have GDI+ support hardware acceleration? Paul
5
by: Ming Yeung | last post by:
I was wondering if .NET had the equivalent of Frames, Global Objects, and most importantly DataModules like in Delphi?
1
by: commander | last post by:
Hello I am trying to make a xml-parser, but now I am stuck.... This is my xml: <Emission type="CO2">249</Emission> <Acceleration>7.6</Acceleration> <MaxSpeed>250</MaxSpeed> <EUEconomy...
0
by: commander | last post by:
Hello I am trying to make a xml-parser, but now I am stuck.... This is my xml: <Emission type="CO2">249</Emission> <Acceleration>7.6</Acceleration> <MaxSpeed>250</MaxSpeed> <EUEconomy...
0
saputello
by: saputello | last post by:
hello, hello, hello can anyone see the problem with this drive() function? i can't get it to work! class Car { // properties public var mc:MovieClip; private var acceleration:Number;...
0
by: Frank Gallagher | last post by:
July 8 2008 Governments are far more corrupt than anyone would believe other than the members of Charter Democracy Force www.cdf.name who have a prodigious amount of irrefutable evidence and are...
2
by: PJ6 | last post by:
I would like to see my application's graphics running full-screen (1920x1200), and GDI+ just doesn't cut it aesthetically since it's neither hardware accelerated, nor efficiently implemented. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.