1. ## Newton's Method Program

Hey, can someone please tell me why this program only prints the first result? The 1st estimate is returned before any further estimates are apparently calculated:

Code:
```#include "StdAfx.h"
#include <stdio.h>
#include <math.h>

/* Function Prototypes */

double	ffunction(double beta, double gamma, double delta, double x),
dfunction(double beta, double gamma,  double x),
calcfunc(double fx, double dfx, double b,double c,double d, double guess);

int
main(void)
{
double	b,
c,
d,
guess,
dfx,
fx,
x;

printf("please enter b, c, d and guess>\n");
scanf("%lf %lf %lf %lf", &b, &c, &d, &guess);
fx = ffunction(b,c,d,guess);
dfx = dfunction(b,c,guess);
x=calcfunc(fx, dfx, b, c, d, guess);
printf("dfx= %f\n", dfx);
printf("fx= %f\n", fx);
printf("x= %f\n", x);
return(0);

}
double
ffunction(double beta, double gamma, double delta, double x){
return ((pow(x,3))+beta*pow(x,2)+gamma*x+delta);
}

double
dfunction(double beta, double gamma, double x){
return(3*pow(x,2)+2*beta*x+gamma);
}

double
calcfunc(double fx, double dfx, double b,double c,double d, double guess){
int	attempt=1,
count=1;
double	difference=1,
approximation=guess,
x,
dx;
while (fabs(difference) > fabs(0.000005)){
if (count<20 && fabs(dfx)>fabs(0.000005)){
x=ffunction(b, c, d, guess);
dx=dfunction(b, c, guess);
approximation = guess - (x/dx);
difference = approximation - guess;
guess = approximation;
count++;}
else if (count>20 && attempt<2){
printf("incguess(count, guess)");
return(fx, dfx, guess, difference, count, approximation);}
else
printf("displaynotfound()");
return(fx, dfx, guess, difference, count, approximation);
}
return(fx, dfx, guess, difference, count, approximation);
}```

2. Code:
```while( fabs > fabs )
if count < 20 and fabs > fabs
... keep going
else
if count > 20
... whatever
else
... stop if count < 20 and fabs ! > fabs```
Be careful with your checks. The second part of your first if check has to be true, and if it's not, you're going to fail down to the final else, which just returns.

Oh, and you can't return multiple things in C, so all those returns with things in parenthesis separated by , isn't doing what you think. (Or if you really think what it really is doing, then you are a very odd bean.)

Quzah.

3. Can you please include a sample run of the program, and then show what output you were actually expecting?

4. Well after what Quzah said, I realised I have some serious fixing up to do. So have started to do that. But I'm ultimately hoping for it to return a root of a cubic equation calculated using Newton's Method.

Output was as Follows:

please enter b, c, d and guess>
1 2 3 4
dfx= 58.000000
fx= 91.000000
x= 2.431034
Press any key to continue . . .

5. I have amended my code to this:

Code:
```#include "StdAfx.h"
#include <stdio.h>
#include <math.h>

/* Function Prototypes */

/*double	ffunction(),
dfunction(),
calcfunction(),
incguess(),
displayfound(),
displaynotfound();*/

double	ffunction(double beta, double gamma, double delta, double x),
dffunction(double beta, double gamma,  double x),
calcfunc(double b,double c,double d, double *iguess);
int		secondattempt(int count);
int
main(void)
{
double	b,
c,
d,
iguess,
x;

/*	FILE	*inp,
*outp;*/

//while (inpstat != EOF){//
printf("please enter b, c, d and guess>\n");
scanf("%lf %lf %lf %lf", &b, &c, &d, &iguess);
x=calcfunc(b, c, d, &iguess);
//}//endwhile

return(0);

}
double
ffunction(double beta, double gamma, double delta, double x){
return ((pow(x,3))+beta*pow(x,2)+gamma*x+delta);
}

double
dffunction(double beta, double gamma, double x){
return(3*pow(x,2)+2*beta*x+gamma);
}

double
calcfunc(double b,double c,double d, double *iguess){
int		attempt=1,
count=1;
double	difference=1,
x=*iguess,
approximation,
dfx=dffunction(b, c, *iguess),
fx=ffunction(b, c, d, x);

while (fabs(difference) > 0.000005){

if (count<20 && fabs(dfx)>0.000005){
approximation = x - (ffunction(b, c, d, x)/dffunction(b, c, x));
difference = approximation - *iguess;
x = approximation;
count ++;}
else if (count>20 && attempt == 1){
count=1;
attempt++;
printf("attempt: %d", attempt);
}
else
printf("displaynotfound()");
return(0);
}
printf("dfx= %f\n", dfx);
printf("fx= %f\n", fx);
printf("x= %f\n", x);
return(0);
}```
and I have discovered that if I remove the first return(0); from the while loop, the printf("displaynotfound()"); will innfinitely print to screen but put return(0); back in and it won't display once. Can anyone help me with this?

