

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


% script to call fwave hx = 1/10; ht = 1/10; nx = 16; nt = 40; c = 1; t=0:nt; hib = zeros(nt + 1, 1); lowb = zeros(nt + 1, 1); lowb(2:5, 1) = 10; init = zeros(1, nx + 1); initslope = zeros(1, nx + 1); u = fwave(nx, hx, nt, ht, init, initslope, lowb, hib, c); surfl(u) axis([0 16 0 40 -10 10]) xlabel('position along string'); ylabel('time'); zlabel('vertical displacement'); function u = fwave(nx, hx, nt, ht, init, initslope, lowb, hib, c) alpha = c * ht / hx; u = zeros(nt + 1, nx + 1); u(:, 1) = lowb; u(:, nx + 1) = hib; u(1, :) = init; for i = 2:nx, u(2, i) = alpha^2 * (init(i + 1) + init(i - 1))/2 + (1 - alpha^2) * init(i) + ht * initslope(i); end for j = 2:nt, for i = 2:nx, u(j + 1, i) = alpha^2 * (u(j, i + 1) + u(j, i - 1)) + (2 - 2 * alpha^2) * u(j, i) - u(j - 1, i); end end
above – result of original file below – result after doubling the spatial sampling