From help odeset
:
Vectorized - Vectorized ODE function [ on | {off} ]
Set this property 'on' if the ODE function F is coded so that F(t,[y1 y2 ...]) returns [F(t,y1) F(t,y2) ...].
For the van der Pol example:
without vectorization:
function dydt = vdp1000(t,y) %// 'y' is passed as [y1
%// y2]
dydt = [y(2); %// 'dydt' is computed as [y1´
1000*(1-y(1)^2)*y(2)-y(1)] %// y2´]
%// where the ´ indicates d/dt
with vectorization:
function dydt = vdp1000(t,y) %// 'y' is passed as [y11 y21 y31 ...
%// y12 y22 y32 ...]
dydt = [y(2,:); %// 'dydt' is computed as
1000*(1-y(1,:).^2).*y(2,:)-y(1,:)]; %// [y11´ y21´ y31´ ...
%// y12´ y22´ y32´ ...]
where the y1
, y2
, y3
, etc. refer to different vectors y
at the same time t
that ode15s
will use to compute the next step.
For your example, you have to take into account that the y
you get passed is no longer a vector, but a matrix in which every column represents a different vector you need to compute the derivative of:
function dydt = exampleODEfun(t,y,N)
%// Adjust sizes to meet size of y
dydt = zeros(2*N, size(y,2));
dTdt = zeros(N, size(y,2));
dXdt = zeros(N, size(y,2));
%// Adjust indices to extract proper vales of ALL vectors
T = y(1:N,:);
X = y(N+1:2*N,:);
%// This sort of section is usually where all the "thought" goes into:
%// you can't use a*b' anymore, so I sum over the third dimension of the
% 3D array I built from your original vector
b = [3 5 -2];
ab = sum(cat(3, b(1)*T(2:N,:).^2, b(2)*T(2:N,:), b(3)*ones(N-1, size(y,2))), 3);
%// and finish it off
dTdt(1:N,:) = 0;
dXdt(1,:) = 0;
dXdt(2:N,:) = ab;
dydt(1:N,:) = dTdt;
dydt(N+1:2*N,:) = dXdt;
end