Jeremy Knight Math 639 5/5/11 Algorithm 12.4: Wave Equation Finite Difference
<Text-field style="Heading 1" layout="Heading 1">WaveEqFiniteDiff(f,g,l,T,alpha,m::integer,N::integer)</Text-field> To approximate the solution to the wave equation LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzZDLUkmbWZyYWNHRiQ2KC1GIzYlLUklbXN1cEdGJDYlLUkjbWlHRiQ2JVEnJiM5NDg7RicvJSdpdGFsaWNHUSZmYWxzZUYnLyUsbWF0aHZhcmlhbnRHUSdub3JtYWxGJy1GIzYlLUkjbW5HRiQ2JFEiMkYnRjovRjhRJXRydWVGJy9GO1EnaXRhbGljRicvJTFzdXBlcnNjcmlwdHNoaWZ0R1EiMEYnLUY0NiVRInVGJ0ZDRkVGOi1GIzYlLUYxNiUtRjQ2JVEoJiM5NDg7dEYnRkNGRUY9RkdGQ0ZFLyUubGluZXRoaWNrbmVzc0dRIjFGJy8lK2Rlbm9tYWxpZ25HUSdjZW50ZXJGJy8lKW51bWFsaWduR0ZZLyUpYmV2ZWxsZWRHRjktSShtZmVuY2VkR0YkNiQtRiM2Ji1GNDYlUSJ4RidGQ0ZFLUkjbW9HRiQ2LVEiLEYnRjovJSZmZW5jZUdGOS8lKnNlcGFyYXRvckdGRC8lKXN0cmV0Y2h5R0Y5LyUqc3ltbWV0cmljR0Y5LyUobGFyZ2VvcEdGOS8lLm1vdmFibGVsaW1pdHNHRjkvJSdhY2NlbnRHRjkvJSdsc3BhY2VHUSYwLjBlbUYnLyUncnNwYWNlR1EsMC4zMzMzMzMzZW1GJy1GNDYlUSJ0RidGQ0ZFRjpGOi1GYW82LVEoJm1pbnVzO0YnRjpGZG8vRmdvRjlGaG9Gam9GXHBGXnBGYHAvRmNwUSwwLjIyMjIyMjJlbUYnL0ZmcEZgcS1GMTYlLUY0NiVRJyYjOTQ1O0YnRjdGOkY9RkctRmFvNi1RIn5GJ0Y6RmRvRl5xRmhv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J25vcm1hbEYnLyUmZmVuY2VHRkcvJSpzZXBhcmF0b3JHRjQvJSlzdHJldGNoeUdGRy8lKnN5bW1ldHJpY0dGRy8lKGxhcmdlb3BHRkcvJS5tb3ZhYmxlbGltaXRzR0ZHLyUnYWNjZW50R0ZHLyUnbHNwYWNlR1EmMC4wZW1GJy8lJ3JzcGFjZUdRLDAuMzMzMzMzM2VtRictSSNtbkdGJDYkUSIwRidGVEZURlQtRlE2LVEiPUYnRlRGVi9GWUZHRlpGZm5GaG5Gam5GXG8vRl9vUSwwLjI3Nzc3NzhlbUYnL0Zib0ZdcC1GLzYlUSJnRidGMkY1LUZJNiQtRiM2JEZNRlRGVEZU for LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYoLUkjbW5HRiQ2JFEiMEYnLyUsbWF0aHZhcmlhbnRHUSdub3JtYWxGJy1JI21vR0YkNi1RJiZsZXE7RidGLy8lJmZlbmNlR1EmZmFsc2VGJy8lKnNlcGFyYXRvckdGOC8lKXN0cmV0Y2h5R0Y4LyUqc3ltbWV0cmljR0Y4LyUobGFyZ2VvcEdGOC8lLm1vdmFibGVsaW1pdHNHRjgvJSdhY2NlbnRHRjgvJSdsc3BhY2VHUSwwLjI3Nzc3NzhlbUYnLyUncnNwYWNlR0ZHLUkjbWlHRiQ2JVEieEYnLyUnaXRhbGljR1EldHJ1ZUYnL0YwUSdpdGFsaWNGJ0YyLUZLNiVRImxGJ0ZORlFGLw== WaveEqFiniteDiff:=proc(f,g,l,T,alpha,m::integer,N::integer) #f,g = f(x),g(x) functions from equation #l = maximum x from equation parameters #T = maximum time #alpha = equation parameter #m = x-value subintervals #N = time subintervals local i,j,k,w,h,lambda,t,x: #option trace: w:=Array(0..m,0..N): h:=l/m: k:=T/N: lambda:=k*alpha/h: for j from 1 to N do w[0,j]:=0: w[m,j]:=0: end do: w[0,0]:=f(0): w[m,0]:=f(l): #initialize for t=0, and t=k for i from 1 to m-1 do w[i,0]:=f(i*h): w[i,1]:=(1.0-lambda^2)*f(i*h)+(lambda^2)/2.0(f((i+1.0)*h)+f(i-1.0)*h)+k*g(i*h): end do: #perform matrix multiplication for j from 1 to N-1 do for i from 1 to m-1 do w[i,j+1]:=2.0*(1.0-lambda^2)*w[i,j]+lambda^2*(w[i+1,j]+w[i-1,j])-w[i,j-1] end do: end do: for j from 0 to N do t:=j*k: for i from 0 to m do x:=i*h: print(x,t,w[i,j]): end do: end do: end proc: