solve ODE using finite difference method

8 ビュー (過去 30 日間)
Emon Baroi
Emon Baroi 2023 年 2 月 8 日
回答済み: SAI SRUJAN 2024 年 8 月 13 日
du/dt=d^2u/dx^2

回答 (2 件)

Torsten
Torsten 2023 年 2 月 8 日
This is a PDE, not an ODE.
Use "pdepe" to solve.

SAI SRUJAN
SAI SRUJAN 2024 年 8 月 13 日
Hi Emon,
I understand that you are trying to solve a pde using finite difference method.
Refer to the following code sample to proceed further,
Nx = 50; % Number of spatial points
Nt = 1000; % Number of time steps
% Discretization
dx = 1 / (Nx - 1);
dt = 0.1 / Nt;
x = linspace(0, 1, Nx);
t = linspace(0, 0.1, Nt);
% Stability condition
if dt > dx^2 / (2)
error('Time step is too large for stability.');
end
% Initial condition
u = sin(pi * x);
% Time-stepping loop
for n = 1:Nt
u_new = u;
for i = 2:Nx-1
% Finite difference approximation
u_new(i) = u(i) +dt / dx^2 * (u(i+1) - 2*u(i) - u(i-1));
end
u = u_new;
end
plot(x, u);
xlabel('x');
ylabel('u');
title('Solution of the PDE \partial u/\partial t = \partial^2 u/\partial x^2');
I hope this helps!

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by