Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. QuadraticTaylor[{y_, p_, t_, dt_}] := {Clip[y + p dt + 1/2 (y^2 - t) dt^2, {-4, 4}],
  2. p + (y^2 - t) dt, t + dt, dt};
  3.  
  4. With[{Parabola = ContourPlot[x == y^2, {x, 0, 10}, {y, -5, 5},ContourStyle->Black]},
  5. Manipulate[T = NestList[QuadraticTaylor, {0, [Alpha], 0, 10/N}, N];
  6. Show[Parabola,
  7. ListPlot[Map[{#[[3]], #[[1]]} &, T]]], {{[Alpha], 0.92437`}, -10,
  8. 10}, {{dt, 0.1}, 0, 1}, {{N, 10000}, 1, 1000000, 1}]]
  9.  
  10. {inf = 20, pre = 32};
  11. pa = ParametricNDSolveValue[{y''[x] == y@x^2 - x, y[0] == 0, y'[0] == alpha},
  12. y, {x, 0, inf}, alpha, MaxSteps -> Infinity, WorkingPrecision -> pre];
  13.  
  14. {i = 5, init = 92437/10^5};
  15.  
  16. (alphavalue =
  17. NestWhile[If[pa[# + 10^(-i)][inf] < Sqrt[inf], # + 10^(-i), i++; #] &, init,
  18. i <= Floor[pre/2] &] // Quiet) // AbsoluteTiming
  19. (* {36.075637, 9243754874375653/10000000000000000} *)
  20.  
  21. p1 = Plot[#, {x, 0, 20}] &@{x^(1/2), pa[alphavalue][x],
  22. pa[alphavalue + 10^-(pre/2)][x]} // Quiet
  23.  
  24. points = 25;
  25. grid = Array[# &, points, {0, inf}];
  26. difforder = 4;
  27. (* Definition of pdetoae isn't included in this code piece,
  28. please find it in the link above. *)
  29. ptoa = pdetoae[y[x], grid, difforder];
  30. set = {y''[x] == y@x^2 - x, y[0] == 0, y[inf] == Sqrt[inf]};
  31. ae = MapAt[#[[2 ;; -2]] &, ptoa@set, 1];
  32.  
  33. sollst =
  34. FindRoot[ae, {y/@ grid, Sqrt[grid]}[Transpose], WorkingPrecision -> 32][[All, -1]];
  35. sol = ListInterpolation[sollst, grid, InterpolationOrder -> difforder];
  36.  
  37. sol'[0]
  38. (* 1.019443775765894394411167742141 <- Not accurate at all.
  39. This can be improved by using a larger points and difforder,
  40. but not quite economic. *)
  41. (* However, the solution i.e. value of y is accurate enough: *)
  42. p2 = Plot[sol[x], {x, 0, inf}, PlotStyle -> {Red, Thick, Dashed}];
  43. Show[p1, p2]
  44.  
  45. soln[y10_?NumericQ, y20_?NumericQ] :=First@NDSolve[{y1'[t]==y2[t],y2'[t]-y1[t]^2 + t == 0,
  46. y1[0] == y10, y2[0] == y20}, {y1, y2}, {t, 0, 5}];
  47. Plot[Evaluate[{y1[t], y2[t]} /. soln[#, #] & /@Range[-1.5, 0.4, 0.1]], {t, 0, 5},
  48. PlotRange -> All,PlotStyle -> {Red, Green}, AxesLabel -> {"t", "y"},
  49. PlotLegends -> {"y1", "y2"}]
  50.  
  51. ParametricPlot[Evaluate[{y1[t], y2[t]} /. soln[#, #] & /@Range[-1.5, 0.4, 0.1]], {t, 0, 3},
  52. PlotRange -> All,PlotStyle -> {Red, Green}, AxesLabel -> {"y1", "y2"}]
  53.  
  54. restart:with(plots):
  55. ode:=diff(y(t),t,t)=y(t)^2-t:
  56. bcs:=y(0)=0,y(N)=sqrt(N):
  57. N:=50:
  58. A1:=dsolve({ode,bcs},numeric):
  59. odeplot(A1, [[t,y(t), color=red],[t,sqrt(t), color=green,linestyle=3]],0..N,axes=boxed)
  60.  
  61. A1(0)
  62.  
  63. eq = y''[x] == y[x]^2 - x;
  64. ibcs = {y[0] == 0, y[N1] == Sqrt[N1]};
  65. sol = First@NDSolve[Join[{eq}, ibcs], y[x], {x, 0, N1}]
  66. Plot[{y[x]} /. sol, {x, 0, N1}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement