Skip to main content
Major clean-up
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

Nested "If" in Mathematica Need help with using nested If expressions in NDSolve

Questions regarding the use of nested forloop hasFor-loops have been posted for a few times on StackExchange. However, I think that the solutions to those questions are not really applicable to what I am trying to do here. I am trying to use a nested for loopFor-loops in NDSolve, which somehow does not work.

One of the methodmethods included in the solution to other similar problems include things like putting If in whichWhich, and that. That does not seem to work for me.

TheMy code is attached below:

ClearAll["Global`*"] z[t_] := y[t] - l Cos[\[CurlyPhi][t]];Cos[φ[t]]; n[t_] := k Abs[z[t]]^(3/2) - c z'[t]; R = 0.0238; k = 141000000; c = 20; g = 9.81; \[Mu]kμk = 0.1804; i = 8.92958*10^-6; m = 0.035; l = R Sqrt[2 (1 + Sin[(1/2) \[Pi]π - 2*0.41])]; a =  NDSolve[{m   y''[t] ==    {m y''[t] == n[t] - m g, \[CurlyPhi]''[ t]φ''[t] == (n[t]*l*Sin[\[CurlyPhi][t]]n[t]*l*Sin[φ[t]] +  m x''[t]*l*Cos[\[CurlyPhi][t]]x''[t]*l*Cos[φ[t]])/i,   x''[t] ==   If[(x[t] - l*\[CurlyPhi][t]l*φ[t] == 0) // Evaluate,  l (\[CurlyPhi]''[   l t](φ''[t] Cos[\[CurlyPhi][t]]Cos[φ[t]] - \[CurlyPhi]'[t]^2 Sin[\[CurlyPhi][  φ'[t]^2 t]]Sin[φ[t]]) // Evaluate,   If[x[t] - l*\[CurlyPhi][t]l*φ[t] > 0, -\[Mu]k*n[t]μk*n[t]/m //  Evaluate, -\[Mu]k*n[t]μk*n[t]/m // Evaluate]],  y[0] == l*Cos[0.41],  y'[0] == -2.22, \[CurlyPhi][0] φ[0] == 0.41, \[CurlyPhi]'[0]φ'[0] == -50,   x[0] == 0, x'[0] == 0,   WhenEvent[z[t] == 0 // Evaluate, "StopIntegration";   Print[y'[t]];    Print[\[CurlyPhi]'[t]]] "StopIntegration"; Print[y'[t]]; Print[φ'[t]]]},  {y'[t], y[t], x[t],  x'[t], \[CurlyPhi][t]φ[t], \[CurlyPhi]'[t]φ'[t], z[t],  n[t], \[CurlyPhi]''[t]φ''[t]},  {t, 0, 0.2},   Method ->  {"EquationSimplification" -> "Residual",   "DiscontinuityProcessing" -> False},  AccuracyGoal -> Automatic,   WorkingPrecision -> MachinePrecision,  MaxSteps -> 100000000,   PrecisionGoal -> Automatic] 
x''[t] ==   If[(x[t] - l*\[CurlyPhi][t]l*φ[t] == 0) // Evaluate,  l (\[CurlyPhi]''[  l t](φ''[t] Cos[\[CurlyPhi][t]]Cos[φ[t]] - \[CurlyPhi]'[t]^2 Sin[\[CurlyPhi][  φ'[t]^2 t]]Sin[φ[t]]) // Evaluate,   If[x[t] - l*\[CurlyPhi][t]l*φ[t] > 0, -\[Mu]k*n[t]μk*n[t]/m //  Evaluate, -\[Mu]k*n[t]μk*n[t]/m // Evaluate]],  

It would be great if anyone can help. Thanks in advance.

Edit

Edit: I am trying to simulate a bounce of an object through these equationsthis equation. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along the x axis-axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so. I have to take both into account of both) And under. Under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thatsThat's why there is aare nested if, the main ifIf expressions. The outer If is to differentiate between static and kinetic friction, while the nested ifinner If is to differentiate between positive and negative kinetic friction ).

Nested "If" in Mathematica in NDSolve

Questions regarding the use of nested forloop has been posted for a few times on StackExchange. However, I think that the solutions to those questions are not really applicable to what I am trying to do here. I am trying to use a nested for loop in NDSolve, which somehow does not work.

One of the method included in the solution to other similar problems include things like putting If in which, and that does not seem to work for me.

The code is attached below

ClearAll["Global`*"] z[t_] := y[t] - l Cos[\[CurlyPhi][t]]; n[t_] := k Abs[z[t]]^(3/2) - c z'[t]; R = 0.0238; k = 141000000; c = 20; g = 9.81; \[Mu]k = 0.1804; i = 8.92958*10^-6; m = 0.035; l = R Sqrt[2 (1 + Sin[(1/2) \[Pi] - 2*0.41])]; a = NDSolve[{m y''[t] ==    n[t] - m g, \[CurlyPhi]''[ t] == (n[t]*l*Sin[\[CurlyPhi][t]] +  m x''[t]*l*Cos[\[CurlyPhi][t]])/i, x''[t] == If[(x[t] - l*\[CurlyPhi][t] == 0) // Evaluate,  l (\[CurlyPhi]''[   t] Cos[\[CurlyPhi][t]] - \[CurlyPhi]'[t]^2 Sin[\[CurlyPhi][  t]]) // Evaluate, If[x[t] - l*\[CurlyPhi][t] > 0, -\[Mu]k*n[t]/m //  Evaluate, -\[Mu]k*n[t]/m // Evaluate]], y[0] == l*Cos[0.41],  y'[0] == -2.22, \[CurlyPhi][0] == 0.41, \[CurlyPhi]'[0] == -50, x[0] == 0, x'[0] == 0, WhenEvent[z[t] == 0 // Evaluate, "StopIntegration"; Print[y'[t]];    Print[\[CurlyPhi]'[t]]]}, {y'[t], y[t], x[t],  x'[t], \[CurlyPhi][t], \[CurlyPhi]'[t], z[t],  n[t], \[CurlyPhi]''[t]}, {t, 0, 0.2}, Method -> {"EquationSimplification" -> "Residual", "DiscontinuityProcessing" -> False}, AccuracyGoal -> Automatic, WorkingPrecision -> MachinePrecision, MaxSteps -> 100000000, PrecisionGoal -> Automatic] 
x''[t] == If[(x[t] - l*\[CurlyPhi][t] == 0) // Evaluate,  l (\[CurlyPhi]''[  t] Cos[\[CurlyPhi][t]] - \[CurlyPhi]'[t]^2 Sin[\[CurlyPhi][  t]]) // Evaluate, If[x[t] - l*\[CurlyPhi][t] > 0, -\[Mu]k*n[t]/m //  Evaluate, -\[Mu]k*n[t]/m // Evaluate]] 

It would be great if anyone can help. Thanks in advance.

Edit: I am trying to simulate a bounce of an object through these equations. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along x axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so I have to take into account of both) And under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thats why there is a nested if, the main if is to differentiate between static and kinetic friction, while the nested if is to differentiate between positive and negative kinetic friction )

