1-D Heat equation

4 次查看(过去 30 天)
Dereje
Dereje 2018-3-22
评论: Dereje 2018-3-22
I have no idea what went wrong. Two days since looking for the errors, please help
if
close all
x_min= 0;
x_max = 1;
N = 10;
L = x_max-x_min;
h = L/N;
x = linspace(x_min,x_max,N+1)'
uexact= @(x) 4*(x-x.^2);
f = @(x) 8*15; % Source term
u_exact = uexact(x);
ua = uexact(x_min);
ub = uexact(x_max);
u = zeros(N-1,1);
A = zeros(N-1,N-1);
b = f(x(2:N)); %
b(1) = b(1) + ua/h^2;
b(N-1) = b(N-1) + ub/h^2;
dA = diag( 2*ones(1,N-1) );
dAp1 = diag( -1*ones(1,N-2), 1 );
dAm1 = diag( -1*ones(1,N-2), -1 );
A = (dA + dAp1 + dAm1);
A = 15*A/h^2;
u = A\b; % solving the linear system
g = [ua; u; ub];
plot(x,u_exact,'b',x,g,'ro-');
legend('exact','numerical');
Thanks for the help.

采纳的回答

Birdman
Birdman 2018-3-22
x_min= 0;
x_max = 1;
N = 10;
L = x_max-x_min;
h = L/N;
x = linspace(x_min,x_max,N+1)'
uexact= @(x) 4*(x-x.^2);
f = @(x) 8*15*ones(1,N-1); % Source term
u_exact = uexact(x);
ua = uexact(x_min);
ub = uexact(x_max);
u = zeros(N-1,1);
A = zeros(N-1,N-1);
b = f(x(2:N)); %
b(1) = b(1) + ua/h^2;
for i=2:N-1
b(i) = b(i) + ub/h^2;
end
dA = diag( 2*ones(1,N-1) );
dAp1 = diag( -1*ones(1,N-2), 1 );
dAm1 = diag( -1*ones(1,N-2), -1 );
A = (dA + dAp1 + dAm1);
A = 15*A/h^2;
u = A\b.'; % solving the linear system
g = [ua; u; ub];
plot(x,u_exact,'b',x,g,'ro-');
legend('exact','numerical');
  8 个评论
Birdman
Birdman 2018-3-22
This looks like a new question. Ask it to the forum as a new question so more people can contribute.
Dereje
Dereje 2018-3-22
Ok, I will do that.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by