MATLAB Answers

# How to accomplish periodic boundary condition?

76 views (last 30 days)
adv v on 6 Mar 2015
Answered: CKanellas on 10 May 2017
%Explicit Method
clear;
%Parameters to define the advective equation and the range in space and time
l=4*pi;%length of the wire
t=1.;%Final time
c=0.1
% Parameters needed to solve the equation within the explicit method
maxk=10; %Number of time steps
n=20; %Number of space steps
dx=l/n;
dt=dx*0.5/c
b=0.5
% Initial value of the function u(amplitude of the wave).
for i=1:n+1
for k=1:maxk
x(i) = (i-1)*dx;
t(k) = (k-1)*dt;
u0(i,:)= (sin(x(i))).^6
ua(i,k)= (sin(x(i)-c*t(k))).^6
end
end
u=ua(:,1:1)
u1=ua(:,1:1)
%p=[0:0.1:2*pi]
% Implementation of the explicit method
for k=2:maxk+1 %Timeloop
for i=2:n-1; %space loop
u(i,k+1)=u(i,k-1)-b*(u(i+1,k)-u(i-1,k));%Centeredinspace and centered in time(leapfrog)
end
end
for k=1:maxk %Timeloop
for i=2:n; %space loop
u1(1,k+1)=u1(n,k);
u1(i,k+1)=u1(i,k)-b*(u1(i,k)-u1(i-1,k));%upwind(forwardintime and backwardinspace
u1(n+1,k)=u1(n,k);
end
end
figure(1)
%plot(x,u)
%integrating the values at different time 1,5,10,25...
plot(x,ua(:,10),'g-',x,u1(:,10),'b-',x,u(:,10),'r')
legend('exact','uw','lf')
title('sinx,Resol(20)')
xlabel('X')
ylabel('Amplitude')

#### 1 Comment

adv v on 8 Mar 2015
Hi, Could someone help me how to accomplish periodic boundary condition in Leap frog and Upwind method. I have printed the copy of my program too.

Sign in to comment.

### Answers (1)

CKanellas on 10 May 2017
Instead of using u1(i-1,k) try circshift(u1,1,2).

#### 0 Comments

Sign in to comment.