Need help with using nested If expressions in NDSolve

Questions regarding the use of nested For-loops have been posted a few times on StackExchange. However, I think that the solutions to those questions are not really applicable to what I am trying to do here. I am trying to use nested For-loops in NDSolve, which somehow does not work.

One of the methods included in the solution to other similar problems include things like putting If in Which. That does not seem to work for me.

My code:

ClearAll["Global`*"] z[t_] := y[t] - l Cos[φ[t]]; n[t_] := k Abs[z[t]]^(3/2) - c z'[t]; R = 0.0238; k = 141000000; c = 20; g = 9.81; μk = 0.1804; i = 8.92958*10^-6; m = 0.035; l = R Sqrt[2 (1 + Sin[(1/2) π - 2*0.41])]; a =  NDSolve[   {m y''[t] == n[t] - m g, φ''[t] == (n[t]*l*Sin[φ[t]] + m x''[t]*l*Cos[φ[t]])/i,   x''[t] ==   If[(x[t] - l*φ[t] == 0) // Evaluate, l (φ''[t] Cos[φ[t]] - φ'[t]^2 Sin[φ[t]]) // Evaluate,   If[x[t] - l*φ[t] > 0, -μk*n[t]/m // Evaluate, -μk*n[t]/m // Evaluate]],  y[0] == l*Cos[0.41], y'[0] == -2.22,  φ[0] == 0.41, φ'[0] == -50,   x[0] == 0, x'[0] == 0,   WhenEvent[z[t] == 0 // Evaluate,     "StopIntegration"; Print[y'[t]]; Print[φ'[t]]]},  {y'[t], y[t], x[t], x'[t], φ[t], φ'[t], z[t], n[t], φ''[t]},  {t, 0, 0.2},   Method ->  {"EquationSimplification" -> "Residual",   "DiscontinuityProcessing" -> False},  AccuracyGoal -> Automatic,   WorkingPrecision -> MachinePrecision,  MaxSteps -> 100000000,   PrecisionGoal -> Automatic] 
x''[t] ==   If[(x[t] - l*φ[t] == 0) // Evaluate, l (φ''[t] Cos[φ[t]] - φ'[t]^2 Sin[φ[t]]) // Evaluate,   If[x[t] - l*φ[t] > 0, -μk*n[t]/m // Evaluate, -μk*n[t]/m // Evaluate]],  

It would be great if anyone can help.

Edit

I am trying to simulate a bounce of an object through this equation. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along the x-axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction. I have to take both into account. Under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. That's why there are nested If expressions. The outer If is to differentiate between static and kinetic friction, while the inner If is to differentiate between positive and negative kinetic friction.

added 2 characters in body
Source Link
hehehaha
  • 663
  • 3
  • 10

Edit: I am trying to simulate a bounce of an object through this equationthese equations. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along x axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so I have to take into account of both) And under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thats why there is a nested if, the main if is to differentiate between static and kinetic friction, while the nested if is to differentiate between positive and negative kinetic friction )

Edit: I am trying to simulate a bounce of an object through this equation. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along x axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so I have to take into account of both) And under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thats why there is a nested if, the main if is to differentiate between static and kinetic friction, while the nested if is to differentiate between positive and negative kinetic friction )

Edit: I am trying to simulate a bounce of an object through these equations. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along x axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so I have to take into account of both) And under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thats why there is a nested if, the main if is to differentiate between static and kinetic friction, while the nested if is to differentiate between positive and negative kinetic friction )

added 504 characters in body
Source Link
hehehaha
  • 663
  • 3
  • 10

Edit: I am trying to simulate a bounce of an object through this equation. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along x axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so I have to take into account of both) And under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thats why there is a nested if, the main if is to differentiate between static and kinetic friction, while the nested if is to differentiate between positive and negative kinetic friction )

Edit: I am trying to simulate a bounce of an object through this equation. I have the equation describing the motion along the y axis, x axis and rotation respectively. However, for the motion along x axis, it would be piecewise as during the collision, there may be both static friction and kinetic friction (so I have to take into account of both) And under kinetic friction, it will be either in the positive or negative direction depending on the relative motion between contact point and ground. (thats why there is a nested if, the main if is to differentiate between static and kinetic friction, while the nested if is to differentiate between positive and negative kinetic friction )

Source Link
hehehaha
  • 663
  • 3
  • 10
Loading