Are you sure?
This action might not be possible to undo. Are you sure you want to continue?
:
1DLinear
Wave
Equation]
[MAE]
[542]
[Engineering Applications of Computational Fluid Dynamics]
[ 04
th
April
2011]
Submitted by
Mandeep Singh
Person # 3721 2672
2  P a g e
Contents
1 Introduction ....................................................................................................................................3
2 Problem definition / Problem statement .......................................................................................3
3 Method of Solution : .......................................................................................................................5
4 Discussion of results ........................................................................................................................6
4.1 Lax & Lax Wendroff Solution :.................................................................................................. 6
4.2 1
st
and 2
nd
error terms of MDE .............................................................................................. 10
4.3 Leading order truncation error term : .................................................................................. 16
4.4 For wave number n = 3 : ....................................................................................................... 18
4.4.1 Error analysis for condition n = 3 ................................................................................... 23
4.5 Exploring the Heaviside Initial Condition ............................................................................... 26
5 Appendix ...................................................................................................................................... 36
5.1 Matlab Code ........................................................................................................................... 36
5.2 Heaviside Wave form plot for c = 0.3, 0.6 , 1 and t = 0.25 ..................................................... 43
6 References ................................................................................................................................... 44
3  P a g e
1 Introduction
Wave equation : It is a base model for the Hyperbolic equations. Hyperbolic partial
differential equations have time derivative and hence are generally solved using the
marching method. Lax, Lax Wendroff and midpoint leap frog methods are some of
explicit methods for exploring the wave propagation.
The 1Dlinear wave equation is given by equation
0
x
u
t
u
=
∂
∂
+
∂
∂
a
(1)
Where u is the scalar function satisfying the wave equation and x is the space
domain. ‘a’ is the speed of propagation.
2 Problem definition / Problem statement
In this project we need to solve the given wave equation for periodic boundary
condition. The domain length L = 1.
Periodic Boundary conditions for the given problem are given as follows :
L x x
x L x
t x u t x u
t x u t x u
= =
= =
− = −
+ = +
 ) , (  ) , (
 ) , (  ) , (
0
0
The value of a = 1. We need to analyze two initial condition 1. Sinusoidal initial
boundary condition with u
o
(x) = sin(2πnx) and , 2. Heaviside sharp step function ,
where initial condition is given as u
o
(x)=1H(x  0.5), where H is the Heaviside
function.
4  P a g e
We need to develop a computer algorithm to solve the given hyperbolic equation
using the Lax and Lax Wendroff method.
We need to analyze the system using the following variation in the system
S. No Condition to Explore Initial Condition Parameter given
1 Solution and accuracy for Lax and Lax
Wendroff compared to analytical solution
uo(x) = sin(2πnx) n = 1, t = 0.5,
c = 0.3, 0.6,1.0
2 Numerical error for the first and second
error term of MDE
uo(x) = sin(2πnx) n = 1, t = 0.5,
c = 0.3, 0.6,1.0
3 Leading order truncation error term
analytically for Lax and Lax Wendroff
method and relative phase of the errors
relative the speed of propagation u
uo(x) = sin(2πnx) n = 1, t = 0.5,
c = 0.3, 0.6,1.0
4 Repeat 1 & 2 for n = 3. Discuss change in
errors observed . Errors increases or
decreases
uo(x) = sin(2πnx) n = 3, t = 0.5,
c = 0.3, 0.6,1.0
5 Repeat 1 & 2 using the Heaviside boundary
condition
u
o
(x)=1H(x  0.5) n = 1, t = 0.25,
c = 0.3, 0.6,1.0
5  P a g e
3 Method of Solution :
In this problem we will use the two algorithms given i.e., Lax and Lax Wendroff for
getting the numerical solution for the given equation. Lax Algorithm is given by the
equation :
2 2
1 1 1 1
1
n
i
n
i
n
i
n
i
n
i
u u
c
u u
u
− + − +
+
−
−
−
=
Where c is the courant number given by the equation c = a Δt/Δx.
Lax Wendroff Algorithm is given by the following equation :
+ −
+
−
− =
− + − +
+
2
2
2
1 1 2 1 1
1
n
i
n
i
n
i
n
i
n
i
n
i
n
i
u u u
c
u u
c u u
Since the boundary condition in given periodic and the domain is descritized in space
from node 1 to nx. Hence for solving for node 1, we require u(i1) = u(1) = u(nx1)
similarly for calculating the last node nx, we require u(i+1) = u(nx+1) = u(2). Such
conditions also applied during the calculation of u
xx
, u
xxx
and u
xxxx
using the central
difference scheme.
6  P a g e
4 Discussion of results
4.1 Lax & Lax Wendroff Solution :
Bu using the value of n = 1, t = 0.5, Δx = 0.025, c = 0.3, we obtain the solution(figure
1) for the given wave equation. For the given conditions and value of c = 0.3, we can
see that the Lax method lacks accuracy at the same time LaxWendroff method
traces the analytical solution quite accurate.
Figure 1( Figure showing the solution for the wave equation for c =0.3, using the lax, Lax Wendroff and Analytical)
7  P a g e
From figure 1 , we can observe that the maximum error in the Lax method is
approximately 55% at the nodes when L = 0.25 and L = 0.75. Both methods that is Lax
and LaxWendroff follows the same propagation as of the sinusoidal wave.
Now we plot the solution plot using the value of c = 0.6 . For this parameter , keeping
the value of Δx=0.025, we get Δt = 0.015
Figure 2( Figure showing the solution for the wave equation for c =0.6, using the lax, Lax Wendroff and Analytical)
From the figure 2. We can see that the accuracy of the Lax method increases and the
error in the Lax Wendroff is observed. There is a shift of around 0.01 lag in the Lax
Wendroff method when c is increased from 0.3 to 0.6 though the accuracy is still
maintained. This may be due to the rounding of the time period near to 0.5 with the
8  P a g e
time step of 0.015, when c = 0.6. Since we plot the time at t = 0.5 which is not the
multiple of 0.015(0.5/0.015 = 33.333) hence we may observe this shift. Also we can
observe that there is a change of accuracy in the Lax method and the maximum error
can be observed at L = 0.25 and L =0.75 around 13%. Hence by increasing the value of
c, the accuracy of Lax method also increases.
Now we plot (figure 3) the solution for the value of c = 1, and the value of Δt = 0.025.
We can observe that the value of the solution for the lax method becomes more
accurate and approached the analytical solution.
Figure 3 (Figure showing the solution for the wave equation for c =1, using the lax, Lax Wendroff and Analytical)
For the value of c = 1, we get the solution for Lax and Lax Wendroff , almost
overlapping with the analytical solution. Hence high accuracy is achieved by
increasing the value of c to 1.
9  P a g e
Now we plot (Figure 4) for the solution of the wave equation using Lax method for
the various values of c ( 0.3 , 0.6 , 1)
Figure 4 ( Lax Method for Different Values of c  0.3 , 0.6 , 1.0 )
Hence, from the figure 4, we can see inaccurate of more than 50% in the solution
when the value of c = 0.3. Contrary to this scheme, If we take a glance on the Lax
Wendroff scheme(Figure 5, we see that the accuracy of this scheme is not much
affected by the value of c. There is marginal shift in the wave at c = 0.6. That may be
due to the truncation error. But Lax Wendroff scheme is stable and gives accurate
solutions to the given one dimensional wave equation when compared to the Lax
method.
10  P a g e
Figure 5 ( Lax Wendroff Method solution for different values of c = 0.3 , 0.6 , 1)
4.2 1
st
and 2
nd
error terms of MDE
In this section of the project we are given the Modified differential equations (MDE)
for the Lax and Lax Wendroff’s methods. The MDE’s only satisfy the numerical
solutions and hence we can calculate the error in out solutions using them. Given two
MDE’s for the two numerical methods we calculated the error for the 1st and the 2nd
error term in the equation. These terms contain the second, third and fourth
derivative of the wave function u with respect to space x. These derivatives has been
calculated using the central difference formula of the second order accuracy. The
values of u for calculating the derivatives has been taken from the values of u obtained
11  P a g e
by the Lax and Lax Wendroff method after time t = 0.5. Following MDE’s are given in
the project.
( ) Method s Lax u c
x a
u c
c
x a
au u
xxx xx x t
. ' ........ 1
3
1
2
2
2
+ −
∆
−

¹

\

−
∆
= +
( ) ( ) Method f LaxWendrof step u c c
x a
u c
x a
au u
xxxx xxx x t
. . 1 ........ 1
8
1
2
2
3
2
2
− + −
∆
+ −
∆
− = +
Using these values we plotted the error for two methods for different values of c we
obtained the results as shown in from Fig 6 to Fig 10.
Figure 6 (Figure showing the Disrtribution
of error for Lax Method for c = 0.3 at different nodal values)
12  P a g e
From figure 6, we can observe that in the Lax method, error grows proportionally to
the sine wave i.e. as the value of our wave equation increases the magnitude of error
increases and vice versa. The magnitude of the error has the maximum value of
around 0.7 for the value of c = 0.3. The first term in the MDE for the Lax Method is
Dissipative in nature and has high magnitude compared to the Dispersive error which
is the 2nd term in the Lax’s MDE. The maximum value for the dissipative error comes
out to be 0.01 which is around 1% compared to the 70% for the diffusion error.
Figure 7 (Figure showing distribution of error in the
Lax Wendroff Method for c = 0.3 at different nodal values)
For the value of c = 0.3 for the Lax Wendroff method , we get considerably less
amount of error with the maximum to be 0.023. The error in the Lax wendroff
compared to the Lax method is around 0.67 more. Hence for Low value of c, Lax
Wendroff is yields better and accurate results.
13  P a g e
Now, for c = 0.6(Figure 8 and 9), we can see in figure 8, that for the Lax method the
error has decreased to 0.4 compared to 0.7 when c = 0.3. But still there is 40% error
in the Lax method solution which is definitely not negligible. From the plots we can
see that the maximum value of second term of the MDE has increased to 0.2
compared to 0.1 for c = 0.3.
Figure 8 ( Figure showing distribution of error in the
Lax Method for c = 0.6 at different nodal values)
Perhaps for the Lax Wendroff method , by increasing the value of c to 0.6, higher
accuracy is achieved. Previously for c = 0.3 , the maximum value of error for the cyclic
boundary conditions was coming out to be 0.023. By increasing the value of c = 0.6,
the error has reduced to 0.017, that means error has further decreased by 26%. The
error is already less in Lax Wendroff and accuracy has further improved by 26%
14  P a g e
Figure 9 ( Figure showing distribution of error
in the Lax Wendroff Method for c = 0.6 at different nodal values)
For the value of c = 1(Figure 10), we can observe that the 1st and the 2nd term of the
MDE for both the methods has reduced to zero. Hence both numerical methods
yields accurate results.
15  P a g e
Figure 10( Figure showing distribution of error in Lax and
Lax Wendroff Method for c = 1 at different nodal values)
Also not that the error terms in the MDE contains the relation of subtraction of c
from 1. Hence for c = 1, the 1st term and the 2nd term becomes zero, hence reducing
the error.
Hence for from this problem we have seen how the error improves by varying the
value of c. Lax method yields better results for higher values of c. For lower values of
c, 1step Lax Wendroff is more accurate.
16  P a g e
4.3 Leading order truncation error term :
If we calculate the leading error term using the derivative of the sin function given as
the solution to the wave equation we get the error for the 1st and 2nd term
analytically to be maximum 1.5
For calculating the u
xx
we used the exact solution for the wave equation
˯(˲, ˮ) = s˩n(Ŷnn(˲ oˮ))
which leads to
˯
xx
= Ÿn
2
n
2
s˩n(Ŷnn(˲ oˮ))
This leads us to the leading error term for the 1st MDE in the equation to be

oA˲
Ŷ
(
ŵ
c
c)(Ÿ)n
2
n
2
s˩n(Ŷnn(˲ oˮ))
Similarly by calculating the leading error for the second MDE for wave equation of
lax method we get the derivative
˯
xxx
= 8n
3
n
3
cos(Ŷnn(˲ oˮ))
The leading term for the 2nd MDE comes out to be
oA˲
2
ź
(ŵ  c
2
)8n
ŷ
n
ŷ
cos(Ŷnn(˲  oˮ))
For the first term , we get the sinusoidal derivative with a negative sign. The negative
sign makes the dissipation term of the MDE i.e. the even derivative term to be at the
17  P a g e
phase difference of π. At the same time the dispersive term has the value of cosine
which makes the error to lag by π/2 .
Similarly for the 2nd MDE for the Lax Wendroff , we have the leading error term to be
dispersive with an odd derivative , which makes the leading error to be in phase
difference of π/2. The second term in the 2nd MDE is dissipative(even) term and
since its derivative will come to be a sinusoidal term hence the term will have the
phase difference of π.
Figure 11 (Error plot for c = 0.3 using the analytical solution for calculating leading term derivative)
From the plot we can observe that the phase difference for the lax method first
truncatione error term is having the same sinusoidal shape with the opposite sign of
the wave propagation. Hence we have a difference for π for the 1st MDE (Lax
Method)
18  P a g e
Similarly for the 2nd MDE , Leading error truncation term has the phase difference of
π/2 since it follows a cosine nature. Clearly the error plots obtained in the 2
nd
case
(calculating the errors numerically) are consistent with the phase obtained
analytically. Though the value of error doesn’t matches because the derivative in the
MDE cannot be satisfied by the derivatives for analytical solution. The derivatives for
the MDE will satisfy the derivative calculated using the MDE.
4.4 For wave number n = 3 :
For this condition we get more number of waves within the same domain length
(L=1). The number of cycle is equal to 3 since n = 3. For this wave condition also, we
can observe that Lax Wendroff method behaves well for the value of c = 0.3. In figure
12 some lag can be observed due to the error in truncation and some dissipation
error. Hence we can observe a lag in the numerical wave. Also for the Lax Wendroff
we get the maximum peak of 0.96, which gives us the error of 4%. At the same time ,
we get almost 100% inaccuracy for the Lax method and its value remains zero for the
whole length domain.
Figure 12 ( Numerical Solution to the Wave Equation for n = 3 using Lax and Lax Wendroff Method)
19  P a g e
Figure 13 (Numerical Solution to the Wave Equation using Lax Wendroff and Lax method for c = 0.6, n=3)
For c=0.6, n = 3, we can see in the fig 13 that some edges in the Lax methods starts
appearing and the peaks for the Lax Wendroff methods remains almost same as
observed in the plot(figure 12) for solution when c = 0.3.
We can also see a shift of 0.4 on the Lax Wendroff scheme and its solution lags. It
may be due to the truncation error for the time step.
20  P a g e
Figure 14(Numerical Solution to the Wave Equation using Lax Wendroff and Lax method for c = 1, n=3)
For the value of c = 1, the Lax and Lax Wendroff numerical techniques both
approaches the analytical solution and can be seen overlapped on the analytical
solution as shown in fig 14. Hence in this case also the we see the same trend for Lax
method , in which the accuracy increases with the increase in the value of c.
21  P a g e
Figure 15 (Numerical Solution to the Wave Equation using Lax method for c = 0.3,0.6,1 and n=3)
For comparison for the c = 0.3, 0.6,1 we can see that the value of u calculated using c
= 0.3 is zero. At c =0.6 we get small amplitude waves of maximum height around 0.1.
This growth in the value of u shoots up when c > 0.7. before that there is not much
appreciation in the value of u. But for the Lax Wendroff method , we can see that this
method generates accurate answers for almost any value of 0<c<=1. As observed in
the Figure 16, Lax Wendroff moves quite close to the analytical solution of the wave.
22  P a g e
Figure 16 (Numerical Solution to the Wave Equation using Lax Wendroff method for c = 0.3,0.6,1 and n=3)
23  P a g e
4.4.1 Error analysis for condition n = 3
There is one interesting fact to be observed(figure 17) for the Lax method error for
the first and secong error terms. The uxx and uxxx for the terms are calculated from
the central difference. But since the value of u Lax for c = 0.3 is almost zero , then
also we are getting the value of error of around 0.0125 as shown in figure. We are
not getting 100 % error , which the case should be, may be because of the time and
space step. There can be a problem due to the mesh size which may be creating the
error in the central difference formula.
Figure 17(1st and 2nd Error Waveform for the MDE at c = 0.3 for Lax Method)
24  P a g e
Figure 18( Figure showing distribution of error in the Lax Wendroff Method for c = 0.3 at different nodal values)
For the Lax Wendroff Method(figure 18 and 20) the Maximum value of the error
comes out to be around 0.6 for c = 0.3. This means there is 6% error in the peak
values of the solution. The diffusion error in the second term for the Lax Wendroff
method in quite less with the maximum size of 0.06. But these values are not perhaps
inconsistent to our solution obtained as shown in figure 19 and 20. By changing the
frequency or the wave number of the equation , the meshing requirement will
change. We need to have, may be , more refined mesh for proper solution using the
central difference method for calculating the derivative u
xx
, u
xxx
, u
xxxx
.
25  P a g e
Figure 19 (Figure showing distribution of error in the Lax Method for c = 0.6 at different nodal values)
Figure 20( Figure showing distribution of error in the Lax Wendroff Method for c = 0.6 , n=3 at different nodal values)
26  P a g e
Figure 21( Figure showing distribution of error in the Lax and Lax Wendroff Method for c = 1 at different nodal values)
We can observe that for c = 1(figure 21) , there is no error in the Lax as well as Lax
Wendroff methods. When the value of n is high , subsequently we get large number
of waves in the same domain due to increased frequency. May be we require a finer
mesh or adaptivity so that the error analysis can be done accurately. Also the
accuracu for the Lax method suffers at low value of c since lax method have high
dissipative nature and dampens the abrupt changes.
4.5 Exploring the Heaviside Initial Condition
In this case we are provided with the initial condition which contain Heaviside
function. The initial boundary condition is u
o
(x)=1H(x  0.5), where H is the Heaviside
function. Hence initially we have a distribution of 1 over the length upto 0.5 and then
it drps with a sudden step to zero value. Taking these initial values for running the Lax
and Lax Wendroff equation , we get the following figure 22.
27  P a g e
Figure 22 (Figure showing the solution for the wave equation for c =0.3, using the lax, Lax Wendroff and Analytical)
From Figure 22 , we can observe that lax method follows a wavy path , but for the
LaxWendrof we get the wiggles in the solution known as dispersion error. The wiggles
reaches upto 1.2 as the maximum dispersion error. Also dissipation error in
approaching the straight step line for the Heaviside function is observed. Dispersion
error occurs close to the area of sudden step change in the analytical function. Here
wiggles produce an error of 20% in the solution at certain nodes.
For the Lax method we did not observe any wiggles, but it lacks accuracy. For the
maximum height of the step function to be 1, Lax method yields the value of around
0.92, hence creating and error of 8%. Also by using the lax method , sudden step
change in the Heaviside function are not matched. It seems to be more or less like a
28  P a g e
smooth function like sinusoidal function rather than a cyclic function and dampens
the sharp changes in that happen in the wave.
Figure 23(Figure showing the solution for the wave equation for c =0.6, using the lax, Lax Wendroff and Analytical)
By using the value of c = 0.6 (figure 23), we can see a considerable change in the
accuracy of the solution. The wiggle jump in the Lax Wendroff function jumps upto
1.12 compared to 1.2 when we used c = 0.3.
Also the accuracy of the Lax method is considerably increased and it touches the
height of step function that is 1 and maintain the trajectory with dissipation error.
29  P a g e
The dissipation error in the Lax Wendroff method is considerably less than that for
the Lax method. At the same time Lax method doesn’t suffers from the wiggles that
appears in the Lax Wendroff method. In lax method , when c = 0.6, the the step
height is achieved by travelling the length of 0.5, which the step function achieves
instantly.
Figure 24(Figure showing the solution for the wave equation for c =1, using the lax, Lax Wendroff and Analytical)
When the value of c = 1(figure 24), both the Lax and 1 step Lax Wendrof method
achieves a quite accurate trajectory of the step function. Also there are no wiggles or
dissipation errors in the plot. For comparison of Lax and Lax Wendroff method for
different values of c in the same plot see Appendix.
30  P a g e
Figure 25( Error Distribution for the Lax method using the value of c = 0.3)
The error pattern(figure 25) for the first error term in the Lax method appears as the
wiggles like a sawtooth wave.The second error term in the Lax method appears
almost reaching zero. The error at the center for some nodes reaches to the max
value of around 1.2 i.e., 120% error in the solution compared to the zero. Total error
(Diffusion error = Dispersion error + Dissipation error) is almost matching with the
dispersion error because diffusion error is almost negligible in magnitude.
31  P a g e
Figure 26 ( Error Distribution for the Lax Wendroff method using the value of c = 0.3)
Figure 26 shows the pattern of error using the Lax Wendroff method and is found
random as compared to the Lax method. We get this uneven distribution of errors
due to the presence of dispersive error. Both the 1st and the 2nd term error in the
Lax Wendroff wiggles. An error of 0.9 occurs at certain nodes. This wiggle behavior
occurs due to the reason the at the drooping or the sudden rise of step in the step
function the wave speed is quite high , hence numerical method (lax wendroff) is
unable to match with that speed for low value of courant number. The Lax method is
too irresponsive to the abrupt changes in the wave propagation and dampens the
solution.
32  P a g e
Figure 27( Error Distribution for the Lax method using the value of c = 0.6)
For the value of c = 0.6 (figure 27) , the peaks for the first term on the MDE for the
Lax method, we get the maximum error of 1.1, which is 10% higher than the
analytical solution. But as the value of c is increased , the error in the 2nd term of the
MDE gets noticeable peaks, rising with the error of as high as 0.2, that is 2 % of the
error relative to the scale of 1.
33  P a g e
Figure 28( Error Distribution for the Lax Wendroff method using the value of c = 0.6)
For the lax Wendroff scheme, the only problem that appears are the wiggles(as
shown in figure 28) that create unnecessary and uneven noise in the solution. We get
the peaks as high as upto 0.5 creating an error ranging from 0% to 50%.
34  P a g e
Figure 29( Error Distribution for the Lax Wendroff method using the value of c = 1)
For the value of c = 1(figure 29), we get the error to be zero for the both the Lax and
the Lax Wendroff first and second terms. This is due to the fact the the value of c is
either subtracted from 1 or vice versa. Hence at c = 1 , good accuracy is achieved for
both the methods.
35  P a g e
4.6 Conclusion and Discussion
In this project we studied the behavior of tho explicit schemes – Lax and one step Lax
Wendroff method. We observed that the accuracy for the lax wendroff is quite good
at even at low value of c(courant number). The Lax methods has very low response
factor for the abrupt changes in the wave propagation. Mainly it is irresponsive to the
abrupt changes in the signals and dampen it. But contrary to this the Lax Wendroff
method have quite sensitive behavior for the abrupt changes in the boundary. For
this reason we observed high dispersive error during the solution for the Heaviside
boundary condition. The main reason for this different behavior in the two methods
is because of the dispersive leading truncation error in the 1step Lax Wendroff
method and diffusive nature of leading truncation error in Lax method. Also these
differences creates phase difference in the two methods. In lax , the error have the
phase difference of π for the sinusoidal periodic boundary and phase difference of
π/2 in the Lax Wendroff method. By increasing the value of wave number n, Lax
method was giving almost zero response since lax method is irresponsive to abrupt
changes and increased frequency. So the main points to be concluded from this
project are that by increasing the value of the courant number, accuracy of the
numerical technique(Lax and Lax Wendroff) increases. For the step changes the lax
wendroff shows dissipative nature. For such changes lax method shows quite
damping behavior.
36  P a g e
5 Appendix
5.1 Matlab Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name : Mandeep Singh %
% Project 3 (1D Wave Equation) %
% Assigned: 2/10/11 Due: 2/24/11 %
% MAE 542 Engineering Applications of Computational Fluid Dynamics %
% Lax & Lax Wendroff method for solving 1D Wave Equation %
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% given
% Equation du/dt + a (du/dx) = 0
% Wave velocity a = 1
% given the initial boundary condition
% uo=sin(2 pi n x)
% and uo= 1  H(x0.5)
%%
clear all
close all
clc
%% Inputs:
t=myinput('Please Enter maximum time for iternation(t=0.5):',0.5);
dt=myinput('Please Enter time step dt(dt=0.0075):',0.0075);
dx=myinput('Please Enter the value of space step dx(dx=0.025):',0.025);
n=myinput('Please Enter the number of waves(n=1):',1);
L=myinput('Please Enter the lenght (L=1):',1);
a=myinput('Please Enter the value of waves speed a(a=1):',1);
BC=myinput('Please Enter Bound cond to used 1. Sin funtn 2.Heaviside
Funtn:(1)',1);
nt=round(((t/dt)+1));%Time steps number
nx= (L/dx)+1;%Space steps number
x=0:dx:L;
if BC==1
uo=sin(2*pi*n*x);%initial boundary condition
else
for i=1:nx
if i==1i==nx
uo(i)=0.5;
else
uo(i)= 1heaviside(x(i)0.5);%initial boundary condition
end
end
end
u=zeros(nx,nt);%Initializing the
37  P a g e
% lax method
u(:,1)=uo;
c=a*dt/dx;
for i=1:nt %0:length(t)
for j=1:nx %0:length(x)
if j==1
u(j,i+1)= ((u(j+1,i)+u(nx1,i))/2)(c*(u(j+1,i)u(nx1,i))/2);
else if j==nx
u(j,i+1)= ((u(2,i)+u(j1,i))/2)(c*(u(2,i)u(j1,i))/2);
else
u(j,i+1)= ((u(j+1,i)+u(j1,i))/2)(c*(u(j+1,i)u(j1,i))/2);
end
end
end
end
% Lax Wendroff Method
U=zeros(nx,nt);
U(:,1)=uo;
for i=1:nt %0:length(t)
for j=1:nx %0:length(x)
if j==1
U(j,i+1)= U(j,i)c*((U(j+1,i)U(nx1,i))/2)+c^2*((U(j+1,i)
2*U(j,i)+U(nx1,i))/2);
else
if j==nx
U(j,i+1)= U(j,i)c*((U(2,i)U(j1,i))/2)+c^2*((U(2,i)2*U(j,i)+U(j
1,i))/2);
else
U(j,i+1)= U(j,i)c*((U(j+1,i)U(j1,i))/2)+c^2*((U(j+1,i)
2*U(j,i)+U(j1,i))/2);
end
end
end
end
titer = ceil(linspace(0,t,nt));% breaking time into steps
%%
C1=(a*dx/2)*((1/c)c);
C2=(a*(dx^2)/3)*(1(c^2));
C3=(a*(dx^2)/6)*(1(c^2));
C4=(a*(dx^3)/8)*c*((c^2)1);
%% 
% For error in the Lax and LaxWendroff method
% we use the last matrix for u after t time
% for calculating the error
38  P a g e
%% 
%% Error term for the du^2/dx^2  using values for Lax
% Numerical Error term for the du^2/dx^2  analytical
for m = 1:nx
if m==1
Lerxx(m,:)=C1*((u(m+1,nt)2*u(m,nt)+u(nx1,nt))/(dx^2));
elseif m==nx
Lerxx(m,:)=C1*((u(2,nt)2*u(m,nt)+u(m1,nt))/(dx^2));
else
Lerxx(m,:)=C1*((u(m+1,nt)2*u(m,nt)+u(m1,nt))/(dx^2));
end
end
%% Error term for the du^3/dx^3  analytical for Lax
for k = 1:nx
if k==1
Lerxxx(1,:)=C2*((u(k+2,nt)+2*u(k+1,nt)2*u(nx1,nt)+u(nx
2,nt))/(2*dx^3));
elseif k==2
Lerxxx(k,:)=C2*((u(k+2,nt)+2*u(k+1,nt)2*u(k1,nt)+u(nx
1,nt))/(2*dx^3));
elseif k==nx1
Lerxxx(k,:)=C2*((u(2,nt)+2*u(k+1,nt)2*u(k1,nt)+u(k2,nt))/(2*dx^3));
elseif k==nx
Lerxxx(k,:)=C2*((u(3,nt)+2*u(2,nt)2*u(k1,nt)+u(k2,nt))/(2*dx^3));
else
Lerxxx(k,:)=C2*((u(k+2,nt)+2*u(k+1,nt)2*u(k1,nt)+u(k
2,nt))/(2*dx^3));
end
end
%% Error term for the du^3/dx^3  analytical for Lax Wendroff
for k = 1:nx
if k==1
LWErxxx(k,:)=C3*((U(k+2,nt)+2*U(k+1,nt)2*U(nx1,nt)+U(nx
2,nt))/(2*dx^3));
elseif k==2
LWErxxx(k,:)=C3*((U(k+2,nt)+2*U(k+1,nt)2*U(k1,nt)+U(nx
1,nt))/(2*dx^3));
elseif k==nx1
LWErxxx(k,:)=C3*((U(2,nt)+2*U(k+1,nt)2*U(k1,nt)+U(k
2,nt))/(2*dx^3));
elseif k==nx
LWErxxx(k,:)=C3*((U(3,nt)+2*U(2,nt)2*U(k1,nt)+U(k2,nt))/(2*dx^3));
else
LWErxxx(k,:)=C3*((U(k+2,nt)+2*U(k+1,nt)2*U(k1,nt)+U(k
39  P a g e
2,nt))/(2*dx^3));
end
end
%% Error term for the du^4/dx^4  analytical for Lax Wendroff
for l = 1:nx
if l==1
LWerxxxx(l,:)=C4*((U(l+2,nt)4*U(l+1,nt)+6*U(l,nt)4*U(nx1,nt)+U(nx
2,nt))/(dx^4));
elseif l==2
LWerxxxx(l,:)=C4*((U(l+2,nt)4*U(l+1,nt)+6*U(l,nt)4*U(l1,nt)+U(nx
1,nt))/(dx^4));
elseif l==nx1
LWerxxxx(l,:)=C4*((U(2,nt)4*U(l+1,nt)+6*U(l,nt)4*U(l1,nt)+U(l
2,nt))/(dx^4));
elseif l==nx
LWerxxxx(l,:)=C4*((U(3,nt)4*U(2,nt)+6*U(l,nt)4*U(l1,nt)+U(l
2,nt))/(dx^4));
else
LWerxxxx(l,:)=C4*((U(l+2,nt)4*U(l+1,nt)+6*U(l,nt)4*U(l1,nt)+U(l
2,nt))/(dx^4));
end
end
%% For Analytical Solution
if BC==1
for i=1:nx
uanal(i,:)=sin(2*pi*n*((i1)*dxa*t));
end
else
for i=1:nx
if i*dx>0.25+dx&&i*dx<0.75+dx
uanal(i)=1;
elseif(i*dx==0.25+dx  i*dx==0.75+dx)
uanal(i)=0.5;
else
uanal(i)= 0;%initial boundary condition
end
end
%uanal=1heaviside(xa*t0.5);%using initial boundary condition
end
%%
laxerror=Lerxx+Lerxxx;%% Lax Error
laxWerror=LWErxxx+LWerxxxx;%% Lax Wendroff Error
%% 
%% Error term for the du^2/dx^2  analytical for Lax
if BC==1
you1=sin(2*pi*n*(xa*t));%using initial boundary condition
else
40  P a g e
you1=(uanal(:,nx))';%using initial boundary condition
end
% Error term for the du^2/dx^2  analytical
for m = 1:nx
if m==1
erxx(m)=C1*((you1(m+1)2*you1(m)+you1(nx1))/(dx^2));
elseif m==nx
erxx(m)=C1*((you1(2)2*you1(m)+you1(m1))/(dx^2));
else
erxx(m)=C1*((you1(m+1)2*you1(m)+you1(m1))/(dx^2));
end
end
%% Error term for the du^3/dx^3  analytical for Lax
if BC==1
you2=sin(2*pi*n*(xa*t));%using initial boundary condition
else
you2=uanal(:,nt);%using initial boundary condition
end
for k = 1:nx
if k==1
erxxx(k)=C2*((you2(k+2)+2*you2(k+1)2*you2(nx1)+you2(nx
2))/(2*dx^3));
elseif k==2
erxxx(k)=C2*((you2(k+2)+2*you2(k+1)2*you2(k1)+you2(nx1))/(2*dx^3));
elseif k==nx1
erxxx(k)=C2*((you2(2)+2*you2(k+1)2*you2(k1)+you2(k2))/(2*dx^3));
elseif k==nx
erxxx(k)=C2*((you2(3)+2*you2(2)2*you2(k1)+you2(k2))/(2*dx^3));
else
erxxx(k)=C2*((you2(k+2)+2*you2(k+1)2*you2(k1)+you2(k2))/(2*dx^3));
end
end
%% Error term for the du^3/dx^3  analytical for Lax Wendroff
if BC==1
you3=sin(2*pi*n*(xa*t));%using initial boundary condition
else
you3=uanal(:,nt);%using initial boundary condition
end
for k = 1:nx
if k==1
Erxxx(k)=C3*((you3(k+2)+2*you3(k+1)2*you3(nx1)+you3(nx
2))/(2*dx^3));
elseif k==2
Erxxx(k)=C3*((you3(k+2)+2*you3(k+1)2*you3(k1)+you3(nx1))/(2*dx^3));
elseif k==nx1
Erxxx(k)=C3*((you3(2)+2*you3(k+1)2*you3(k1)+you3(k2))/(2*dx^3));
elseif k==nx
Erxxx(k)=C3*((you3(3)+2*you3(2)2*you3(k1)+you3(k2))/(2*dx^3));
else
Erxxx(k)=C3*((you3(k+2)+2*you3(k+1)2*you3(k1)+you3(k2))/(2*dx^3));
end
end
%% Error term for the du^4/dx^4  analytical for Lax Wendroff
41  P a g e
if BC==1
you4=sin(2*pi*n*(xa*t));%using initial boundary condition
else
you4=uanal(:,nt);%using initial boundary condition
end
for l = 1:nx
if l==1
erxxxx(l)=C4*((you4(l+2)4*you4(l+1)+6*you4(l)4*you4(nx1)+you4(nx
2))/(dx^4));
elseif l==2
erxxxx(l)=C4*((you4(l+2)4*you4(l+1)+6*you4(l)4*you4(l1)+you4(nx
1))/(dx^4));
elseif l==nx1
erxxxx(l)=C4*((you4(2)4*you4(l+1)+6*you4(l)4*you4(l1)+you4(l
2))/(dx^4));
elseif l==nx
erxxxx(l)=C4*((you4(3)4*you4(2)+6*you4(l)4*you4(l1)+you4(l
2))/(dx^4));
else
erxxxx(l)=C4*((you4(l+2)4*you4(l+1)+6*you4(l)4*you4(l1)+you4(l
2))/(dx^4));
end
end
%%
Alaxerror=erxx+erxxx;%% Lax Error
ALaxWerror=Erxxx+erxxxx;%% Lax Wendroff Error
figure(1)
plot(x,uanal,'bs',x,u(:,nt),'db',x,U(:,nt),'+r')
title('Wave Propagation function u Vs Domain Lenght L','fontWeight',...
'bold','fontsize',10);
xlabel('Domain Length','fontWeight','bold','fontSize',10);
ylabel('u  wave propagation function ','fontWeight','bold','fontSize',10);
hleg = legend('Analytical','Lax','LaxWendroff',...
'Location','NorthEastOutside');
% Make the text of the legend italic and color it brown
set(hleg,'FontAngle','italic','TextColor',[.3 .2 .1])
grid on
figure (6)
plot(x,u(:,nt),'db')
hold on
figure (7)
plot(x,U(:,nt),'db')
hold on
figure(2)
plot(x,Lerxx,'bs',x,Lerxxx,'dg',x,LWErxxx,'+r',x,LWerxxxx,'.b')
title('Error Plot for 1st and 2nd error terms',...
'fontWeight','bold','fontsize',10);
xlabel('Domain Length','fontWeight','bold','fontSize',10);
42  P a g e
ylabel('Error','fontWeight','bold','fontSize',10);
hleg = legend('Lax  first Error Term','Lax  Second Error Term',...
'LaxWendroff   first Error Term ',...
'LaxWendroff   Second Error Term','Location','NorthEastOutside');
% Make the text of the legend italic and color it brown
set(hleg,'FontAngle','italic','TextColor',[.3 .2 .1])
grid on
figure(3)
plot(x,Lerxx,'bo',x,Lerxxx,'dg',x,Lerxx+Lerxxx,'r.')
title('Error Plot for 1st and 2nd error terms for Lax Method',...
'fontWeight','bold','fontsize',10);
xlabel('Domain Length','fontWeight','bold','fontSize',10);
ylabel('Error','fontWeight','bold','fontSize',10);
hleg = legend('Lax  first Error Term','Lax  Second Error Term',...
'Total Error','Location','NorthEastOutside');
% Make the text of the legend italic and color it brown
set(hleg,'FontAngle','italic','TextColor',[.3 .2 .1])
grid on
figure(4)
plot(x,LWErxxx,'bo',x,LWerxxxx,'dg',x,LWErxxx+LWerxxxx,'r.')
title('Error Plot for 1st and 2nd error terms for LaxWendroff Method',...
'fontWeight','bold','fontsize',10);
xlabel('Domain Length','fontWeight','bold','fontSize',10);
ylabel('Error','fontWeight','bold','fontSize',10);
hleg = legend('Lax Wendroff  first Error Term',...
'Lax Wendroff  Second Error Term',...
'Total Error','Location','NorthEastOutside');
% Make the text of the legend italic and color it brown
set(hleg,'FontAngle','italic','TextColor',[.3 .2 .1])
grid on
figure(5)
plot(x,erxx,'bs',x,erxxx,'dg',x,Erxxx,'+r',x,erxxxx,'.b')
title('Error Plot for 1st and 2nd error terms using analytical',...
'fontWeight','bold','fontsize',10);
xlabel('Domain Length','fontWeight','bold','fontSize',10);
ylabel('Error','fontWeight','bold','fontSize',10);
hleg = legend('Lax  first Error Term','Lax  Second Error Term',...
'LaxWendroff   first Error Term ',...
'LaxWendroff   Second Error Term','Location','NorthEastOutside');
% Make the text of the legend italic and color it brown
set(hleg,'FontAngle','italic','TextColor',[.3 .2 .1])
grid on
43  P a g e
5.2 Heaviside Wave form plot for c = 0.3, 0.6 , 1 and t = 0.25
Figure 30(Plot for comparing the Lax method at different value of c)
Figure 31 (Plot for comparing the LaxWendroff method at different values of c)
44  P a g e
6 References
1. Lecture notes by Prof. Desjardin
2. Hoffman & Chian, Computational Fluid Dynamics VolI
This action might not be possible to undo. Are you sure you want to continue?