I have a discretely sampled 2D function:
S = 1 2 3 4 1 2 3 4 1 2 3 4
I want to find finite difference matrices, DX and DY such that:
$\ S_x=DX*S,S_y=DY*S $
where subscript denotes partial derivation with respect to.
Finding DY was easy. Using central differences:
DY = -1.0000 1.0000 0 -0.5000 0 0.5000 0 -1.0000 1.0000
Test:
>> DY*S ans = 0 0 0 0 0 0 0 0 0 0 0 0
Finding DX seems more difficult. Matrix product is "row x column". Since x is along the row of S; I try to find DX such that $\ S_x=S*DX $ instead:
DX = -1.0000 -0.5000 0 0 1.0000 0 -0.5000 0 0 0.5000 0 -1.0000 0 0 0.5000 1.0000
Test:
>> S*DX ans = 1 1 1 1 1 1 1 1 1 1 1 1
Now I have the two matrix equations: $\ Sx = S*DX, Sy = DY*S $
How can I combine these two into one matrix equation?
The thing is that I know Sx and Sy and am trying to solve for S using a least squares approach. I read that the conjugate gradient method does this. If I can cast my two equations into the shape of one matrix equation: $\ \mathbf{A}\mathbf{x}=\mathbf{b} $ I could use the conjugate gradient method to find a least squares solution.
I guess I will have to reshape S into a vector:
s=S(:)
s = 1 1 1 2 2 2 3 3 3 4 4 4
Next I will have to find a 12x12 matrix dy such that:
$\ sx = dx*s $
where sx now is also reshaped as a vector.
Using my example data and solving for dx I find:
>> sx/s ans = 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0 0 0 0 0 0 0 0 0 0 0.2500 0 0
How is this related to DX? I do not understand how to generate this matrix when s is unknown. BTW I am using Matlab.
Thanks in advance for any answers!