# plotting Integrating by using numerical solution

pde1 = -y1''[x] - (2*y1'[x])/x + ((y1[x])^3 + y2[x])y1[x] == 0;
pde2 = y2''[x] + (2y2'[x])/x - (y1[x])^3 == 0;
sol = NDSolve[ {pde1, pde2, y1[1] == 0.001, y2[1] == -0.001,
y1'[0.001] == 0.001, y2'[0.001] == 0.001}, {y1, y2}, {x,0.001, 20}]


I need to plot the values of Integrate[ y2[x]*y1[x]^3* x^2,{x,0.001, 20}]

• {fy1, fy2} = Values@First@sol; NIntegrate[fy2[x]*fy1[x]^3 x^2, {x, 0.001, 20}] .What you're asking for gives a single number so I don't know why you're asking for a plot. – flinty Dec 2 '20 at 18:52
• thank you so much for your answer , i was think it can varies in the interval but i'm not sure – Bahi Mido Dec 2 '20 at 19:32
• 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) Take the tour! 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! – Michael E2 Jan 2 at 15:59

You can add a extra differential equation f'[x]==y2[x]*y1[x]^3* x^2 in your equations and solve f'[x] at the same time.

so we need to set the initial value f[0.001]=c and c is a parameters,so we use ParametricNDSolve

Clear[pde1, pde2, pde3, sol];
pde1 = -y1''[x] - (2*y1'[x])/x + ((y1[x])^3 + y2[x]) y1[x] == 0;
pde2 = y2''[x] + (2 y2'[x])/x - (y1[x])^3 == 0;
pde3 = f'[x] == y2[x]*y1[x]^3*x^2;
sol = ParametricNDSolve[{pde1, pde2, pde3, y1[1] == 0.001,
y2[1] == -0.001, y1'[0.001] == 0.001, y2'[0.001] == 0.001,
f[0] == c}, {y1, y2, f}, {x, 0.001, 20}, {c}]
f[0][20] - f[0][0.001] /. sol
f[1][20] - f[1][0.001] /. sol
Plot[f[0][x] /. sol, {x, 0.001, 20}]
Plot[f[1][x] /. sol, {x, 0.001, 20}]


-2.38247*10^-9

-2.3811*10^-9

• Thank you so much – Bahi Mido Dec 3 '20 at 16:48
• This method is relatively inaccurate because it depends on the errors at only two points. With the method, @flinty proposed, errors are averaged over the whole integration interval. The relative error here is 0.6 percent, whereas the method of flinty calculated with machinePrecision gives an error of about 0.002 percent. – Akku14 Dec 4 '20 at 7:01
• The main reason the result is inaccurate is that the default AccuracyGoal is almost 8 dec. pl. and the result is 2*10^-9 — that is, the goal is to get at least one digit of accuracy. Use AccuracyGoal -> 16 or 17 (== - Log10[result] + MachinePrecision/2), and this result will be equivalent to flinty's. A minor reason is a typo in the initial condition: Use f[0.001] == c. Then f[0][20] gives the most accurate result. The number f[1][20] - 1 is less accurate due to subtractive cancellation. (I think the parameter c is useless in this case for the OP's particular problem.) – Michael E2 Jan 2 at 16:00