5
$\begingroup$

My code:

r5 = 47000;
vcc = 3.3;
hfe = 420; (* BC337-40: 250 *)
vbe = 0.6;
r4 = vbe/2*^-6;
ctr = 1;
r1 = 1000000; r2 = r1;
vrms = 230;
vled = 1.5;
v0[t_] := Max[vcc - r5 ic[t], 0.2]
ic[t_] := Min[hfe ib[t] + 0.000005, vcc/r5]
ib[t_] := idar[t] - ir4[t]
ir4[t_] := Min[idar[t], vbe/r4]
idar[t_] := Abs[iled[t]] ctr
iled[t_] := Max[Sign[vi[t]] (Max[Abs[vi[t]] - vled, 0])/(r1 + r2), 0]
vi[t_] := vrms Sqrt[2]  Sin[2 Pi t/20]
pmains[t_] := vi[t] iled[t]
pvcc[t_] := vcc idar[t] + vcc ic[t]
power[t_] := pmains[t] + pvcc[t]
pvccavg = Integrate[pvcc[t], {t, 0, 20}]/20

MMA gives me this result:

enter image description here

which is my list of nested function calls written as one single function. Why doesn't Integrate work here? Note that it does give me results if I split the [0,20] interval in [0,10] and [10,20].

$\endgroup$
4
  • 4
    $\begingroup$ NIntegrate gives you the answer in less than a second $\endgroup$
    – rm -rf
    Commented Jul 16, 2012 at 16:40
  • $\begingroup$ @R.M. - Yes, OK, Thanks. But I was wondering why Integrate doesn't work, while it does over the two smaller intervals. Does it have to do with the Abs and/or Max I'm using? $\endgroup$
    – stevenvh
    Commented Jul 16, 2012 at 16:59
  • 1
    $\begingroup$ Integrate requires 1. Symbolic Input 2. a function that it knows how to symbolically solve. The function that you are integrating using floating point numbers instead of rational numbers. This by itself is a problem since symbolic manipulation of floating point numbers isn't necessarily numerically stable. Additionally, Integrate does not guarantee that it can always find a symbolic solution for an Integral. This is because finding a symbolic solution is often impossible. $\endgroup$
    – Searke
    Commented Jul 16, 2012 at 19:06
  • $\begingroup$ Although really in this case, the solution is a piecewise function, which is easily solved symbolically. Integrate just doesn't seem to have any functionality for converting the Max/Min statements into a piecewise expression. $\endgroup$
    – Searke
    Commented Jul 16, 2012 at 19:17

3 Answers 3

10
$\begingroup$
Plot[pvcc[t], {t, 0, 20}]

Mathematica graphics

seems to show several discrete regions.

After studying those, this seems to give enough of a hint to accomplish the goal.

In[1]:= Integrate[Simplify[pvcc[t], t > 0], {t, 0, 20}]

Out[1]= 0.00585019
$\endgroup$
2
  • 1
    $\begingroup$ Bill Simpson, welcome to the site, and nice answer. I included an image of the plot for you. $\endgroup$
    – Mr.Wizard
    Commented Jul 16, 2012 at 17:54
  • $\begingroup$ Another hint of this is that the integral of vcc idar[t] is actually done (without the Simplify). $\endgroup$ Commented Jul 16, 2012 at 18:03
4
$\begingroup$

Or just use Nintegrate[]

pvccavg = NIntegrate[pvcc[t], {t, 0, 20}]

which gives 0.00587397.

$\endgroup$
3
$\begingroup$

OK, so NIntegrate seems to be the solution. But I found a possible reason why symbolic integration doesn't work. Take this indefinite integral:

Integrate[Abs[x], x]  

enter image description here

It won't do it. Apparently Abs sees the argument as complex, because

Integrate[Abs[x], x, Assumptions :> Element[x, Reals]

does work:

enter image description here

It's not a complete explanation though, because if we make the first form into a definite integral

Integrate[Abs[x], {x, -1, 2}]  

enter image description here

also works.

$\endgroup$
5
  • $\begingroup$ Regarding the Abs example: try Integrate[If[x > 0, x, -x], x] and now it works. Apparently it assumes a real $x$ for this If[] construct (which a priori I'd have thought shouldn't be used for mathematical functions). $\endgroup$
    – acl
    Commented Jul 17, 2012 at 10:39
  • $\begingroup$ @acl - "which a priori I'd have thought shouldn't be used for mathematical functions". Exactly my thoughts. It's bad enough that my functions are full of Abs and Max. $\endgroup$
    – stevenvh
    Commented Jul 17, 2012 at 11:14
  • 1
    $\begingroup$ @acl by implying that $x>0$, it must be real. The same thing occurs when specifying the limits on the integral. $\endgroup$
    – rcollyer
    Commented Jul 17, 2012 at 14:27
  • $\begingroup$ @acl Complementing rcollyer's answer: see this post. $\endgroup$
    – sebhofer
    Commented Jul 17, 2012 at 14:37
  • $\begingroup$ @rcollyer but it doesn't, it just says "if x>0". I'm not sure I could have predicted what would happen if I hadn't tried this. $\endgroup$
    – acl
    Commented Jul 17, 2012 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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