Name

assert_condnum — Computes the empirical condition number of the function f at point x.

Calling Sequence

   [ c , y ] = assert_condnum ( f , x )
   [ c , y ] = assert_condnum ( f , x , order )
   [ c , y ] = assert_condnum ( f , x , order , h )
   
   

Parameters

f :

a function with header y = f(x)

x :

a vector of doubles, the current point

order :

a 1x1 matrix of floating point integers, the order (Default order = 2). Available are order= 1, 2, 4, 6. If order==[], then the default order is used.

h :

a matrix of doubles, the step. Default tries to be optimal for accuracy. If h==[], then the default h is used.

c :

the condition number as the ratio ry/rx

y :

the computed value of f at point x

Description

Computes the condition number by using an order one finite difference formula.

Any optional input argument equal to the empty matrix is replaced by its default value.

Examples

// Show that sin can be ill-conditionned
function [c,y] = sincond (x)
// Condition number of the sin function
y = sin(x)
c = abs(x.*cos(x)) ./ abs(y)
endfunction
[ c , y ] = assert_condnum ( sin , 1.e-100 )         // c~1
[ c , y ] = assert_condnum ( sin , 0 )               // c = nan
[ c , y ] = assert_condnum ( sin , 3.14159 )         // c ~10^6
[ c , y ] = assert_condnum ( sin , 3.141592653 )     // c ~10^9
[ c , y ] = assert_condnum ( sin , 3.1415926535898 ) // c ~10^14
[ c , y ] = assert_condnum ( sin , %pi )             // c ~10^16
[ c , y ] = assert_condnum ( sin , 1.000000357564167061e5 ) // c ~10^16

// An ill-conditionned case for the sum.
function [c,y] = sumcond(x)
// Condition number of the sum function
y = sum(x)
c = sum(abs(x)) / abs(y)
endfunction
xl = 10^(1:15);
x = [-xl xl+0.1];
yExpected = 1.5
cExpected = sumcond(x) // c~10^15
[ c , y ] = assert_condnum ( sum , x )

// Check various orders for the sqrt function.
function [c,y] = sqrtcond(x)
// Condition number of the sqrt function
y = sum(x)
c = 1/2
endfunction
// The condition number is 0.5.
cc = assert_condnum ( sqrt , 1 , 1 )
cc = assert_condnum ( sqrt , 1 , 2 )
cc = assert_condnum ( sqrt , 1 , 4 )
cc = assert_condnum ( sqrt , 1 , 6 )
// Make a loop
x = 1.;
order = [1 2 4 6];
for k = 1:4
cc = assert_condnum ( sqrt , x , order(k) );
ce = sqrtcond ( x );
d = assert_computedigits(cc,ce);
disp([order(k) cc ce d])
end

// Use default order, but configure h
[cc,yc] = assert_condnum ( sqrt , 1 , [] , 1.e-8 )
// Use default order and h
[cc,yc] = assert_condnum ( sqrt , 1 , [] , [] )
// Set order, use default h
[cc,yc] = assert_condnum ( sqrt , 1 , 4 , [] );

   

Authors

Michael Baudin, Digiteo, 2010