hw6_complete - Kham Lee HW 6 CS111 1. 2. Make_Movie = 1; D...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Kham Lee HW 6 CS111 1. 2. Make_Movie = 1; D = .5; p = 100; t_target = 1; t = 0; dt = .1; xmin = 0; xmax = 1; dx = (xmax - xmin) / (p - 1); x = xmin:dx:xmax; A = sparse(p,p); rhs = zeros(p,1); A(1,1) = 1; rhs(1) = exp(-D * t) * cos(0); A(p,p) = 1; rhs(p) = exp(-D * t) * cos(1); u_numerical = zeros(p,1); u_exact = zeros(p,1); if Make_Movie fig = figure; aviobj = avifile('Diffusion_Model.avi'); end for i = 2: p-1 A(i,i) = 1 + 2 * D * (dt / (dx * dx)); A(i, i+1) = -D * (dt / (dx * dx)); A(i, i-1) = -D * (dt / (dx * dx)); rhs(i) = exp(-D * t) * cos(x(i)); end u_numerical = A\rhs; while (t < t_target) if (t + dt > t_target) dt = t_target - t; end rhs(1) = exp(-D * t) * cos(0); rhs(p) = exp(-D * t) * cos(1); Kham Lee HW 6 CS111 u_numerical = A\rhs; rhs = u_numerical; u_exact = exp(-D * t) * cos(x); t = t + dt; clf; hold on; axis([0 1 0 1]); plot(x, u_numerical, 'bo'); plot(x, u_exact, 'r'); legend('Numerical', 'Exact'); pause(dt/10); if Make_Movie F = getframe(fig); aviobj = addframe(aviobj,F); end end if Make_Movie close(fig); aviobj = close(aviobj); end ...
View Full Document

Page1 / 2

hw6_complete - Kham Lee HW 6 CS111 1. 2. Make_Movie = 1; D...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online