0

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);
  • 1
    k55 is undefined – Ulrich Neumann Jan 29 '24 at 16:30
  • Sorry @UlrichNeumann, let me give the definitions. – Jules Alvarez Jan 29 '24 at 16:32
  • Try V= NDSolve[{v'[t] + (v[t]^2 + f == 0, v[0] == -I*k55}, v[t], {t, 0, 150}]: V[x2]-V[x1] – Ulrich Neumann Jan 29 '24 at 16:33
  • 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 ? – andre314 Jan 29 '24 at 16:34
  • 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]}] – Ulrich Neumann Jan 29 '24 at 16:39
  • @andre314 That would be great, but only if you could integrate inside the NDSolve. I am interested in the integration part since I have already solved for v. However, it would be preferable if I could just get the antiderivative of v through an Integrate[] function. – Jules Alvarez Jan 29 '24 at 16:39
  • @UlrichNeumann Would it show me the integral of v? It seems more like just the difference of two numbers of V. – Jules Alvarez Jan 29 '24 at 16:43

2 Answers2

0

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] == -Ik55[[1]] ( and not k55 *)}, v[t], t, MaxStepSize -> 0.001]];

res = Table[NDSolveIterate[stateData, t]; varAndDerivativesValues = NDSolveProcessSolutions[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

andre314
  • 18,474
  • 1
  • 36
  • 69
  • 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. – Jules Alvarez Jan 29 '24 at 18:25
  • 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. – andre314 Jan 29 '24 at 21:12
0

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

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
  • 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}]. – Jules Alvarez Jan 29 '24 at 18:39
  • Change ic to intv[0] == 1/Sqrt[2*k55]+0 I. Now NDSolveassumes complex intv. – Ulrich Neumann Jan 29 '24 at 18:44
  • 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. – Jules Alvarez Jan 29 '24 at 18:46
  • 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. – Jules Alvarez Jan 29 '24 at 20:42
  • Did you modify the definitions k55, f ? – Ulrich Neumann Jan 29 '24 at 20:58
  • Yeah, just as you mentioned with the [[1]] – Jules Alvarez Jan 29 '24 at 21:07
  • I found a worse problem, running solv = NDSolve[{v'[t] + (v[t])^2 + f == 0, v[0] == -I*k55}, {v}, {t, 0, 150}] is supposed to give a function v[t] solution of the differential equation v'[t]+(v[t])^2+f==0. However, while substituting the solution, it does not fit, and it even highly oscilates in orders of 10^5. – Jules Alvarez Jan 29 '24 at 23:17
  • Usually plotting the ode with the numerical solution doesn't give 0 – Ulrich Neumann Jan 30 '24 at 07:54