# problem with Solve/findroot and plotting when using NIntegrate

do[
eq11[q1_] := (2*(1 - Cos[2*Pi*q1]));
U = 1;
hq1[q1_] := ((eq11[q1])^2 + 2*U*n0*(eq11[q1]))^(1/2);
ha1[q1_] := (((eq11[q1]) + (U*n0))/hq1[q1]) - 1;
vv32[n0_?NumericQ] := n0 + 0.5*NIntegrate[ha1[q1], {q1, -0.5, 0.5}];
FindRoot[vv32[n0] == 1, {n0, 1}];
{n0,0,1,0.1}]


I am getting these errors:

NIntegrate::inumri: "The integrand -1+(1. +2(1-Cos[Times[<<3>>]]))/Sqrt[4.(1-Cos[<<1>>])+4(1+Times[<<2>>])^2]\n has evaluated to Overflow, Indeterminate, or Infinity for all sampling points in the region with boundaries {{0.,1.07577*10^-15}}." NIntegrate::inumri: "The integrand -1+(1. +2(1-Cos[Times[<<3>>]]))/Sqrt[4.(1-Cos[<<1>>])+4(1+Times[<<2>>])^2]\n has evaluated to Overflow, Indeterminate, or Infinity for all sampling points in the region with boundaries {{0.,1.07577*10^-15}}. " FindRoot::nlnum: "The function value {0. +0.5\ NIntegrate[ha1[q1],{q1,-0.5,0.5}]}\n is not a list of numbers with dimensions {1} at {n0} = {1.}."

I want to plot between n0 and U for values of U from 1-20. The problem lies in using FindRoot to get the value of n0. Can anyone help me out?

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Jan 31 '15 at 17:46
• Side issues: do or Do? Why ; after FindRoot? Why not Table? Or skip it altogether? – Michael E2 Jan 31 '15 at 18:12
• Your integrand has a singularity at q1 = 0 of the order 1/Abs[q1] for n0 != 0, so the integral is divergent almost everywhere. – Michael E2 Jan 31 '15 at 18:23

Your code has a few syntax issues as noted in the comments. The following seems to work (if I understand correctly what you are trying to do)

Configure all functions to accept only numerical arguments:

Clear[eq11, hq1, ha1, vv32];
eq11[q1_?NumericQ] := (2*(1 - Cos[2*Pi*q1]));
hq1[q1_?NumericQ, U_?NumericQ, n0_?NumericQ] := ((eq11[q1])^2 + 2*U*n0*(eq11[q1]))^(1/2);
ha1[q1_?NumericQ, n0_?NumericQ, U_?NumericQ] := (((eq11[q1]) + (U*n0))/hq1[q1, U, n0]) - 1;
vv32[n0_?NumericQ, U_?NumericQ] := n0 + 0.5*NIntegrate[ha1[q1, n0, U], {q1, -0.5, 0.5}];


Define a function to provide $n_0$ as a function of $U$ (I used an initial search value of $0.1$):

Clear[n];
n[U_?NumericQ] := FindRoot[vv32[n0, U] == 1, {n0, .1}][[1, 2]];


Use Plot to obtain the graph (takes a few seconds). Note use of Quiet to suppress any messages from FindRoot:

Quiet@Plot[n[U], {U, 1, 20}, PlotRange -> {{0, 20}, {0, 0.5}},
AxesOrigin -> {0, 0},
AxesLabel -> {"U", "\!$$\*SubscriptBox[\(n$$, $$0$$]\)"}]


EDIT: As MichaelE2 commented, the integral seems to be non convergent, which is also reflected by significantly different output of NIntegrate with different values of WorkingPrecision option. Therefore, the answer only addresses the syntax issues of the OP code, and does NOT give a correct/reliable solution of the problem.

• I don't think the integral converges; and the integrand is positive, so no principal value, either. What do you think? – Michael E2 Jan 31 '15 at 18:37
• @MichaelE2 Indeed, did not bother to check this issue at all. Thanks for pointing this out. – Stelios Jan 31 '15 at 18:47
• Bro thanks a ton! – jazz1001 Jan 31 '15 at 21:03
• @Stelios What's the use of "[[1,2]]"in the line "Clear[n]; n[U_?NumericQ] := FindRoot[vv32[n0, U] == 1, {n0, .1}][[1, 2]];" – jazz1001 Jan 31 '15 at 21:12
• @jazz1001 An alternative to [[1, 2]] is n0 /. FindRoot[vv32[n0, U] == 1, {n0, .1}], or perhaps better, Block[{n0}, n0 /. FindRoot[vv32[n0, U] == 1, {n0, .1}]]`. – Michael E2 Feb 1 '15 at 14:55