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

7 views (last 30 days)
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'.

Answers (1)

KSSV
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);
  3 Comments
KSSV
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!