Message Boards Message Boards

NDSolve debugging and help

Posted 5 days ago

Hello. After running the attached code by substituting random values into model, I get quite a few errors, namely Part::partw:, Set::partw:, Rule::argr:,NDSolveValue::ndode: . I would greatly appreciate any help!

M = 23.58/1000;
l = 0.9;
Ms = (2*2.02)/1000 Sqrt[l^2 - 0.4^2];
NN = 10; 
dl = Sqrt[l^2 - 0.4^2]/NN;
g = 9.81;
e = 0.9229372872377224;
m[n_] := If[n == NN, M, Ms/(NN - 1)];
r[n_, t_] := 
  Sum[UnitVector[3, 1]*dl Sin[\[Theta][i, t]] - 
    UnitVector[3, 3]*dl Cos[\[Theta][i, t]], {i, n}];
rdot[n_, t_] := D[r[n, t], t];
tt[t_] := 1/2 Sum[m[n]*Total[rdot[n, t]^2], {n, NN}];
vv[t_] := g*Sum[m[n]*r[n, t][[3]], {n, NN}];
ll[t_] := tt[t] - vv[t];
rr[t_] := 
  1/2 kvc*Sum[(D[\[Theta][n, t], t] - D[\[Theta][n - 1, t], t])^2, {n,
      2, NN}];
airdamp[t_] := 1/2 cd*Total[rdot[NN, t]^2];
eqLag[k_] := 
  D[D[ll[t], D[\[Theta][k, t], t]], t] - 
    D[ll[t], \[Theta][k, t]] == -D[rr[t], D[\[Theta][k, t], t]];
lastpend := 
  D[D[ll[t], D[\[Theta][NN, t], t]], t] - 
    D[ll[t], \[Theta][NN, t]] == -D[airdamp[t], 
      D[\[Theta][NN, t], t]] - D[rr[t], D[\[Theta][NN, t], t]];

eqs = Join[Table[eqLag[k], {k, NN - 1}], {lastpend} ];
initialData = 
  Flatten[Table[{\[Theta][k, 0] == ArcTan[0.423/-0.723], 
     Derivative[0, 1][\[Theta]][k, 0] == 0}, {k, NN}]];
model[cd_?NumberQ, kvc_?NumberQ] := 
 Module[{\[Theta], t}, 
  NDSolveValue[Flatten@{eqs, initialData}, 
   Table[\[Theta][k, t], {k, NN}], {t, 0, 75.272}, 
   Method -> {"EquationSimplification" -> "Residual"}, 
   StartingStepSize -> 10^-6, MaxStepFraction -> 1/100000, 
   MaxStepSize -> 10^-6, MaxSteps -> Infinity]]

model[1, 1]
POSTED BY: Ee Kin Chan
3 Replies

The expression model[cd, kvc][[k]][[0]][t] can be simplified as model[cd, kvc][[k]]. The factor dl cancels out, doesn't it? I am not expert in model fitting, but the dependence of your model in cd and kvc seems too involuted. I would suggest incorporating the two parameters into the model using ParametricNDSolve, of which, sadly, I know very little.

POSTED BY: Gianluca Gorni

It is the Module[{\[Theta], t} that makes trouble. The equations and initial data use variables \[Theta], t from outside, while the part Table[\[Theta][k, t], {k, NN}], {t, 0, 75.272} uses local variables.

Just remove the Module.

POSTED BY: Gianluca Gorni
Posted 5 days ago

Ah I see thank you that works. As a quick follow up question, my next line of code is this, and I seem to get some new errors. Would you mind taking a look at it? (I am assuming you do not need dampdata to debug the code?)

nlm = NonlinearModelFit[dampdata, 
  ArcTan[(dl Sum[Sin[model[cd, kvc][[k]][[0]][t]], {k, NN}])/(
   dl Sum[Cos[model[cd, kvc][[k]][[0]][t]], {k, NN}])], {cd, kvc}, t, 
  Method -> "Gradient"]

enter image description here

POSTED BY: Ee Kin Chan
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract