1
$\begingroup$

I am trying to do numerical calculations by recursion with vectors. I wrote a test code with single variable using 2 ODE's as shown below,

Clear[x, y, t, h, NN, cpustart, cpuend, sol1];
resolution = 0.1; tmax = 1;
cpustart = TimeUsed[];
nn = IntegerPart[tmax/resolution];
With[{h = resolution, NN = nn, x0 = 0, y0 = 1, t0 = 0}, 
ans = Transpose[
RecurrenceTable[{x[k + 1] == x[k] + h 1/(x[k]^2 + y[k]^2)^(0.5), 
  y[k + 1] == y[k] + h 4/(x[k]^2 + y[k]^2)^(0.5), 
  t[k + 1] == t[k] + h , x[0] == x0, y[0] == y0, t[0] == t0}, {x, 
  y, t}, {k, 0, NN}]]];
ListPlot[ans[[1, All]], DataRange -> {0, tmax}, 
 PlotStyle -> {Yellow, PointSize[0.03]}]
ListPlot[ans[[2, All]], DataRange -> {0, tmax}, 
 PlotStyle -> {Yellow, PointSize[0.03]}]
cpuend = (TimeUsed[] - cpustart)
sol1 = cpuend;

Then I converted the above code to use vectors as shown below,

Clear[x1, y1, x2, y2, z1, z2, r1, r2, t, h, NN, cpustart, cpuend, 
 sol2];
r1 = {x1, y1, z1};
r2 = {x2, y2, z2};
resolution = 0.1; tmax = 1;
cpustart = TimeUsed[];
nn = IntegerPart[tmax/resolution];
With[{h = resolution, NN = nn, r10 = {0, 0, 0}, r20 = {1, 1, 0}, 
  t0 = 0}, ans = Transpose[RecurrenceTable[{
  r1[k + 1] == r1[k] + h r1[k]/(r1[k] . r2[k])^(0.5),
  r2[k + 1] == r2[k] + h 4 r2[k]/(r1[k] . r2[k])^(0.5),
  t[k + 1] == t[k] + h , r1[0] == r10,
  r2[0] == r20, t[0] == t0}, {r1, r2, t}, {k, 0, NN}]]];
ListPlot3D[ans[[1, All]], DataRange -> {0, tmax}, 
 PlotStyle -> {Yellow, PointSize[0.03]}]
ListPlot3D[ans[[2, All]], DataRange -> {0, tmax}, 
 PlotStyle -> {Yellow, PointSize[0.03]}]
cpuend = (TimeUsed[] - cpustart)
sol2 = cpuend;

However, the above code does not work. I believe its due to wrong definition/use of vectors.

RecurrenceTable::dvout: The output function {x1,y1,z1} is not one of the dependent   variables {{x1,y1,z1},{x2,y2,z2},t}.

Where I am wrong?

$\endgroup$

1 Answer 1

1
$\begingroup$

I do not know if one can get RecurrenceTable to treat some dependent variables as vectors, but I will describe another solution using NestList. I usually try to stay away from more specialized commands if more basic commands such as NestList suffice.

Define the recurrence as a map:

recurrence[h_][{r1_,r2_,t_}]:={
   r1+h*r1/(r1.r2)^(1/2),
   r2+h*4*r2/(r1.r2)^(1/2),
   t+h
};

Given data such as

resolution=0.1;
r10={0,1,1};
r20={1,1,0};
t0=0;
nn=10;

the solution is obtained using

NestList[recurrence[resolution],{r10,r20,t0},nn]

Note. I used a different r10 than OP to avoid division by zero.

Note. To answer OP's actual question (Where I am wrong?) note that OP did something similar to the following simplified example:

RecurrenceTable[{
   {x1,x2,x3}[k+1]=={x1,x2,x3}[k]+{1,2,3},
   {x1,x2,x3}[0]=={4,5,6}},
   {{x1,x2,x3}},{k,0,2}]

In particular, the second argument {{x1,x2,x3}} is not just a symbol or list of symbols, but a list of vectors of symbols. Nothing in the current version of the documentation of RecurrenceTable suggests that something like this is supported, hence the error.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.