@Spancymath
2016-06-23T16:14:15.000000Z
字数 988
阅读 621
数值解
求解一维抛物方程的初边值问题:
clc
clear
xb = [0, pi]; %boundary of x
m = input('input the grid number of x:');
n = input('input the grid number of t:');
h = (xb(2)-xb(1))/m; %step value of x
k = 1/n; %step value of time
r = k/h^2; %ratio of grid
u = zeros(n+1, m+1); % the value of every grid
x = h*((1:m+1)-1);
u(1, :) = sin(x); %initial
u(:, 1) = 0; %boundary condition
u(:, end) = 0;
%A = zeros(m-2, 3); %the coefficient matrix of equation
d = zeros(1, m-1);
g = zeros(1, m-1);
w = zeros(1, m-1);
alpha = r/2;
beta = 1+r;
gamma = r/2;
for N = 2:n+1
for M = 2:m
ite = M-1;
d(ite) = r/2*(u(N-1, M-1)+u(N-1, M+1))+(1-r)*u(N-1, M);
if ite==1 %zhui
g(1) = d(1)/beta;
w(1) = gamma/beta;
else
g(ite) = (d(ite)+alpha*g(ite-1))/(beta-alpha*w(ite-1));
w(ite) = gamma/(beta-alpha*w(ite-1));
end
end
for M = m:-1:2 %gan
ite = M-1;
if M==m
u(N, M) = g(ite);
else
u(N, M) = g(ite)+w(ite)*u(N, M+1);
end
end
end
X = xb(1):h:xb(2);
T = 0:k:n*k;
[X, T] = meshgrid(X, T);
surf(X, T, u);