4
$\begingroup$

I have a problem with plotting the integral of an NDSolve solution.

I have attached the code for a simple 2nd order linear ODE to be solved by NDSolve and then to be integrated by NIntegrate.

I would you appreciate if you could give me a help to overcome the problem.

My best regards.

Hadi

f := NDSolve[{x''[t] + 2 x'[t] + x[t] == 0, x[0] == 1, x'[0] == -4}, 


x, {t, 0, 2}]

g[t_] := NIntegrate[Exp[y*Evaluate[x[t] /. f]], {y, 1, 10}]

Plot[g[t], {t, 0, 2}]

And hear are the errors

NDSolve::dsvar: 0.00004085714285714285` cannot be used as a variable. >>

ReplaceAll::reps: {NDSolve[{x[0.0000408571]+2 (x^[Prime])[0.0000408571]+(x^[Prime][Prime])[0.0000408571]==0,x[0]==1,(x^[Prime])[0]==-4},x,{0.0000408571,0,2}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

NIntegrate::inumr: The integrand E^(y (x[0.0000408571]/. NDSolve[{x[<<1>>]+Times[<<2>>]+(<<1>>^(<<1>>))[<<1>>]==0,x[0]==1,(x^[Prime])[0]==-4},x,{0.0000408571,0,2}])) has evaluated to non-numerical values for all sampling points in the region with boundaries {{1,10}}. >>

NDSolve::dsvar: 0.00004085714285714285` cannot be used as a variable. >>

ReplaceAll::reps: {NDSolve[{x[0.0000408571]+2 (x^[Prime])[0.0000408571]+(x^[Prime][Prime])[0.0000408571]==0,x[0]==1,(x^[Prime])[0]==-4},x,{0.0000408571,0,2}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

NIntegrate::inumr: The integrand E^(y (x[0.0000408571]/. NDSolve[{x[<<1>>]+Times[<<2>>]+(<<1>>^(<<1>>))[<<1>>]==0,x[0]==1,(x^[Prime])[0]==-4},x,{0.0000408571,0,2}])) has evaluated to non-numerical values for all sampling points in the region with boundaries {{1.,10.}}. >>

NDSolve::dsvar: 0.04085718367346938` cannot be used as a variable. >>

General::stop: Further output of NDSolve::dsvar will be suppressed during this calculation. >>

ReplaceAll::reps: {NDSolve[{x[0.0408572]+2 (x^[Prime])[0.0408572]+(x^[Prime][Prime])[0.0408572]==0,x[0]==1,(x^[Prime])[0]==-4},x,{0.0408572,0,2}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

General::stop: Further output of ReplaceAll::reps will be suppressed during this calculation. >>

NIntegrate::inumr: The integrand E^(y (x[0.0408572]/. NDSolve[{x[<<1>>]+Times[<<2>>]+(<<1>>^(<<1>>))[<<1>>]==0,x[0]==1,(x^[Prime])[0]==-4},x,{0.0408572,0,2}])) has evaluated to non-numerical values for all sampling points in the region with boundaries {{1,10}}. >>

General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation. >>

$\endgroup$
3
$\begingroup$

A useful piece of advice. Before putting many commands in the same cell, try to run each one individually to make sure that there are no errors. This time it was the first NDSolve causing something. The following code works

f1 = NDSolve[{x''[t] + 2 x'[t] + x[t] == 0, x[0] == 1, x'[0] == -4}, 
  x, {t, 0, 2}]
Plot[x[t] /. f1, {t, 0, 2}]

And then,the numerical integration and its plot

g[t_] := NIntegrate[Exp[y*Evaluate[x[t] /. f1]], {y, 1, 10}]
Plot[g[t], {t, 0, 2}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you so much for your answer. However when I still have problem with this code: f[a_] := NDSolve[{ax''[t] + 2 x'[t] + x[t] == 0, x[0] == 1, x'[0] == -4}, x, {t, 0, 2}] g[t_] := NIntegrate[Exp[yEvaluate[x[t] /. f[1]]], {y, 1, 10}] Plot[g[t], {t, 0, 2}] The errors: NDSolve::dsvar: 0.00004085714285714285` cannot be used as a variable. >> ... $\endgroup$ – user14750 Jan 27 at 12:42
  • $\begingroup$ Observe that I have not used the := but rather the = in the solution I proposed $\endgroup$ – DiSp0sablE_H3r0 Jan 27 at 12:46
2
$\begingroup$

There is no need to use NIntegrate:

X = NDSolveValue[{x''[t] + 2 x'[t] + x[t] == 0, x[0] == 1,x'[0] == -4}, x, {t, 0, 2}]

Plot[Integrate[Exp[y X[t]], {y, 1, 10}],{t,0,2}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ I understand your points and appreciate for them. I still have not reached my answer. Please consider the code in which a and b are arbitrary constants and I want to have them utill the finall solution: f[a_, b_] := NDSolve[{ax''[t] + bx'[t] + x[t] == 0, x[0] == 1, x'[0] == -4}, x[t], {t, 0, 2}] Plot[Evaluate[x[t] /. f[1, 2]], {t, 0, 2}] Now I want to perform integration but the following code does not work: g[t_] := Integrate[Exp[y*x[t]] /. f[1, 2], {y, 1, 10}] Plot[g[t], {t, 0, 2}] $\endgroup$ – user14750 Jan 27 at 16:34
  • $\begingroup$ @user14750 as I mentioned previously and you can see in every answer, the issue is the use of the set delayed. That is the := symbol. If you change that to = your code works just fine $\endgroup$ – DiSp0sablE_H3r0 Jan 27 at 16:40
0
$\begingroup$
Clear["Global`*"]

eqns = {x''[t] + 2 x'[t] + x[t] == 0, x[0] == 1, x'[0] == -4};

In defining f use Set rather than SetDelayed

f = NDSolve[eqns, x, {t, 0, 2}][[1]]

enter image description here

g[t_?NumericQ] :=
 NIntegrate[Exp[y*x[t] /. f], {y, 1, 10}]

For comparison, the exact solution of the differential equation is

sol = DSolve[eqns, x, t][[1]]

(* {x -> Function[{t}, -E^-t (-1 + 3 t)]} *)

Verifying the exact solution

eqns /. sol // Simplify

(* {True, True, True} *)

And the exact expression for g is

g2[t_] = Integrate[Exp[y*x[t]] /. sol, {y, 1, 10}]

(* (E^(E^-t (1 + (-30 + E^t) t)) (-E^(9 E^-t) + E^(27 E^-t t)))/(-1 + 3 t) *)

g2[t] has a minimum

min = Minimize[g2[t], t] // Simplify

(* {1/3 E^(4/3 - 30/E^(4/3)) (-1 + E^(27/E^(4/3))), {t -> 4/3}} *)

Plot[{g[t], g2[t]}, {t, 0, 2},
 PlotStyle -> {Automatic, Dashed},
 PlotLegends -> Placed[{Numeric, Exact}, {0.5, 0.5}],
 Epilog ->
  {Red, AbsolutePointSize[4],
   Point[{t, g2[t]} /. min[[2]]]}]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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