# Thread: Newton-Raphson Iteration - logical problem

1. ## 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

2. 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.

3. 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. 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?)

5. -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. ok thx.
so I declare: double *array
but can I fill in its address in the function like that?
newtonRaphson(eps,&n2,&array);

7. What would possibly stop you from doing so?

8. 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. 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. 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.

11. Question 4.8
quzah was telling you this.

12. 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. 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. I suggest that OP take a good tutorial on pointer and memory management... before doing this.

15. finally it works, thx for your support!
Matts