Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am using FindRoot[] to solve a complicated equation. It seems I get the correct answer even though I get errors about an NIntegrate[] inside the FindRoot[] and the number of evaluatons is just one. Moreover is there any possibility of speeding up the process?

In the following example I calculate the function ABar from the functions bf and cf and then recover bf from ABar and cf. This is done in the function bn. bnVerbose and bn are identical except for the EvaluationMonitor.

    n = 5;
    a0 = 1;
    bmax = 0;
    For[i = 1, i <= n,
      Subscript[b, i, 1] = 1/i^2;
      Subscript[b, i, 2] = 1/i^2;
      a0 = a0 + Subscript[b, i, 1] + Subscript[b, i, 2]; 
      bmax = a0 + Subscript[b, i, 1] + Subscript[b, i, 2]; i++;];
    For[i = 1, i <= n,
      Subscript[c, i, 1] = 1/i^2;
      Subscript[c, i, 2] = 1/i^2;
      a0 = a0 + Subscript[c, i, 1] + Subscript[c, i, 2]; i++;];

    bf[x_] := 
      Sum[Subscript[b, i, 1]*Sin[2 \[Pi] i x] + 
        Subscript[b, i, 2]*Cos[2 \[Pi] i x], {i, 1, n}];
    cf[x_] := 
      Sum[Subscript[c, i, 1]*Sin[2 \[Pi] i x] + 
        Subscript[c, i, 2]*Cos[2 \[Pi] i x], {i, 1, n}];
    ABar[x_] := 1/NIntegrate[1/(a0 + bf[x] + cf[y]), {y, 0, 1}];
    ABarB[b_?NumericQ] := 1/NIntegrate[1/(a0 + b + cf[y]), {y, 0, 1}];
    ABarBJ[b_?
        NumericQ] := {{NIntegrate[
          1/(a0 + b + cf[y])^2, {y, 0, 
           1}]/(NIntegrate[1/(a0 + b + cf[y]), {y, 0, 1}])^2}};

    bn[x_] := 
      b /. FindRoot[ABarB[b] - ABar[x], {b, 0, -bmax, bmax}, 
        Jacobian -> ABarBJ[b]];

    bnVerbose[x_] := 
      Module[{e, b}, e = 0; 
       sol = FindRoot[ABarB[b] - ABar[x], {b, 0, -bmax, bmax}, 
         Jacobian -> ABarBJ[b], EvaluationMonitor -> e++];
       Print["Number of evaluuations:", e, "\n"];
       b /. sol
       ];
    bnVerbose[0.5]
    Plot[{bn[x], bf[x]}, {x, 0, 1}, PlotStyle -> {Green, Red}]

In each evaluation bn I get several errors of the form:

    NIntegrate::inumr: The integrand 1/(6169/900+b$2526673+Cos[2 \[Pi] y]+<<6>>+1/9 Sin[6 \[Pi] y]+1/16 Sin[8 \[Pi] y]+1/25 Sin[10 \[Pi] y])^2 has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>

Edit: Error Screenshot: error

share|improve this question
    
strange, I have Mathematica 9 as well. –  warsaga Dec 17 '12 at 0:41
    
upload the error, too. Any idea about the performance issues and small number of evaluations? Thx –  warsaga Dec 17 '12 at 1:27
    
To break the tie, using v9 I also have no errors. –  Gabriel Dec 17 '12 at 5:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.