0
$\begingroup$

I would like to program the following difference equation.


Find numbers $v_{i,j}$ so that for $1\leq i\leq 4$, $0\leq j\leq 5$: $$ v_{i,j+1} = (0.1)v_{i+1,j} + (0.8)v_{i,j} + (0.1)v_{i-1,j} $$ In addition it satisfies the conditions, for $0\leq j\leq 5$: $$ v_{0,j} = (0.2)j \text{ and }v_{5,j} = 25 + (0.2)j$$ And finally, the condition, for $0\leq i\leq 5$: $$ v_{i,0} = i^2 $$


I made a picture of this grid, where $v_{i,j}$ is draw in the standard xy-plane.

enter image description here

The red points, is the information which is given to us. The intermediate grid is unknown, but we can determine it from the difference equation. The values of $v_{i,j}$ are draw in black numbers next to red points.

The tree in the middle is the visualization of the difference equation. The purple point is the weighed average of the three points below it with the numbers indicating how it is being averaged.


I would like to produce a grid $v_{i,j}$ in MatLab. Then plot it using the 'surf' command for the approximating surface.

$\endgroup$
1
  • 1
    $\begingroup$ Welcome to scicomp.SE. I don't see a question here. Have you even tried to do this yet? If so, what would you like to ask? $\endgroup$ Sep 18, 2016 at 10:43

1 Answer 1

2
$\begingroup$

According to the way you defined your stencil in your picture, I think you made a mistake in your formula, that should be : $$ v_{i+1,j}=(0.1)v_{i,j-1}+(0.8)v_{i,j}+(0.1)v_{i+1,j} $$ You have switched $i$ and $j$, same thing in your initial conditions.

The implementation is very straightforward. Define a grid by using meshgrid. Define v as a matrix with the same dimension. The first two loops define the boundary conditions. And the last one is the difference formula. Then, all is in place to use surf :

clc;clear all;

N=5;M=5;
[X,Y]=meshgrid(0:N,0:M);
v=zeros(N+1,M+1);

for i=1:M+1
     v(i,1)=0.2*(i-1);
     v(i,end)=v(i,1)+25;
end
for j=1:N+1
    v(1,j)=(j-1)*(j-1);
end

for i=1:N
    for j=2:M
        v(i+1,j)=0.1*v(i,j-1)+0.8*v(i,j)+0.1*v(i,j+1);
    end
end

surf(X,Y,v)
$\endgroup$

Your Answer

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

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