How to accomplish periodic boundary condition?

65 visualizzazioni (ultimi 30 giorni)
adv v
adv v il 6 Mar 2015
Risposto: CKanellas il 10 Mag 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 Commento
adv v
adv v il 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.

Accedi per commentare.

Risposte (1)

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

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by