# MATLAB program to compute the potential in a square region by solving Laplace’s equation using the Finite Difference (FD) approximation method.using the “surf” and “contour” MATLAB commands

13 views (last 30 days)
Sai Hitesh Gorantla on 6 Jun 2020
Commented: KSSV on 7 Jun 2020
My Code:
clear all;
[X,Y] = meshgrid(linspace(0,1,20));
V = zeros(size(X));
[M,N] = size(V);
V(2:M-1,1) = zeros(M-2,1);
V(2:M-1,N) = 100;
V(1,2:N-1) = 40;
V(M,2:N-1) = 10;
for r = 1:10000
V(2:M-1,2:N-1) = 0.25*(V(3:M,2:N-1) + V(1:M-2,2:N-1) + V(2:M-1,2:N-1) + V(4:M-1,2:N-1));
end
surf(X,Y,V);
Getting Error:
>> contour(X,Y,V)
Unrecognized function or variable 'X'.
Sai Hitesh Gorantla on 6 Jun 2020

KSSV on 7 Jun 2020
Just rectifies the error you said:
clc; clear all ;
clear all;
[X,Y] = meshgrid(linspace(0,1,20));
V = zeros(size(X));
[M,N] = size(V);
V(2:M-1,1) = zeros(M-2,1);
V(2:M-1,N) = 100;
V(1,2:N-1) = 40;
V(M,2:N-1) = 10;
for r = 1:10000
V(2:M-1,2:N-1) = 0.25*(V(3:M,2:N-1) + V(1:M-2,2:N-1) ....
+ V(2:M-1,2:N-1) + V(2:M-1,2:N-1));
end
surf(X,Y,V);
KSSV on 7 Jun 2020
I have already mentioned it rectifies onl the error. For expected contour, you have to check your code according to the theory or formula.

### Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!