Please enable javascripts for pretty navigation. Here are ugly links to make it usable:

Speed Test of Using Symbolic VS Automatic Differentiation

by SeHyoun Ahn, Sept 2016

Contents

An html file properly marked up comments is avaiable at http://sehyoun.com/EXAMPLE_test_symbolic.html

Evaluates the Jacobian of

$$ y = \begin{bmatrix} x_1 & 0 & 0 & ... & 0 \\ 0 & x_2 & 0 & ... & 0\\ 0 & ... & ... &...& 0\\ 0 & 0 & ... & 0 & x_n\end{bmatrix} \begin{bmatrix} x_1\\ x_2\\ ...\\ x_n\end{bmatrix} $$ at $(x_1, x_2 , ... , x_n) = (1,2,...,n)$

n = 300;

Automatic Differentiation

fprintf('n = %d\n',n);
addpath('../');
fprintf('\n\nAUTOMATIC DIFFERENTIATION\n');
tAD = tic;
x = myAD((1:n)');
A = spdiags(x,0,n,n);
y = A*x;
dydx_val_AD = getderivs(y);
tAD = toc(tAD);
fprintf('   Done!\n\n');
n = 300


AUTOMATIC DIFFERENTIATION
   Done!

Symbolic Differentiation

disp('SYMBOLIC')
tSymb = tic;

tSetup = tic;
vector = sym('x_%d',[1 n])';
A      = diag(vector);
y      = A*vector;
fprintf('  Initialize Symbolic Variables: %2.6f secs\n',toc(tSetup));

tDeriv = tic;
dydx   = jacobian(y,vector');
fprintf('  Time to Differentiate: %2.6f secs\n',toc(tDeriv));

tConvert  = tic;
dydx_eval = matlabFunction(dydx);
fprintf('  Time to Convert to Function: %2.6f secs\n',toc(tConvert));
fprintf('     You would only need to do this conversion once to evaluate \n');
fprintf('     derivatives at different values of x_n.\n');
tEval     = tic;
x = num2cell(1:n);
dydx_val = dydx_eval(x{:});
fprintf('  Time to Calculate Numerical Values: %2.6f secs\n\n\n',toc(tEval));
% Uncomment the following and comment above block to Test evaluation
%     by using <subs>, but using matlabFunction is faster than subs
%{
tSubs    = tic;
dydx_val = subs(dydx,vector',1:n);
fprintf('Time to Calculate Numerical Values (Subs): %2.6f\n\n\n',toc(tSubs));
%}

tSymb = toc(tSymb);

fprintf('Total time using Automatic Differentiation: %2.6f secs\n',tAD);
fprintf('Total time using Symbolic: %2.6f secs\n',tSymb);
fprintf('   Note that just the evaluation of derivatives using symbolic\n');
fprintf('      is comparable to the entire computation of values and derivatives using AD.\n');
SYMBOLIC
  Initialize Symbolic Variables: 1.168734 secs
  Time to Differentiate: 1.504292 secs
  Time to Convert to Function: 184.486152 secs
     You would only need to do this conversion once to evaluate 
     derivatives at different values of x_n.
  Time to Calculate Numerical Values: 0.441781 secs


Total time using Automatic Differentiation: 0.006623 secs
Total time using Symbolic: 187.602057 secs
   Note that just the evaluation of derivatives using symbolic
      is comparable to the entire computation of values and derivatives using AD.
SYSTEM INFORMATION FOR REPLICATION
Computer:
	Version: ThinkPad X230

CPU:
	Version: Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz

Memory:
	Number Of Devices: 2
Memory Device
	Size: 4096 MB
	Type: DDR3
	Speed: 1600 MHz
	Configured Clock Speed: 1600 MHz
Memory Device
	Size: 4096 MB
	Type: DDR3
	Speed: 1600 MHz
	Configured Clock Speed: 1600 MHz

OS:
Description:	Linux Mint 18 Sarah

MATLAB:
9.1.0.441655 (R2016b)