Newton-Raphson Iteration - logical problem

This is a discussion on Newton-Raphson Iteration - logical problem within the C Programming forums, part of the General Programming Boards category; Hi. I'm working on a program that implements several numerical methods. This is the function for the Newton-Raphson method. I'd ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    Newton-Raphson Iteration - logical problem

    Hi.
    I'm working on a program that implements several numerical methods.
    This is the function for the Newton-Raphson method. I'd like to give the funcion the precison, number of steps, and an array in which every step is hold.

    Code:
    void newtonRaphson(double eps,int *pn2,double *a)
    {	
    	//variable declaration
    	// array: every calculated value is stored into it
    	double *b = 0;
    	int array_size = 10;
    	a = (double*) malloc(array_size*sizeof(double));
    	int k = 0;
    	a[k]=0;
    	// newtonRaphson main loop
    	do
    	{	
    		if (k == array_size)
    		{
    			b = (double*) malloc(array_size*2*sizeof(double)); // allocates space
    		
    			// the values of array have to be copied into the new_array
    			for(k=0;k<array_size;k++)
    				b[k] = a[k];
    		
    			// free the number_array's memory
    			free(a);
    		
    			// pointer pnumber_array shows in direction of pnew_number_array
    			a = b;
    		
    			// array_size = array_size * 2;
    			array_size *= 2; 
    		}
    		
    		double c = a[k];
    		a[k+1] = a[k] -(myfun(c)/myfun_der(c));
    		// k+1 slots occupied
    		k++; 
    	
    	}while (abs(a[k+1] - a[k])>eps);
                  // only one array element as a test, bcs nothing is working yet .. 
    	printf("%lf",a[k]);
    }
    in the main function I have declared:
    double array;
    int n1; EDIT: i dont manipulate it in the function yet, bcs nothing is working yet ..
    and eps is given by the user, but this shouln't be the fault bcs it is working in other functions .. neither the myfun,myfun_der functions which are also working ..

    thx for helping me! =)

    Greets
    matts
    Last edited by Matts; 05-25-2011 at 02:37 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want a to have the new memory address (of your new malloc) outside of the function, you need to be passing a double **a, and send the function the address of the pointer you want to stick the new value in:
    Code:
    void foo( double **x )
    {
        ...
    }
    ...
    double *d;
    ...
    foo( &d );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you probably need to state what you mean by "nothing is working" since who knows what you mean by that.

    Also the bit about b can more or less be eliminated by using realloc().

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    9
    hi thx for the answers.
    my issue is to store the values in the array variable and not intoa .. so I have to do it still your way? (@quzah)
    Code:
    void newtonRaphson(double eps,int *,double *);
    int main()
    {
           ...
           int n1,n2;
           double array;
           double *parray;
           parray = &array;
           ...
           newtonRaphson(eps,&n2,&array);
           ...
    }
    by nothing is working I mean the output for the a[k] elment ist -1.#INFOO0000 ... (what is this value exactly?)
    Last edited by Matts; 05-25-2011 at 03:04 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    -1.#INF = negative infinity, so hooray for dividing by zero. You can't store the values in the array variable, because the array variable is just a single double, and not a bunch of doubles. If you want the "array" variable to be an array-like thing (although not a real array) then you need to make it a double*. (If you made it a real array it would not be resizable.) (ETA: If you just want the answer at the end, and not all the "scratch work" that led up to it, then you use b as your array of values and then at the end set *a = b[k].)

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    9
    ok thx.
    so I declare: double *array
    but can I fill in its address in the function like that?
    newtonRaphson(eps,&n2,&array);

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What would possibly stop you from doing so?

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    9
    I more or less sure that it is wrong like that. But now I have a pointer but I should give my function an address so that it can manipulate it ..
    double *array is a variable that holds a pointers address with no value.. I dont know more bcs my c knowledge is very limited yet

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to give your function the address of the variable you expect it to manipulate. You expect your function to manipulate the variable array. Therefore you should pass your function the address of the variable array. This reasoning has not changed from 20 minutes ago when you thought array was a single number, and therefore the conclusion remains valid.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I showed you how to pass it, the rest stays almost the same, and you can access the doubles you allocate like you do an array's elements.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 4.8
    quzah was telling you this.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    9
    Code:
    void newtonRaphson(double eps,int *pn2,double **a)
    {	
    	//variable declaration
    	// array: every calculated value is stored into it
    	double *b = 0;
    	int array_size = 10;
    	a = (double*) malloc(array_size*sizeof(double)); 1
    	int k = 0;
    	a[k]=0;
    	// newtonRaphson main loop
    	do
    	{	
    		if (k == array_size)
    		{
    			b = (double*) malloc(array_size*2*sizeof(double)); // allocates space
    		
    			// the values of array have to be copied into the new_array
    			for(k=0;k<array_size;k++)
    				b[k] = a[k]; 2
    		
    			// free the number_array's memory
    			free(a);
    		
    			// pointer pnumber_array shows in direction of pnew_number_array
    			a = b;  3
    		
    			// array_size = array_size * 2;
    			array_size *= 2; 
    		}
    		
    		double c = a[k]; 4
    		a[k+1] = a[k] -(myfun(c)/myfun_der(c)); 5
    		// k+1 slots occupied
    		k++; 
    	
    	}while (abs(a[k+1] - a[k])>eps);
    	// only one array element as a test, bcs nothing is working yet .. 
    	printf("%lf\n",a[k]);
    }
    9-1.c: In function 'newtonRaphson':
    9-1.c:79:4: warning: assignment from incompatible pointer type (1)
    9-1.c:91:10: error: incompatible types when assigning to type 'double' from type
    'double *' (2)
    9-1.c:97:6: warning: assignment from incompatible pointer type (3)
    9-1.c:103:14: error: incompatible types when initializing type 'double' using ty
    pe 'double *' (4)
    9-1.c:104:17: error: invalid operands to binary - (have 'double *' and 'doule') (5)

    I did it like quzah said. If I knew what the ** operand exactly is, I would understand the program how it is now better.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This isn't Fortran. We don't have a ** operand. As far as the type goes, it means just what it says: it is a pointer to (i.e., you passed in the address of) a double* argument. So just as before: if you want to modify the variable that comes in, just as you had to mess with *a before, you have to mess with *a now. *a = malloc, (*a)[k] = 0, etc.

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I suggest that OP take a good tutorial on pointer and memory management... before doing this.

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    9
    finally it works, thx for your support!
    Matts

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 raphson
    By kariisa in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2011, 08:13 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 Raphson method code
    By taebin in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 03:07 PM

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