필터 지우기
필터 지우기

help in solving an error

조회 수: 3 (최근 30일)
krayem youssef
krayem youssef 2019년 4월 6일
댓글: krayem youssef 2019년 4월 7일
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.

채택된 답변

Stephan
Stephan 2019년 4월 6일
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
  댓글 수: 1
krayem youssef
krayem youssef 2019년 4월 7일
thank u man !! i had to change another thing too but it finally worked

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by