6. "Return" means ... well, "return". The function stops immediately and returns control from whence it came.

Your while loop is the one that needs to stop when the count reaches 20, not anything internally.

EDIT: Also, shouldn't difference be defined as approximation - x, not approximation - *iguess? You want to stop when the most two recent guesses are very close, not when the current guess is really close to completely-made-up initial number.

7. Yeah I noticed that and now have it all working. But I need to have multiple lines of input from a .dat file and I can't really work out how to loop through the file. I was thinking some sort of while inp!= eof but how exactly do i implement that i am kind of lost on. Here is my code so far:

Code:
```#include "StdAfx.h"
#include <stdio.h>
#include <math.h>

/* Function Prototypes */

/*double	ffunction(),
dfunction(),
calcfunction(),
incguess(),
displayfound(),
displaynotfound();*/

double	ffunction(double beta, double gamma, double delta, double x),
dffunction(double beta, double gamma,  double x),
calcfunc(double b,double c,double d, double *iguess),
found(double b,double c,double d, double *iguess, double attempt, int count, double x),
notfound(double b,double c,double d, double *iguess, double fx, double x);
int		secondattempt(int count);
int
main(void)
{
double	b,
c,
d,
iguess;
FILE	*inp = fopen("input1.dat", "r");
//	*outp;*/
//int	inpstat = getc(inp);

//while (inpstat != EOF){
//printf("please enter b, c, d and guess>\n");
fscanf(inp, "%lf %lf %lf %lf\n", &b, &c, &d, &iguess);
calcfunc(b, c, d, &iguess);

//}
fclose(inp);
return(0);

}
double
ffunction(double beta, double gamma, double delta, double x){
return ((pow(x,3))+beta*pow(x,2)+gamma*x+delta);
}

double
dffunction(double beta, double gamma, double x){
return(3*pow(x,2)+2*beta*x+gamma);
}

double
calcfunc(double b,double c,double d, double *iguess){
int		attempt=1,
count=0;
double	difference=1,
x=*iguess,
approximation,
dfx=dffunction(b, c, *iguess),
fx=ffunction(b, c, d, x);

while (fabs(difference) > 0.000005){
if (count<20 && fabs(dfx)>0.000005){
approximation = x - (ffunction(b, c, d, x)/dffunction(b, c, x));
difference = approximation - x;
x = approximation;
count ++;
}
else if (count>=20 && attempt == 1){
count=1;
attempt++;
difference=1;
x=*iguess + 10;
}else {
notfound( b, c, d, iguess, fx, x);
return(0);}
}
found(b, c, d, iguess, attempt, count, x);
return(x);
}
double
found(double b,double c,double d, double *iguess, double attempt, int count, double x){
printf("Newton's Method\n");
printf("For cubic polynomial a=1; b=%8.5f; c=%8.5f; d=%8.5f\n", b, c, d);
printf("With initial guess = %8.5f\n", *iguess);
if (attempt == 2){
count=2*count;
}
printf("Gives F(x) = zero at x= %8.5f...", x);
printf("...Root found after %d iterations\n", count);
return(0);
}
double
notfound(double b,double c,double d, double *iguess, double fx, double x){
printf("Newton's Method\n");
printf("For cubic polynomial a=1; b=%8.5f; c=%8.5f; d=%8.5f\n", b, c, d);
printf("With initial guess = %8.5f\n", *iguess);
printf("After 20 iterations...");