help in solving an error
3 views (last 30 days)
Show older comments
hi guys,
i found this program on matlab and tried to modifie the boundary condition but it tells me that there is an error and i tried to figure how to solve it but i didn't know how
so if someone could help me i will be very thankfull.
0 Comments
Accepted Answer
Stephan
on 6 Apr 2019
Hi,
try:
% Solving the 2-D Poisson equation by the Finite Difference
...Method
% Numerical scheme used is a second order central difference in space
...(5-point difference)
%%
%Specifying parameters
nx=80; %Number of steps in space(x)
ny=80; %Number of steps in space(y)
niter=1000; %Number of iterations
dx=2/(nx-1); %Width of space step(x)
dy=2/(ny-1); %Width of space step(y)
x=0:dx:2; %Range of x(0,2) and specifying the grid points
y=0:dy:2; %Range of y(0,2) and specifying the grid points
b=zeros(nx,ny); %Preallocating b
pn=zeros(nx,ny); %Preallocating pn
%%
% Initial Conditions
p=zeros(nx,ny); %Preallocating p
%%
%Boundary conditions
p(:,1)=0;
p(:,ny)=0;
p(1,:)=0;
p(nx,:)=0;
%%
%Source term
for i=2:nx-1
for j=2:ny-1
b(i,j)=sin((pi/3)*i).*sin((pi/3)*j);
end
end
%%
i=2:nx-1;
j=2:ny-1;
%Explicit iterative scheme with C.D in space (5-point difference)
for it=1:niter
pn=p;
p(i,j)=((dy^2*(pn(i+1,j)+pn(i-1,j)))+(dx^2*(pn(i,j+1)+pn(i,j-1)))-(b(i,j)*dx^2*dy*2))/(2*(dx^2+dy^2));
%Boundary conditions
p(:,1)=0;
p(:,ny)=0;
p(1,:)=0;
p(nx,:)=0;
end
%%
%Plotting the solution
h=surf(x,y,p','EdgeColor','none');
shading interp
axis([-0.5 2.5 -0.5 2.5 -100 100])
title({'2-D Poisson equation';['{\itNumber of iterations} = ',num2str(it)]})
xlabel('Spatial co-ordinate (x) \rightarrow')
ylabel('{\leftarrow} Spatial co-ordinate (y)')
zlabel('Solution profile (P) \rightarrow')
Best regards
Stephan
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!