How to code a 1D ODE in MATLAB

조회 수: 6 (최근 30일)
JUBAIR
JUBAIR 2025년 3월 26일
댓글: JUBAIR 2025년 3월 26일
Dear Community
I am trying to code the following 1D ODE using backward difference approximation:
dW/dt =U* (dW/dX) +C
I have entered the equation in MATLAB as below:
W(1) = some constant; Boundary condition at inlet
for i = 2:n
DWDt(i) = - U*((W(i)-W(i-1))/(X(i)-X(i-1)))+C ;
end
My question is: Although in my equation U*(dW/dX) is a positive term, why I need to use negative sign in the for loop? if I make the term U8 (dW/dX) positive in the for loop, the ODE23 command can't solve the equation.
I would highly appreciate if someone can answer.
  댓글 수: 1
JUBAIR
JUBAIR 2025년 3월 26일
Here U is the veloctiy term in m/s

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2025년 3월 26일
편집: Torsten 2025년 3월 26일
Usually, the equation reads
dW/dt + U*dW/dx = C (1)
If U in this equation is positive, your flow goes from left to right, if U is negative, your flow goes from right to left.
Depending on the sign of U in (1), you must discretize dW/dX in point X(i) as
(W(i)-W(i-1))/(X(i)-X(i-1)) if U > 0 (information comes from left)
and as
(W(i+1)-W(i))/(X(i+1)-X(i)) if U < 0 (information comes from right)
We call it "Upwind Differencing". Using a discretization that doesn't follow the physical direction of informational flow leads to an unstable discretization and thus a failure of the ODE integrator.
  댓글 수: 1
JUBAIR
JUBAIR 2025년 3월 26일
Thanks for the explanation. That helps a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by