QUAD2D sends in m-by-m matrices, which you are required to evaluate "elementwise", i.e. if Z = f(X,Y), then Z is the same size as X (and Y), and Z(i,j) = f(X(i,j),Y(i,j)).
The LENGTH function was really only meant for vectors. It is defined on matrices, and even on N-D arrays, as the maximum non-zero dimension length. So, for 14x14 matrix X, length(X) is 14. FIND, on the other hand, linearizes arrays. So an m-by-n input to FIND will result in a vector output of FIND that is between 0 and m*n elements in length. You could do this:
g = zeros(numel(X),1)
But you also need to make sure that the result N has the same size as X before the function returns, so I think what you need is
g = zeros(size(X));
You don't have to change the way you index g in what follows.
Rather than load the data each time, you may want to take advantage of the PERSISTENT feature
persistent r2 c2 C
if isempty(C)
load('Data_Frees_Valdez.mat','LOG_LOSS','LOG_ALEA');
[r2, c2] = size([LOG_LOSS LOG_ALEA]);
C=bivempcop([LOG_LOSS LOG_ALEA]);
end
This will prevent the function from doing those computations every time it is called. If you change the data set, just type
>> clear ecopula
to force the function to re-read the data on the next call.
Now, moving on from that detail, QUAD2D is not a great method for integrating functions with jump discontinuities. It is really intended for smooth functions with or without edge singularities. I would recommend using the newer INTEGRAL2 function with the 'iterated' method. This integration method handles jump discontinuities much more efficiently. The problem with QUAD2D, and with INTEGRAL2 using the 'tiled' method, is that the code can't really refine the mesh only perpendicularly to the discontinuity. It ends up refining in both x and y directions, which can create a lot of function evaluations. Normally we suggest using the 'tiled' method anyway but after splitting the region so jump discontinuities only occur on integral boundaries, but that's not practical when the function is fundamentally piecewise-defined with many pieces. -- Mike
