Newton's Method Program

This is a discussion on Newton's Method Program within the C Programming forums, part of the General Programming Boards category; Hey, can someone please tell me why this program only prints the first result? The 1st estimate is returned before ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Can you please include a sample run of the program, and then show what output you were actually expecting?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    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. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "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. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    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...");
    	printf("...Root not found, method did not converge\n");
    	printf("F(x) = %8.5f and x = %8.5f\n", fx, x);
    	return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newton raphson method - C
    By arazki1yes in forum C Programming
    Replies: 8
    Last Post: 04-04-2011, 01:11 PM
  2. Newton's Method
    By hottiefee in forum C++ Programming
    Replies: 33
    Last Post: 02-19-2011, 08:57 AM
  3. using newton raphson method
    By sanskaar in forum C Programming
    Replies: 21
    Last Post: 09-27-2010, 12:02 AM
  4. Newton Raphson Method
    By zeb1d1ah in forum C Programming
    Replies: 2
    Last Post: 12-07-2009, 05:26 AM
  5. Newton's method
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 10-19-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21