MATLAB: How to use a 2D vector in solving a differential equation

2d vectorintegration

Hi,
I am trying to solve a second-order nonlinear equation (in terms of [zz,xx] where 'zz' is independent variable and xx is to solved) which contains a 2D vector [R_p], whose current position [R_p(zz,xx(1))] is called inside the differential equation. Here is the code
close all
r = [0:.1:20];
z = [20:.1:30];
rz = zeros(length(r),length(z));
for n=1:length(z)
w = w0*sqrt(1+(z(n)/zR).^2);
Gau = a0 .* exp(-(r-(max(r)/2)).^2/(2*w));
rz(:,n) = Gau;
n=n+1;
end
[Z_p,R_p] = gradient(rz);
f = @(zz,xx) [xx(2); -R_p(zz,xx(1))* sin(xx(1))*(xx(1))^2 + (1-xx(2))];
ode45(f,z,[1;0])
R_p is basically a 2D vector along zz and xx direction. In each integration step, the value of R_p depends on the current (or the pervious) value of zz and xx.
This doesn't seem to work. Please help me to solve this problem. If one removes R_p(zz,xx(1)), the code seems to work just fine.
Ajay

Best Answer

Your R_p is an array, so subscripts to it must be positive integers.
In your f, you have -R_p(zz,xx(1)) where zz is the time for the ode and xx is the first of the two derivatives . Neither zz or xx would be expected to be positive integers.
"In each integration step, the value of R_p depends on the current (or the pervious) value of zz and xx."
The ode functions have no "memory": there is no way to ask what the previous value of zz or xx(1) are.
Perhaps you want to use R_p(floor(zz), floor(xx(1)) . Perhaps you want to use interp2 to estimate R_p between defined points. Perhaps you want something else.