Group Abstract Group Abstract

Message Boards Message Boards

Cumulative definte integral makes kernel run slow

Posted 10 years ago

I have a problem where I need to calculate the cumulative definite integral and mind the min and max of the function. I believe I have set it up correctly and I have been able to compute what I need for a simple case. But during the evaluation the Mathematica kernel runs for along time and locks up my PC. I would appreciate any help or suggestion you may have to improve the code.

Clear["Global`*"];
t =.; f = 1; \[Omega] = 2 \[Pi] f; Tp = 1/f;
r = 1.;
L = 6 r;
\[Theta] = \[Omega] t;
Vd =.;
Ncyl = 3;
\[Phi][i_] := (i - 1) 2 \[Pi]/Ncyl;
Q[t_, i_] := 
  r Sin[\[Theta] + \[Phi][i]] - (
   r^2 Sin[\[Theta] + \[Phi][i]] Cos[\[Theta] + \[Phi][i]])/Sqrt[
   L^2 - r^2 (Sin[\[Theta] + \[Phi][i]])^2];

ftot[t_] := Sum[Max[Q[t, i], 0], {i, 1, Ncyl}];

favg = Quiet@NIntegrate[ftot[t], {t, 0, Tp}, AccuracyGoal -> 2]/Tp;
Vstroke = favg Tp;
fd[t_] := ftot[t] - favg;

Plot[{ftot[t], favg, fd[t]}, {t, 0, Tp}, Frame -> True, Axes -> True, 
 PlotRange -> { {0, Tp}, {-1.2, 1.2}}, 
 FrameLabel -> {"Time (sec)", "Flow rate"}, GridLines -> Automatic, 
 AspectRatio -> .65, 
 PlotStyle -> {{Red, Thickness[0.005]}, {Green, 
    Thickness[0.0051]}, {Blue, Thickness[0.005]}}, 
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
   FontFamily -> "Arial"}]

Vd[s_] := Integrate[fd[t], {t, 0, s}];
Plot[{Vd[s]}, {s, 0, Tp}, Frame -> True, Axes -> True, 
 FrameLabel -> {"Time (sec)", "Flow"}, GridLines -> Automatic, 
 AspectRatio -> .65, 
 PlotStyle -> {{Brown, Thickness[0.005]}, {Cyan, 
    Thickness[0.0051]}, {Gray, Thickness[0.01]}}, 
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
   FontFamily -> "Arial"}]

Vmax = MaxValue[Vd[s], s];
Vmin = MinValue[Vd[s], s];
deltav = (Vmax - Vmin)/Vstroke
POSTED BY: Kaushik Mallick
5 Replies

For cumulative integrals it may be convenient to use NDSolve instad of Integrate, at least if the interval is not too large:

Vd = NDSolveValue[{Vd[0] == 0, Vd'[s] == fd[s]}, Vd, {s, 0, Tp}];
POSTED BY: Gianluca Gorni
Posted 10 years ago

That did it! Thanks a lot.

POSTED BY: Kaushik Mallick

Sorry, that should probably be

NMaxValue[ {vd[s], 0<=s<=Tp}, s]
POSTED BY: Gianluca Gorni

I think you should add the domain constraints:

NMaxValue[{vd,0<=s<=Tp}, s]
POSTED BY: Gianluca Gorni
Posted 10 years ago

Gianluca, That was an excellent idea. Thanks for your suggestion. I had to make minor adjustment to the code, but it made it work.

vd = NDSolveValue[{vd[0] == 0, vd'[s] == fd[t = s]}, vd, {s, 0, Tp}, 
   AccuracyGoal -> 8];

However, I am getting some errors associated with computing the min and max value of the function. Any suggestion on what I need to do to avoid these errors?

Clear["Global`*"];
t =.; f = 1; \[Omega] = 2 \[Pi] f; Tp = 1/f;
r = 1.;
L = 6 r;
\[Theta] = \[Omega] t;
Vd =.;
Ncyl = 3;
\[Phi][i_] := (i - 1) 2 \[Pi]/Ncyl;
Q[t_, i_] := 
  r Sin[\[Theta] + \[Phi][i]] - (
   r^2 Sin[\[Theta] + \[Phi][i]] Cos[\[Theta] + \[Phi][i]])/Sqrt[
   L^2 - r^2 (Sin[\[Theta] + \[Phi][i]])^2];

ftot[t_] := Sum[Max[Q[t, i], 0], {i, 1, Ncyl}];

favg = Quiet@NIntegrate[ftot[t], {t, 0, Tp}, AccuracyGoal -> 4]/Tp;
vstroke = favg Tp;
fd[t_] := ftot[t] - favg;

Plot[{ftot[t], favg, fd[t]}, {t, 0, Tp}, Frame -> True, Axes -> True, 
 PlotRange -> { {0, Tp}, {-1.2, 1.2}}, 
 FrameLabel -> {"Time (sec)", "Flow rate"}, GridLines -> Automatic, 
 AspectRatio -> .65, 
 PlotStyle -> {{Red, Thickness[0.005]}, {Green, 
    Thickness[0.0051]}, {Blue, Thickness[0.005]}}, 
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
   FontFamily -> "Arial"}]

vd = NDSolveValue[{vd[0] == 0, vd'[s] == fd[t = s]}, vd, {s, 0, Tp}, 
   AccuracyGoal -> 8];


Plot[{vd[s]/vstroke}, {s, 0, Tp}, Frame -> True, Axes -> True, 
 FrameLabel -> {"Time (sec)", "Flow"}, GridLines -> Automatic, 
 AspectRatio -> .65, 
 PlotStyle -> {{Brown, Thickness[0.005]}, {Cyan, 
    Thickness[0.0051]}, {Gray, Thickness[0.01]}}, 
 BaseStyle -> {FontWeight -> "Bold", FontSize -> 15, 
   FontFamily -> "Arial"}]

vmax = NMaxValue[vd, s];
vmin = NMinValue[vd, s];
deltav = (vmax - vmin)/vstroke
POSTED BY: Kaushik Mallick
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard