Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. clc, clear all, close all
  2.  
  3. U = 1; % vecloity of fluid m/s
  4. Lmin = 0; % 0 m
  5. Lmax = 1; % 1 m
  6. n = 100; % nodes/points
  7. dx = Lmax/n; % step size [m]
  8. r = 0.5; % Courant number for stability
  9. t = 0; % initial time
  10. tmax = 1; % time [s]
  11.  
  12. % for lax wendroff/friedrich to be stable, r < 1, we have now r = 0.5
  13. dt = (r*dx)/U; % time step, r = U*dt/dx
  14. kdiff = 0.5*(U^2)*dt;
  15. pausetime = dt; % Pause time between animation
  16.  
  17. for k = 1:((tmax/dt)+1)
  18. t(k) = (k-1)*dt; % saving time steps, starting with t=0
  19. phi(1,k) = -sin(2*pi*t(k)); % left boundary condition phi(x=0,t)
  20. phi_LW(1,k) = phi(1,k); % lax-wendroff
  21. end
  22.  
  23. for l = 1:((Lmax/dx)+1)
  24. x(l) = (l-1)*dx; % saving spatial steps, starting with x = 0
  25. phi(l,1) = sin(2*pi*x(l)); % initial solution phi(x,t=0)
  26. phi_LW(l,1) = phi(l,1); % lax-wendroff
  27. end
  28.  
  29. for k = 1:(tmax/dt)
  30. for l = 2:(Lmax/dx) % start from 2 since 1 is boundary cond.
  31. phi(l,k+1) = 0.5*(phi(l+1,k)+phi(l-1,k)) - (r/2)*(phi(l+1,k)-phi(l-1,k));
  32. 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));
  33. end
  34. pause(pausetime) % Shows results for each timestep
  35. %plot(x(1:end),phi(1:end,k),'r'); % lax-friedrich, NEED TO UNCOMMENT FOR PLOT
  36. plot(x(1:end),phi_LW(1:end,k),'r'); % lax-wendroff, NEED TO UNCOMMENT FOR PLOT
  37. xlabel('x [m]')
  38. ylabel('U [m/s]')
  39. title('The temperature distribution in real time')
  40. end
  41.  
  42. for k = 1:101
  43. exact_solution(k) = sin(2*pi*k*dx); % exact solution at t = 1 s
  44. end
  45.  
  46. hold on
  47. xlabel('x [m]')
  48. ylabel('U [m/s]')
  49. title('Temperature distribution at t = 1s')
  50. plot(x,phi(1:end,end),'r') % plot lax-fried. against x for t = 1 s
  51. plot(x,phi_LW(1:end,end),'g')
  52. plot(x,exact_solution(1:end),'b') % plot exact solution fo t = 1 s
  53. legend('Lax-Friedrich','Law-Wendroff','Exact solution')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement