NDSolve with Quantities

  • Topic: Mathematica 
  • Thread starter Swamp Thing
  • Start date
1 reply · 2K views · last reply
Swamp Thing
Insights Author
Messages
1,087
Reaction score
864
TL;DR
Having trouble running Wolfram's NDSolve with physical quantities.
Code:
v0 = Quantity[0.00001, "meters/second"]
x0 = Quantity[1.0001, "meters"]
K = Quantity[1.01, "Newtons/meter"]
M = Quantity[1.001, "kilograms"]

s = NDSolve[{x''[t] == - K * x[t]/M , x[0] == x0, x'[0] == v0},  x, {t, 0, 10}]

... gives this error:
Code:
NDSolve::ndinnt: Initial condition 1.0001m is not a number or a rectangular array of numbers.

What is going wrong here?

====
Edit: Tried adding units to the start and stop time as well...

Code:
v0 = Quantity[0.00001, "meters/second"]
x0 = Quantity[1.0001, "meters"]
K = Quantity[1.01, "Newtons/meter"]
M = Quantity[1.001, "kilograms"]
t1 = Quantity[0.0, "seconds"]
t2 = Quantity[5.0, "seconds"]

s = NDSolve[{x''[t] == - K * x[t]/M , x[0] == x0, x'[0] == v0}, 
  x, {t, t1, t2}]

And now it's saying...

Code:
NDSolve::ndnl: Endpoint 0.s in {t,0.s,5.s} is not a real number.
 
Attachments
  • Mathematica code assigning quantities with units: v0, x0, K, M, t1 and t2, followed by an NDSolve call solving x''[t] == -K*x[t]/M with initial conditions, over t from t1 to t2.
    1749112555045.webp
    12.8 KB · Views: 60
Last edited: