Assignment 4 Relative_Error function and counting calls to f

Assignment 4 Relative_Error function and counting calls to f

by Karththigan Pushparaj -
Number of replies: 1
I have a question about how we are expected to count the number of function calls to f (which we have to return as a std::pair):


If we compute the stopping condition using error = relative_error(f, x), then relative_error calls f(x) internally, but in my solver loop (inside solve_fixed_point() ) I do:

X_next = f(x);   // first call to f

error = relative_error(f, x);  // second call to f?

num_calls++; // so should num_calls be incremented by two each iteration?

Are we expected to:

  1. Use relative_error directly to determine the stopping condition (so count two calls to f per iteration? 

or

  1. Compute the relative difference manually inside solve_fixed_point (using the already computed x_next) to ensure only one call to f per iteration (so we only use relative_error at the end in solverexample?)

In reply to Karththigan Pushparaj

Re: Assignment 4 Relative_Error function and counting calls to f

by Ramses van Zon -
Good question. Functionally, these implementation options yield the same result , but the first one would (unexpectedly to the user) be twice as expensive, so option two is more efficient and preferred.
A third option which avoids the duplicated code of option two, is to create a function - internal to the module - that computes the relative difference from two values of x, which both relative_error and solve_fixed_point could call.