[关闭]
@Spancymath 2016-06-23T16:14:15.000000Z 字数 988 阅读 621

抛物型方程的差分方程

数值解


例1:用Crank-Nicolson格式和追赶法求解抛物型方程

求解一维抛物方程的初边值问题:


解:

  1. clc
  2. clear
  3. xb = [0, pi]; %boundary of x
  4. m = input('input the grid number of x:');
  5. n = input('input the grid number of t:');
  6. h = (xb(2)-xb(1))/m; %step value of x
  7. k = 1/n; %step value of time
  8. r = k/h^2; %ratio of grid
  9. u = zeros(n+1, m+1); % the value of every grid
  10. x = h*((1:m+1)-1);
  11. u(1, :) = sin(x); %initial
  12. u(:, 1) = 0; %boundary condition
  13. u(:, end) = 0;
  14. %A = zeros(m-2, 3); %the coefficient matrix of equation
  15. d = zeros(1, m-1);
  16. g = zeros(1, m-1);
  17. w = zeros(1, m-1);
  18. alpha = r/2;
  19. beta = 1+r;
  20. gamma = r/2;
  21. for N = 2:n+1
  22. for M = 2:m
  23. ite = M-1;
  24. d(ite) = r/2*(u(N-1, M-1)+u(N-1, M+1))+(1-r)*u(N-1, M);
  25. if ite==1 %zhui
  26. g(1) = d(1)/beta;
  27. w(1) = gamma/beta;
  28. else
  29. g(ite) = (d(ite)+alpha*g(ite-1))/(beta-alpha*w(ite-1));
  30. w(ite) = gamma/(beta-alpha*w(ite-1));
  31. end
  32. end
  33. for M = m:-1:2 %gan
  34. ite = M-1;
  35. if M==m
  36. u(N, M) = g(ite);
  37. else
  38. u(N, M) = g(ite)+w(ite)*u(N, M+1);
  39. end
  40. end
  41. end
  42. X = xb(1):h:xb(2);
  43. T = 0:k:n*k;
  44. [X, T] = meshgrid(X, T);
  45. surf(X, T, u);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注