我的问题是:为什么rhsexpr
有效但rhsfunc
无效。谢谢。:)
m = l = 1.; \[Mu] = 100;
beltv[t_] = .1;
spring[x_] = 1000. (l - x);
viscous[v_] := -30. (v - beltv[t]);
coulomb[v_] := -25. Sign[v - beltv[t]];
stribeck[v_] := -.3 Sign[v] Exp[-2 Abs[v]];
friction[v_] := viscous[v] + coulomb[v] + stribeck[v];
rhsfunc
定义为:
rhsfunc[t_, x_, 1] := beltv'[t];
rhsfunc[t_, x_, 0] := spring[x[t]] + friction[x'[t]];
rhsexpr
定义为:
rhsexpr :=
If[stuck[t] == 1, beltv'[t], spring[x[t]] + friction[x'[t]]];
rhsexpr
作品。
NDSolveValue[{x''[t] == rhsexpr , x[0] == 1, x'[0] == 0,
WhenEvent[x'[t] == beltv[t] && spring[x[t]]^2 < \[Mu]^2, stuck[t] -> 1],
WhenEvent[spring[x[t]]^2 >= \[Mu]^2, stuck[t] -> 0],
stuck[0] == 0}, {x[t], x'[t]}, {t, 0, 2},
DiscreteVariables -> stuck[t]]
rhsfunc
不起作用,提示 Encountered non-numerical value for a derived at t == 0
NDSolveValue[{x''[t] == rhsfunc[t, x[t], stuck[t]] , x[0] == 1,
x'[0] == 0,
WhenEvent[x'[t] == beltv[t] && spring[x[t]]^2 < \[Mu]^2,
stuck[t] -> 1],
WhenEvent[spring[x[t]]^2 >= \[Mu]^2, stuck[t] -> 0], stuck[0] == 0}, {x[t], x'[t]}, {t, 0, 2}, DiscreteVariables -> stuck[t]]
更新 1
实际上,我定义的rhsfunc
就像 m@ 在其内置文档中所做的那样,如下所示:
rhs[t_, x_, 1] = -x;
rhs[t_, x_, 2] = 1;
sol = NDSolve[{x'[t] == rhs[t, x[t], a[t]], x[0] == 1, a[0] == 1,
WhenEvent[x[t] - .1, a[t] -> 2],
WhenEvent[x[t] - 2, a[t] -> 1]}, {x, a}, {t, 0, 15},
DiscreteVariables -> {a[t] \[Element] {1, 2}}];
Plot[x[t] /. sol, {t, 0, 10}]