Hello there, I would appreciate your help with me on this. I am modeling a second order differential equation ( mass and spring ). The spring is nonlinear (k x[t]^1.5). NDSolve solves the system but when encounters complex solutions it stoppes which makes sense to me. I want to make a constraint or a condition by using WhenEvent function inside the NDSolve to set x[t] to zero everytime the solution becomes complex or to just take the positive real part of it and continue solving for the specified period of time of 10 seconds. I’ve trying with no success. Please see the code in this message and I am also attaching the file for you. I would highly appreciate your help. Thank you
You begin with x1’[t] and x1[t] positive, but the force term drives x1’[t] negative and eventually x1[t] goes negative. This is when (x1[t])^(3/2) becomes complex. If this is a projectile, it just hit the ground and you could use “StopIntegration” in WhenEvent.
If it is really a spring which needs a real restoring force when x1 < 0, then the mathematics needs a different formulation. For example:
Dear David, thank you for your help. I understand your explanation of the problem. It made it a lot easier for me to understand what the NDSolve does when it encounters negative solutions, which results in complex solutions. Would you please tell me what are you telling NDSolve to do by your suggestion ( Sign[x1[t]] Abs[x1[t]]^(3/2) ). I want to understand what does it do when it encounters negative solutuions.
Thank you again for your help David
Regards, Khalid
We see the right hand term is a restoring force. When x[t] is positive, it applies a negative force to send x in the negative direction. If it’s a real spring, then when x[t] goes negative, it should be applying a positive force. But it doesn’t. Instead it produces a complex number.
So I changed the physics:
eq2 = x''[t] == -Sign[x[t]] Abs[x[t]]^(3/2)
Now the term on the right hand side works for both positive and negative x[t]. It has the same nonlinear relation on each side of zero, with the proper change of sign. Look up the functions Sign and Abs. Here we plot the restoring force term.
Note that in revising the notebook, WhenEvent is not needed. It is the physics, or if you like the math description of the physics, which I revised. Not the code.