Plotting the heat equation using the explicit method
古いコメントを表示
Hi, I am supposed to use the explicit method to plot an approximation of the heat equation in Matlab. The heat equation is as follows:
du/dx=d^2u/d^2x (u_t=u_xx).
Initial conditions: u(x,0)=1 if x>0, while if x is equal or greater than zero u(x,0)=0.
The explicit method is the following: u(j,m+1) =r*u(j-1,m) + (1-2*r)*u(j,m)+r*u(j+1,m), where u is the solution, j is which x-value while m is which time value it is.
The matlab-code is the following:
L = 2.; % Length of the bar
T =0.1; % Time
maxm = 2000; % Time steps
dt = T/maxm;
n = 70; % Distance steps
dx = L/n;
r = dt/(dx^2); % Stability parameter, r less or equal to 1/2
for j=1:n+1
x(j)=-10+(j-1)*dx; %Because j actually starts at zero
if x> 0
u(j,1)=1;
else
u(j,1)=0;
end
end
for m=1:maxm % Time Loop
u(1,m)=0;
u(n+1,m)=1;
for j=2:n; % Space Loop
u(j,m+1) =r*u(j-1,m) + (1-2*r)*u(j,m)+r*u(j+1,m);
end
end
figure(2)
plot(x,u(:,1))
I am not sure what is wrong. When I plot this, it equals zero for all x-values. It was supposed to be equal to 1 when x>0, while zero when x was less than zero. Can anyone help me to figure out was is wrong with this Matlab-code?
David
10 件のコメント
Torsten
2015 年 3 月 6 日
What do you expect if you set u=0 at both boundaries ?
Best wishes
Torsten.
David
2015 年 3 月 6 日
Torsten
2015 年 3 月 6 日
You still use u=0 at both boundaries. You will see this if you look at u(:,1).
For the error message, I don't have an explanation. Maybe the wrong semicolon behind "for j=2:n".
Best wishes
Torsten.
Torsten
2015 年 3 月 6 日
Why do you use a certain length for a bar ?
According to the problem formulation, you should solve the heat equation over (-oo;oo). Because the solution will be antisymmetric around x=0, you should choose the solution interval to be [-10:10], e.g. with boundary value 0 at x=-10 and 1 at x=10.
Best wishes
Torsten.
David
2015 年 3 月 6 日
Torsten
2015 年 3 月 6 日
>Yes, that is absolutely correct, but how do you choose negative x-values as >well? When I try to do that by setting j to go from negative n to n, I get >an error message which says: "index must be a positive integer or logical."
Choose L=20 and the loop as
for j=1:n+1
x(j)=-10+(j-1)*dx; %Because j actually starts at zero
if x(j)> 0
u(j,1)=1;
else
u(j,1)=0;
end
end
>About the boundary values. Are you sure I am "allowed" to choose boundary values 0 at x=-10 and 1 at x=10? Since I am only given two initial conditions from the text, but no boundary conditions when t not equal to zero.
You will have to choose the x-interval long enough in positive and negative direction such that during the time of integration, the solution at the boundary remains constant. And don't forget to set the boundary values for u in time step m:
for m=1:maxm % Time Loop
u(1,m)=0;
u(n+1,m)=1;
for j=2:n
...
Best wishes
Torsten.
Torsten
2015 年 3 月 6 日
For each time step, the value of u at the left and at the right boundary has to be fixed - thus u(1,m)=0 and u(n+1,m)=1 is not only possible, it is necessary.
And what do you mean by "(Because it is not supposed to be zero at x=0 when m not equal to zero)." u is not zero at x=0, but at x=-10.
You can easily check whether your implementation is correct by comparing with the analytical solution. It is given by
u(x,t)=0.5*(2-erfc(x/(2*sqrt(t))))
where erfc is the complementary error function (available in MATLAB).
Best wishes
Torsten.
David
2015 年 3 月 7 日
採用された回答
その他の回答 (1 件)
Titus Edelhofer
2015 年 3 月 6 日
Hi,
when you set the value for u, you need to replace
if x>0
by
if x(j)>0
Then you will see, that your u looks like you want instead of identical zero.
Titus
3 件のコメント
David
2015 年 3 月 6 日
Titus Edelhofer
2015 年 3 月 6 日
Maybe you should describe first in more detail what the problem is. Having a single point with a value different from the others doesn't sound good to me. Suppose you would refine the grid, then the proportion of the "bar" having u=0 instead of u=1 would go further down ...?
David
2015 年 3 月 6 日
カテゴリ
ヘルプ センター および File Exchange で Exponents and Logarithms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!