1
$\begingroup$

I have a function I want to integrate, which is $v(t)$.

solv = NDSolve[{v'[t] + (v[t])^2 + f == 0, v[0] == -I*k55}, 
v[t], {t, 0, 150}]

Also, I have a range tRange=[0,150,0.001]. Now, what I want to do is to integrate this function v=v[t]/.solv using the range I proposed. For example, say x1 and x2 are in the range, and xmin=0, xmax=150; how do I integrate v from xmin to x_1, then x_1 to x_2, then so on until xmax with 0.001 of difference between each step. At the end, I want to put everything in a table. Is there any way how? Or is it possible to make a For[] loop to get the results?

sol = NDSolve[{x''[t] + 3 x'[t] Sqrt[(x'[t])^2/6 + (1 - Exp[-x[t] 
Sqrt[2/3]])^2/4] + 
Sqrt[3/2] Exp[-x[t] Sqrt[2/3]] (1 - Exp[-x[t] Sqrt[2/3]]) == 0, 
a'[t]/a[t] == Sqrt[(x'[t])^2/6 + (1 - Exp[-x[t] Sqrt[2/3]])^2/4],
τ'[t] == 1/a[t],
x'[0] == -0.008226306418212731,
x[0] == 5.630991866033891,
a[0] == 1,
τ[149.4517772937791] == 0},
{x, τ, a},
{t, 0, 500}]
a = a[t] /. sol;
\[Tau] = \[Tau][t] /. sol;
x = x[t] /. sol;
xt = x'[t] /. sol;
att = a''[t] /. sol;
tEnd = t /. FindRoot[(att == 0), {t, 0, 150}]
aEnd = a /. t -> tEnd;
H = Sqrt[(xt)^2/6 + (1 - Exp[-x*Sqrt[2/3]])^2/4];
V = 3/4 (1 - Exp[-Sqrt[2/3] x])^2;
Vt = Sqrt[3/2] Exp[-x Sqrt[2/3]] (1 - Exp[-x Sqrt[2/3]]);
Vtt = -Exp[-2 Sqrt[2/3]*x]*(Exp[Sqrt[2/3]*x] - 2);
t55 = t /. FindRoot[(aEnd/a == E^55), {t, 0, 150}];
\[Tau]55 = \[Tau] /. t -> t55;
a55 = a /. t -> t55;
eps1 = (1/2) (Vt/V)^2;
eta = Vtt/V;
eps2 = -4 eps1 + 2 eta;
k55 = a55*(H /. t -> t55);
\[Nu] = 3/2 + eps1 + 1/2*eps2;
f = k55^2 - (\[Nu]^2 - 1/4)/(\[Tau]^2);
$\endgroup$
7
  • 2
    $\begingroup$ k55 is undefined $\endgroup$ Commented Jan 29 at 16:30
  • $\begingroup$ Sorry @UlrichNeumann, let me give the definitions. $\endgroup$ Commented Jan 29 at 16:32
  • $\begingroup$ Try V= NDSolve[{v'[t] + (v[t]^2 + f == 0, v[0] == -I*k55}, v[t], {t, 0, 150}]: V[x2]-V[x1] $\endgroup$ Commented Jan 29 at 16:33
  • $\begingroup$ It would be simpler to call NDSolve with a maximum step size option of 0.001, and let NDSolve decides itself where the steps are. Do you really need the steps to be at multiples of 0.001 ? $\endgroup$
    – andre314
    Commented Jan 29 at 16:34
  • $\begingroup$ Try V= NDSolve[{v'[t] + (v[t]^2 + f == 0, v[0] == -I*k55}, v[t], {t, 0, 150}]: MapThread[V[#2]-V[#1&,{Rest[trange],Most[trange]}] $\endgroup$ Commented Jan 29 at 16:39

3 Answers 3

0
$\begingroup$

NOT AN ANSWER
(Too long for a comment, and I'm not sure to understand your aim.)

It would be simpler to call NDSolve with a maximum step size option of 0.001, and let NDSolve decide itself where the steps are.

Here is an example :

I don't copy k55 and f that are defined in your large block of code.

x1 = 10;
x2 = 20;
x3 = 30;
xmax = 150;

stateData = 
  First[NDSolve`ProcessEquations[{v'[t] + (v[t])^2 + f == 0, 
     v[0] == -I*k55[[1]] (* and not k55 *)}, v[t], t, MaxStepSize -> 0.001]];

res = Table[NDSolve`Iterate[stateData, t];
  varAndDerivativesValues = 
   NDSolve`ProcessSolutions[stateData, "Forward"];
  stateData = 
   First@NDSolve`Reinitialize[stateData, 
     Equal @@@ Most[varAndDerivativesValues]];
  varAndDerivativesValues, {t, {x1, x2, x3, xmax}}]  

enter image description here

The integration stops at t=138 due to another problem not related with the question :

enter image description here

Related

$\endgroup$
2
  • $\begingroup$ I was thinking something more like For[i = 0, i <= 138.999, j = i + 0.001, Integrate[v[t], {t, i, j + 1}]], would it work? I just want to integrate v. $\endgroup$ Commented Jan 29 at 18:25
  • $\begingroup$ I don't understand your code For[...]. I have not used For[] since 20 years and your syntax probably have some missing or implicits that I can't guess. $\endgroup$
    – andre314
    Commented Jan 29 at 21:12
0
$\begingroup$

The block which evaluates k55, f can only be executed once without an error message. Strange, don't know why.

Change the last codelines to

k55 = (a55*(H /. t -> t55))[[1]];
\[Nu] = 3/2 + eps1 + 1/2*eps2;
f = (k55^2 - (\[Nu]^2 - 1/4)/(\[Tau]^2))[[1]];

Introduce a second variable intv[t] in NDSolveValue which defines the integral of v[t]

NDSolveValue[{v'[t] + (v[t])^2 + f == 0, intv'[t] == v[t], intv[0] == 0 + 0 I, v[0] == -I*k55}, {v, intv} , {t, 0, 150}]

enter image description here

NDSolve claims reduced simulation range {t,0,149}

Check result

Plot[ReIm[intV[t]], {t, 0, 139}, PlotRange -> Automatic,Evaluated -> True]

enter image description here

$\endgroup$
8
  • $\begingroup$ Good result, however I am getting NDSolve::ndode: The equations <<1>> are not differential equations or initial conditions in the dependent variables {intv}. when running solv = NDSolve[{v'[t] + (v[t])^2 + f == 0, intv'[t] == v[t], intv[0] == 1/Sqrt[2*k55], v[0] == -I*k55}, {v, intv}, {t, 0, 150}]. $\endgroup$ Commented Jan 29 at 18:39
  • $\begingroup$ Change ic to intv[0] == 1/Sqrt[2*k55]+0 I. Now NDSolveassumes complex intv. $\endgroup$ Commented Jan 29 at 18:44
  • $\begingroup$ What I did is to quit the kernel and start again, it worked surprisingly. I do not know why there is an error after running a second time the code. $\endgroup$ Commented Jan 29 at 18:46
  • $\begingroup$ Now I am facing some trouble when running the code you provided, NDSolveValue[{v'[t] + (v[t])^2 + f == 0, intv'[t] == v[t], intv[0] == 0 + 0 I, v[0] == -I*k55}, {v, intv} , {t, 0, 150}]. It says NDSolveValue::ndfdmc: Computed derivatives do not have dimensionality consistent with the initial conditions. $\endgroup$ Commented Jan 29 at 20:42
  • $\begingroup$ Did you modify the definitions k55, f ? $\endgroup$ Commented Jan 29 at 20:58
0
$\begingroup$

[H]ow do I integrate v from xmin to x_1, then x_1 to x_2, then so on until xmax with 0.001 of difference between each step. At the end, I want to put everything in a table.

The answer is Method -> {"FixedStep", Method -> ...} and StartingStepSize -> 0.001. To put all 150,000 steps in a table seems overkill — for the site, if not in the actual use-case. Here's an example of 20 steps:

solv = NDSolveValue[{v'[t] + (v[t])^2 + 1 == 0, v[0] == 1}, 
   v, {t, 0, 0.02}, 
   Method -> {"FixedStep", Method -> "ExplicitRungeKutta"}, 
   StartingStepSize -> 0.001];

TableForm[
 Transpose@{Flatten@solv@"Grid", solv@"ValuesOnGrid"},
 TableHeadings -> {Range[0, 20], {"Time", v[t] // TraditionalForm}}]
$\endgroup$

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.