# hw_5 - U_exact(1, i) = cos(pi*(x(1,i)-c*t)); end u(1,1)=...

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

Kham Lee c = 1; %change c to 1 or -1 xmin = -1; xmax = 1; t_fin = 2; Nb_Nodes = 100; cfl = 0.5; dx = (xmax-xmin) / (Nb_Nodes-1); dt = cfl * dx; x = xmin:dx:xmax; t = 0; U_numerical = zeros(1,100); U_exact = zeros(1,100); u = cos(pi*x); u_e = cos(pi*(x-c*t)); for i = 1:100 U_numerical(1, i) = u(1, i); U_exact(1,i) = u_e(1,i); end while t<t_fin if ( (t+dt)>t_fin ) dt = t_fin - t; end t = t + dt; if (c > 0) for i = 2:(Nb_Nodes) u(1, i) = U_numerical(i) - c *(dt/dx)*(U_numerical(1,i)- U_numerical(1,i-1));

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: U_exact(1, i) = cos(pi*(x(1,i)-c*t)); end u(1,1)= u(1,Nb_Nodes-1); U_numerical = u; else for i = 1:(Nb_Nodes-1) u(1, i) = U_numerical(1,i) - c *(dt/dx)*(U_numerical(1, i+1) - U_numerical(1,i)); U_exact(1, i ) = cos(pi*(x(1,i)-c*t)); end u(1,Nb_Nodes-1)= u(1,1); U_numerical = u; end % pause(dt/10); clf; hold on ; axis( 'square' ); plot(x, U_numerical, 'bo' ); plot(x, U_exact, 'r' ); legend( 'Numerical' , 'Exact' ); pause(.01); end Kham Lee C = 1 C = -1...
View Full Document

## This note was uploaded on 02/19/2012 for the course ENGR 361 taught by Professor Drexel during the Spring '12 term at Bloomsburg.

### Page1 / 2

hw_5 - U_exact(1, i) = cos(pi*(x(1,i)-c*t)); end u(1,1)=...

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

View Full Document
Ask a homework question - tutors are online