# Category: Differential Equations
# Name: D'Alembers solution animation of a plucked string

#
# Solution to the wave equation u_tt = u_xx
# The initial conditions are: position at F(x) and velocity at 0
# Boundary conditions are u(0,t) = u(1,t) = 0
#

# A pulse function.  Feel free to set this function to whatever you want,
# though you should have ends at 0
function F(x) = (
	# This makes the functions periodic
	while x < -1 do x = x + 2;
	while x > 1 do x = x - 2;
	
	if x < 0 then
		-F(-x)
	else if x < 0.45 or x > 0.55 then
		0
	else (
		if x < 1/2 then
			20*(x-0.45)
		else			
			20*(0.55-x)
	)	
);

# A plucked string function.
#function F(x) = (
#	# This makes the functions periodic
#	while x < -1 do x = x + 2;
#	while x > 1 do x = x - 2;
#	
#	if x < 0 then -F(-x)
#	else if x < 0.5 then x
#	else 1-x	
#);

# A plucked string function in two places.
#function F(x) = (
#	# This makes the functions periodic
#	while x < -1 do x = x + 2;
#	while x > 1 do x = x - 2;
#	
#	if x < 0 then -F(-x)
#	else if x < 0.25 then -x
#	else if x < 0.5 then x-0.5
#	else if x < 0.75 then x-0.5
#	else 1-x	
#);


# A plucked string in two places both in positive directions.
#function F(x) = (
#	# This makes the functions periodic
#	while x < -1 do x = x + 2;
#	while x > 1 do x = x - 2;
#	
#	if x < 0 then -F(-x)
#	else if x < 0.25 then x
#	else if x < 0.5 then 0.5-x
#	else if x < 0.75 then x-0.5
#	else 1-x	
#);

# Examples of superposition of sin waves
#function F(x) = (1/2)*sin(2*pi*x);
#function F(x) = (1/2)*sin(2*pi*x) + (1/3)*sin(7*pi*x);
#function F(x) = (1/2)*sin(2*pi*x) + (1/3)*sin(7*pi*x) + (1/8)*sin(20*pi*x);


#t a paramater
function yy(x) = (F(x-t) + F(x+t))/2;

LinePlotDrawLegends = false;
PlotWindowPresent(); # Make sure the window is raised

for n=1 to 1000 do (
	t = n*0.005;
	LinePlot(yy,[0,1,-1.1,1.1])
)
