Flattening Matrices

Plotting Matrices and Resolution

{{a,b},{c,d}}={{0,5},{1,4}};
{Δx,Δy}={0.1,0.1};
U = Table[
(x-3)^2 +( y-2)^2,
{x, a, b, Δx},
{y, c, d, Δy}
];
{n,m}=Dimensions[U]
ListPlot3D[U]
ListContourPlot[U]

All our software solves equations A.x=b so we need vectors of unknowns. Easy but confusing :)

u=Flatten[U];
Dimensions[U]
Dimensions[u]
ListLinePlot[u]

UU=Partition[u,31 ];
ListPlot3D[UU]

Counting in a Flattened Array

we need to be able to go backwards and forwards between the Array indexing  and the flat vector indexing.

One way is given {i,j} find k.

i=3;
j=4;
U[[i,j]]
u[[31*(i-1)+j]]

31*51

k=134;
j=Mod[k-1,31]+1
i=1+(k-j)/31
u[[k]]
U[[i,j]]

Equations for a 2D Diffussion Equation. 
Periodic and Neumann Conditions

Consider (Subscript[u, t])=k ((Subscript[u, xx])+(Subscript[u, yy]))+f for a≤x≤b, c≤y≤d (with Δx=((b-a)/mx)and Δy=((d-c)/my)) and initial conditions u(x,y,0)=g(x,y) and boundary conditions u(a,y,t)=u(b,y,t) and (Subscript[u, y])(x,c,t)=(Subscript[u, y])(x,d,t)=0.  Use an explicit scheme with a forward difference in time and centered differences in the spatial variables to approximate the solution at positive times (Subscript[t, n])=n Δt and spatial coordinates (Subscript[x, i])=a+i Δx and (Subscript[y, j])=c+j Δy.

The interior equation at (Subscript[𝒰, i,j]^n)≈u((Subscript[x, i]),(Subscript[y, j]),(Subscript[t, n])) is
	(((Subscript[𝒰, i,j]^(n+1))-(Subscript[𝒰, i,j]^n))/(Δt))=k((((Subscript[𝒰, i-1,j]^n) - 2 (Subscript[𝒰, i,j]^n) + (Subscript[𝒰, i+1,j]^n))/((Δx)^2))+(((Subscript[𝒰, i,j-1]^n) - 2 (Subscript[𝒰, i,j]^n) + (Subscript[𝒰, i,j+1]^n))/((Δy)^2)))+(Subscript[ℱ, i,j]^n)
where (Subscript[ℱ, i,j]^n)≈f((Subscript[x, i]),(Subscript[y, j]),(Subscript[t, n])).

The periodic equation at (Subscript[𝒰, 0,j]^n)≈u((Subscript[x, 0]),(Subscript[y, j]),(Subscript[t, n])) is 
	(((Subscript[𝒰, 0,j]^(n+1))-(Subscript[𝒰, 0,j]^n))/(Δt))=k((((Subscript[𝒰, -1,j]^n) - 2 (Subscript[𝒰, 0,j]^n) + (Subscript[𝒰, 1,j]^n))/((Δx)^2))+(((Subscript[𝒰, 0,j-1]^n) - 2 (Subscript[𝒰, 0,j]^n) + (Subscript[𝒰, 0,j+1]^n))/((Δy)^2)))+(Subscript[ℱ, 0,j]^n)
where we replace (Subscript[𝒰, -1,j]^n)  with (Subscript[𝒰, mx-1,j]^n).

The periodic equation at (Subscript[𝒰, mx,j]^n)≈u((Subscript[x, mx]),(Subscript[y, j]),(Subscript[t, n])) is 
	(((Subscript[𝒰, mx,j]^(n+1))-(Subscript[𝒰, mx,j]^n))/(Δt))=k((((Subscript[𝒰, mx-1,j]^n) - 2 (Subscript[𝒰, mx,j]^n) + (Subscript[𝒰, mx+1,j]^n))/((Δx)^2))+(((Subscript[𝒰, mx,j-1]^n) - 2 (Subscript[𝒰, mx,j]^n) + (Subscript[𝒰, mx,j+1]^n))/((Δy)^2)))+(Subscript[ℱ, mx,j]^n)
where we replace (Subscript[𝒰, mx+1,j]^n)  with (Subscript[𝒰, 1,j]^n).

The equation at (Subscript[𝒰, i,0]^n)≈u((Subscript[x, i]),(Subscript[y, 0]),(Subscript[t, n])) is 
	(((Subscript[𝒰, i,0]^(n+1))-(Subscript[𝒰, i,0]^n))/(Δt))=k((((Subscript[𝒰, i-1,0]^n) - 2 (Subscript[𝒰, i,0]^n) + (Subscript[𝒰, i+1,0]^n))/((Δx)^2))+(((Subscript[𝒰, i,-1]^n) - 2 (Subscript[𝒰, i,0]^n) + (Subscript[𝒰, i,1]^n))/((Δy)^2)))+(Subscript[ℱ, i,0]^n)
where we solve the approximate Neumann condition 
	(((Subscript[𝒰, i,-1]^n) -  (Subscript[𝒰, i,0]^n) )/(Δy))=0
to get 
	(Subscript[𝒰, i,-1]^n) =  (Subscript[𝒰, i,0]^n)
for the ghost value and replace (Subscript[𝒰, i,-1]^n)  with (Subscript[𝒰, 1,0]^n).

The Neumann condition at (Subscript[𝒰, i,my]^n)≈u((Subscript[x, i]),(Subscript[y, my]),(Subscript[t, n])) is similar.

There are four corner points.  The equation at (Subscript[𝒰, 0,0]^n)≈u((Subscript[x, 0]),(Subscript[y, 0]),(Subscript[t, n])) should be
	(((Subscript[𝒰, 0,0]^(n+1))-(Subscript[𝒰, 0,0]^n))/(Δt))=k((((Subscript[𝒰, -1,0]^n) - 2 (Subscript[𝒰, 0,0]^n) + (Subscript[𝒰, 1,0]^n))/((Δx)^2))+(((Subscript[𝒰, 0,-1]^n) - 2 (Subscript[𝒰, 0,0]^n) + (Subscript[𝒰, 0,1]^n))/((Δy)^2)))+(Subscript[ℱ, 0,0]^n)
and we can replace the pink values with the Neumann and periodic conditions.

Now the annoying thing is that we somehow want to count from 0 for our discretization and 1 for our matrices etc.

We need to work out how to put these equations into our matrix form
	((OverVector[u])^(n+1))=A·((OverVector[u])^n)+((OverVector[f])^n) 
where (OverVector[u]) and (OverVector[f]) are the flattened versions of the arrays (𝒰^n) and (ℱ^n).

Matrix for a 2D Diffussion Equation. 
Periodic and Neumann Conditions

Fisrt look at the general internal equation
	(((Subscript[𝒰, i,j]^(n+1))-(Subscript[𝒰, i,j]^n))/(Δt))=k((((Subscript[𝒰, i-1,j]^n) - 2 (Subscript[𝒰, i,j]^n) + (Subscript[𝒰, i+1,j]^n))/((Δx)^2))+(((Subscript[𝒰, i,j-1]^n) - 2 (Subscript[𝒰, i,j]^n) + (Subscript[𝒰, i,j+1]^n))/((Δy)^2)))+(Subscript[ℱ, i,j]^n)
Pick a translation from {i,j}->k