- #1
CAF123
Gold Member
- 2,948
- 88
I want to compute the gradient of some smooth function at many points by taking the value of the function at point x(i) subtracted from the value of the function at point x(i+1) and then divide the result by ( x(i+1)-x(i) ). My function has a struct as an argument and within that struct I have a member variable `double x;' How can I pass the value x(i+1) to the function without introducing another struct? i.e how to evaluate
if my function func has a struct `struct1` as an argument where struct1 is defined as follows:
struct struct1 {
...
double x;
...
}
ie. func = func(struct1). How to let the function to know to take the value x1 and not just x?
(I am using someone else's code to perform this task so I do not wish to reformat the whole code, i.e I could just trivially do (func(x1)-func(x))/(x1-x) but I would have to reformat the definition of func which is used many times for different purposes throughout the program. It has the struct instead as an argument, func(struct1) which has member x and I'd like to evaluate func not at x but at x1 so I can use it in the gradient definition. Hope my question makes sense
Code:
for (int i = 0; i<=100; i++) {
x=(100-10)*double(i)/100+10;
x1=(100-10)*double(i+1)/100+10;
double slope = (func@x1 - func@x)/(x1-x) ; }
if my function func has a struct `struct1` as an argument where struct1 is defined as follows:
struct struct1 {
...
double x;
...
}
ie. func = func(struct1). How to let the function to know to take the value x1 and not just x?
(I am using someone else's code to perform this task so I do not wish to reformat the whole code, i.e I could just trivially do (func(x1)-func(x))/(x1-x) but I would have to reformat the definition of func which is used many times for different purposes throughout the program. It has the struct instead as an argument, func(struct1) which has member x and I'd like to evaluate func not at x but at x1 so I can use it in the gradient definition. Hope my question makes sense