2
$\begingroup$

I'm having a difficulty to NDSolve a ODE due to the dimensions problem (according to the error message). Below is the code I have. First of all are functions:

otime = 0; nzone = 5; iex = 6;T0 = 293.15;hd=150;
lj={6, 12, 18, 24, 29.5};
tmax={35.4, 17.7, 8.85, 5.9, 4.425, 3.97753};
webSpeed={5/6, 5/3, 10/3, 5, 20/3, 89/12};
TE={{383, 393, 403, 413, 298}, {383, 393, 403, 413, 298}, {383, 393, 403,413, 298}, {383, 393, 403, 413, 298}, {383, 393, 403, 413,298}, {383, 393, 403, 413,298}}; 
TA[t_, ie_] := If[t <= otime,T0,For[i = 1, i <= nzone, i++,If[t - otime <lj[[i]]/webSpeed[[ie]], Return[TE[[ie, i]]]]]]
VE={{36, 36, 36, 36, 36}, {36, 36, 36, 36, 36}, {36, 36, 36, 36,36}, {36, 36,36, 36, 36}, {36, 36, 36, 36, 36}, {36, 36, 36, 36,36}};
hi[ie_, i_] := (VE[[ie, i]]/36)^0.8 hd (293/TE[[ie, i]])^(2/3);
htc[t_, ie_]:= If[t <= otime, 0,For[i = 1, i <= nzone, i++,If[t - otime <lj[[i]]/webSpeed[[ie]], Return[hi[ie, i]]]]]

Then is the NDSolve equation:

NDSolve[{T'[t] == htc[t, iex] (TA[t, iex] - T[t]), T[0] == T0}, T, {t, 0, tmax[[iex]]}]

The ultimate goal is to NDSolve the ODE above by varying the index iex from 1, 2, 3, ..., iex But first of all I need to make sure the NDSolve can work! Below is the error message:

NDSolve::nlnum: "The function value {0.0128667 Return[125.469] (-293.15+Return[383])} is not a list of numbers with dimensions {1} at {t,T[t]} = {8.9003*10^-308,293.15}."

Any suggestions would be greatly appreciated!!

$\endgroup$
  • $\begingroup$ It looks like you are misusing Return. Did you try removing it? $\endgroup$ – Karsten 7. Oct 23 '15 at 6:11
  • $\begingroup$ Check reference.wolfram.com/language/ref/Return.html?q=Return $\endgroup$ – Karsten 7. Oct 23 '15 at 6:12
  • $\begingroup$ Thanks @Karsten7.! I tried to remove "Return". But so far seems like it's the only way I can think of to describe TA[t_,ie_] and htc[t_,ie_]. Since only when I used "Return" I can Plot both TA and htc without an error. Any suggestions for this part? Thanks a lot!! $\endgroup$ – DavidC Oct 23 '15 at 6:23
2
$\begingroup$

I'm not sure what you're trying to achieve so failed to do further clean-up, but the following code works:

Clear[TA, htc, message]

message[t_] := "You forgot to define a return value for t = "<>ToString@t;
TA[t_?NumericQ, ie_] := 
  Catch@If[t <= otime, T0, 
    Do[If[t - otime < lj[[i]]/webSpeed[[ie]], Throw@TE[[ie, i]]], {i, nzone}]; 
    Print@message@t];
htc[t_?NumericQ, ie_] := 
  Catch@If[t <= otime, 0, 
    Do[If[t - otime < lj[[i]]/webSpeed[[ie]], Throw@hi[ie, i]], {i, nzone}]; 
    Print@message@t];

sol = NDSolveValue[{T'[t] == htc[t, iex] (TA[t, iex] - T[t]), T[0] == T0}, 
  T, {t, 0, tmax[[iex]]}]

{{lb, rb}} = sol["Domain"];
sol = Plot[sol[t], {t, lb, rb}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you @xzczd ! It works with your new defined TA and htc! Although I can plot the results, I still somehow get an error after running NDSolveValue, which is : NDSolveValue::nlnum: "The function value {0.0128667\ (-324.842+Print)\ Print} is not a list of numbers with dimensions {1} at {t,T[t]} = {3.97753,324.842}" Does this also happen when you run the code? Thanks!! $\endgroup$ – DavidC Oct 23 '15 at 17:35
  • $\begingroup$ @DavidC Slightly different in v9.0.1, the function value in the warning is {(-298.+Null) Null}. If you still have difficulty in understanding the reason for the warning, try TA[tmax[[iex]], iex]. $\endgroup$ – xzczd Oct 24 '15 at 2:47
  • $\begingroup$ Thank you @xzczd !! With your suggestion I found that there's a "=" missing :) $\endgroup$ – DavidC Oct 26 '15 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.