How to code a 1D ODE in MATLAB

11 次查看(过去 30 天)
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.

采纳的回答

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.

更多回答(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