フィルターのクリア

How to solve method of lines on one-dimensional heat equation using Euler's method?

27 ビュー (過去 30 日間)
I have tried but I couldn't solve it fully. Here i attach my code and the question.
% Parameters
k = 0.5;
% Evaluate BC
N = 3;
u(0)= 0;
u(1:N)= U;
u(N+1)=0;
h = 1/(N+1) ; %step size
%Initial condition
for i=1:N
u(i)=sin(pi*i);
end
% Define du/dt
dudt = zeros (N+1,1);
for i=1:N
dudt(i)=k/h^2*(u(i-1)-2*u(i)+u(i+1));
end
  1 件のコメント
Torsten
Torsten 2023 年 1 月 3 日
編集済み: Torsten 2023 年 1 月 3 日
u(0)= 0;
Array indices start with 1, not with 0. Thus u(0) will throw an error.
u(1:N)= U;
You did not yet define U.
u(i)=sin(pi*i);
i must be replaced by x(i) if 0=x(1)<x(2)<...<x(N+2)=1 is your grid in x-direction. And using N+2 grid points, your loop limits 1 and N are wrong.
dudt = zeros (N+1,1);
You use N+2 grid points.
for i=1:N
dudt(i)=k/h^2*(u(i-1)-2*u(i)+u(i+1));
end
Loop limits are wrong.
You must advance u in time by dudt, so something like u(n+1,i) = u(n,i) + dt * dudt(i) is missing where the "n" refers to the solution at time dt*n.

サインインしてコメントする。

採用された回答

Torsten
Torsten 2023 年 1 月 3 日
編集済み: Torsten 2023 年 1 月 4 日
% Parameters
k = 0.5;
% Evaluate BC
dx = 0.01;
x = 0:dx:1;
dt = 0.0001;
t = 0:dt:0.5;
h = 1/(length(x)-1) ; %step size
u = zeros(length(t),length(x));
%Initial condition
for i=1:length(x)
u(1,i)=sin(pi*x(i));
end
for j = 1:length(t)-1
for i=2:length(x)-1
u(j+1,i) = u(j,i) + dt* k/dx^2*(u(j,i-1)-2*u(j,i)+u(j,i+1));
end
end
figure(1)
plot(x,[u(1,:);u(500,:);u(1000,:);u(2000,:);u(5000,:)])
figure(2)
surf(x,t,u,'Edgecolor','none')
  6 件のコメント
Ankitha
Ankitha 2023 年 1 月 4 日
編集済み: Ankitha 2023 年 1 月 4 日
@Torsten could you please have a look at my question as well https://de.mathworks.com/matlabcentral/answers/1888402-how-to-solve-heat-equation-using-euler-cauchy-method?s_tid=srchtitle ? iit would be really great. Thank you
Nannthini
Nannthini 2023 年 1 月 10 日
okey Thank you so much it helps me a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by