0
$\begingroup$

I have a differential equation which I solve numerically using NDSolve to obtain y[x]for different values of t.
Example:
t=0.00;
s = NDSolve[{y'[x] == x+2t,, y[0] == 1}, y[x], {x, 0, 30}]
I need the first and last value of the function y[x] which I can obtain by:
f[x_]:=Evaluate[y[x]/.s];
f[0][[1]]
f[30][[1]]
Now I want the program to run for every t from 0 to 1 at an interval of 0.01
such that for all 101 time steps of t, I get the values of f[0][[1]] and f[30][[1]]. Also I need to create a .dat file which stores the value of t, f[0][[1]], f[30][[1]] after each iteration.

Can anyone please guide me how to do this? I am new to Mathematica and have difficulties writing loops. Any help would be much appreciated

Please note that the above differential equation is only for demonstration. The actual one cannot be solved analytically.

$\endgroup$
  • $\begingroup$ You might want to use ParametricNDSolve[] in this case since your ODE has a varying parameter. $\endgroup$ – J. M. will be back soon Nov 9 '17 at 14:42
  • $\begingroup$ Could you please explain using an example? $\endgroup$ – Apurba Roy Nov 9 '17 at 15:14
  • 1
    $\begingroup$ What J.M. meant is whether your t should be variable or is it a fixed constant and you're only interested in solving for one specific value. In any case, I'd do something like t = 0.; f = NDSolveValue[{y'[x] == x+2t, y[0] == 1}, y, {x, 0, 30}]; data = Table[{x, f[x]}, {x, 0, 30, .01}]; Export["data.dat", data, "Table"] $\endgroup$ – LLlAMnYP Nov 9 '17 at 15:19
1
$\begingroup$

You are looking for something like this?

data1 = Table[{t, y[0] /. First@NDSolve[{y'[x] == x + 2 t, y[0] == 1}, y, {x, 0, 30}]}, 
{t, 0, 1, 0.01}];

data2 = Table[{t, y[30] /. First@NDSolve[{y'[x] == x + 2 t, y[0] == 1}, y, {x, 0, 30}]}, 
    {t, 0, 1, 0.01}];

pdata = ListPlot[{data1, data2}, PlotStyle -> {Red, {Green, Dashed}}, 
  Joined -> True, PlotRange -> All, Frame -> True]

enter image description here

points = Cases[Normal@pdata, Line[pts_, ___] :> Flatten[pts, Depth[pts] - 3], Infinity];

ifuncts = Interpolation[#, Method -> "Spline", InterpolationOrder -> 2][t] & /@points;

data = Table[Prepend[ifuncts, t], {t, 0, 1, 0.1}];

TableForm[data, TableHeadings -> {None, Prepend[{"y[0]", "y[30]"}, t]}]

enter image description here

Export["C:/tcdata/myfile.txt", data, "Table"]
$\endgroup$
  • $\begingroup$ Funnily enough, since the initial condition is already specified to begin with, y[0] /. First @ NDSolve[{y'[x] == x + 2 t, y[0] == 1}, y, {x, 0, 30}] is a very complicated way to compute 1. :D $\endgroup$ – J. M. will be back soon Nov 10 '17 at 4:38
  • $\begingroup$ @J.M. I agree with you. For the first data point, we dont need to do the calculations but the OP wants it so I did it. $\endgroup$ – zhk Nov 11 '17 at 4:22

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.