How to accomplish periodic boundary condition?

65 ビュー (過去 30 日間)
adv v
adv v 2015 年 3 月 6 日
回答済み: CKanellas 2017 年 5 月 10 日
%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 件のコメント
adv v
adv v 2015 年 3 月 8 日
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.

サインインしてコメントする。

回答 (1 件)

CKanellas
CKanellas 2017 年 5 月 10 日
Instead of using u1(i-1,k) try circshift(u1,1,2).

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by