Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc, clear all, close all
- U = 1; % vecloity of fluid m/s
- Lmin = 0; % 0 m
- Lmax = 1; % 1 m
- n = 100; % nodes/points
- dx = Lmax/n; % step size [m]
- r = 0.5; % Courant number for stability
- t = 0; % initial time
- tmax = 1; % time [s]
- % for lax wendroff/friedrich to be stable, r < 1, we have now r = 0.5
- dt = (r*dx)/U; % time step, r = U*dt/dx
- kdiff = 0.5*(U^2)*dt;
- pausetime = dt; % Pause time between animation
- for k = 1:((tmax/dt)+1)
- t(k) = (k-1)*dt; % saving time steps, starting with t=0
- phi(1,k) = -sin(2*pi*t(k)); % left boundary condition phi(x=0,t)
- phi_LW(1,k) = phi(1,k); % lax-wendroff
- end
- for l = 1:((Lmax/dx)+1)
- x(l) = (l-1)*dx; % saving spatial steps, starting with x = 0
- phi(l,1) = sin(2*pi*x(l)); % initial solution phi(x,t=0)
- phi_LW(l,1) = phi(l,1); % lax-wendroff
- end
- for k = 1:(tmax/dt)
- for l = 2:(Lmax/dx) % start from 2 since 1 is boundary cond.
- phi(l,k+1) = 0.5*(phi(l+1,k)+phi(l-1,k)) - (r/2)*(phi(l+1,k)-phi(l-1,k));
- phi_LW(l,k+1) = phi_LW(l,k) - (r/2)*(phi_LW(l+1,k)-phi_LW(l-1,k))+ ((kdiff*dt)/(dx^2))*(phi_LW(l-1,k)-2*phi_LW(l,k)+phi_LW(l+1,k));
- end
- pause(pausetime) % Shows results for each timestep
- %plot(x(1:end),phi(1:end,k),'r'); % lax-friedrich, NEED TO UNCOMMENT FOR PLOT
- plot(x(1:end),phi_LW(1:end,k),'r'); % lax-wendroff, NEED TO UNCOMMENT FOR PLOT
- xlabel('x [m]')
- ylabel('U [m/s]')
- title('The temperature distribution in real time')
- end
- for k = 1:101
- exact_solution(k) = sin(2*pi*k*dx); % exact solution at t = 1 s
- end
- hold on
- xlabel('x [m]')
- ylabel('U [m/s]')
- title('Temperature distribution at t = 1s')
- plot(x,phi(1:end,end),'r') % plot lax-fried. against x for t = 1 s
- plot(x,phi_LW(1:end,end),'g')
- plot(x,exact_solution(1:end),'b') % plot exact solution fo t = 1 s
- legend('Lax-Friedrich','Law-Wendroff','Exact solution')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement