How to code a 1D ODE in MATLAB

5 views (last 30 days)
JUBAIR
JUBAIR on 26 Mar 2025
Commented: JUBAIR on 26 Mar 2025
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.

Accepted Answer

Torsten
Torsten on 26 Mar 2025
Edited: Torsten on 26 Mar 2025
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 Comment
JUBAIR
JUBAIR on 26 Mar 2025
Thanks for the explanation. That helps a lot.

Sign in to comment.

More Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!