0
$\begingroup$

I am Trying to solve x'[t]+a*x[t]==0,x[0]==1, for x with {t,0,10} using NDSolve.

Clear[a, x, t]; Flatten[Table[{s = NDSolve[{x'[t] + a*x[t] == 0, x[0] == 1}, x, {t, 0, 100}];
Table[x[t] /. s, {t, 0, 10}], a}, {a, 0, 10}]]

I got a list of x and a.list of values of x

now I apply r = Select[%, -1 < # < 1 &] to select only those x which lie between -1 to 1, selected list

Now as there may be repeated values of x, I delete them by using p = DeleteDuplicates[r] . Now , I want to get the values of x and all the corresponding a, including those a as well which correspond to the repeated values of x. How can I do this.

$\endgroup$
3
  • 1
    $\begingroup$ Is there a reason why you are not doing x/.DSolve[{x'[t]+a*x[t]==0, x[0]==1}, x,t] which gives you x[t]=E^-(a t) and you can calculate any desired values of x[t] and a from that $\endgroup$
    – Bill
    Commented Mar 4, 2019 at 8:17
  • $\begingroup$ because I want to solve Mathieu equation, and using DSolve gives x[t] as a combination of MathieuS , MathieuC , MathieuSPrime, which are not easy to solve. So by using the reasoning of following problem I can solve the Mathieu equation and plot the stability diagram between a and q. That is why I need those a and q for which x is between some limiting value, and I don't want to use inbuilt function to do this. $\endgroup$ Commented Mar 4, 2019 at 10:30
  • 1
    $\begingroup$ I believe this is another example of "the XY problem" which is explained here: en.wikipedia.org/wiki/XY_problem. This happens when a person really wants to solve problem X, but can't, so they create a different problem Y and ask how to solve it without any hint what X is. Readers are puzzled and confused, but start trying to help solve Y, The answers to Y are often of no help at all for solving X. IF there were a way to put a brief very well written description how to avoid this in the posting process description for new users then perhaps we could avoid some of this. $\endgroup$
    – Bill
    Commented Mar 4, 2019 at 18:06

1 Answer 1

2
$\begingroup$
Clear["Global`*"];

eqns = {x'[t] + a*x[t] == 0, x[0] == 1};

As pointed out by Bill in the comments, for comparison the exact solution is

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

(* {x -> Function[{t}, E^(-a t)]} *)

Verifying,

eqns /. sol1

(* {True, True} *)

table1 = Table[x[t] /. sol1, {a, 1, 10}, {t, 0, 10}];

Since there is a parameter in the differential equation, use ParametricNDSolve

sol2 = ParametricNDSolve[eqns, x, {t, 0, 10}, a, WorkingPrecision -> 45]

enter image description here

Comparing the approximate numeric results with the exact results (see Chop):

table2 = Table[(x[t] /. sol2)[a], {a, 1, 10}, {t, 0, 10}];

table1 - table2 // Chop

(* {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} *)

EDIT: Duplicate values of x occur whenever the products of a and t are identical.

table3 = DeleteDuplicates[
  Table[
    {(x[t] /. sol2)[a] // N, a, t},
    {a, 1, 10}, {t, 0, 10}] //
   Flatten[#, 1] &,
  ((Times @@ Rest[#1]) == (Times @@ Rest[#2]) &)]

{{1., 1, 0}, {0.367879, 1, 1}, {0.135335, 1, 2}, {0.0497871, 1, 
  3}, {0.0183156, 1, 4}, {0.00673795, 1, 5}, {0.00247875, 1, 
  6}, {0.000911882, 1, 7}, {0.000335463, 1, 8}, {0.00012341, 1, 
  9}, {0.0000453999, 1, 10}, {6.14421*10^-6, 2, 6}, {8.31529*10^-7, 2,
   7}, {1.12535*10^-7, 2, 8}, {1.523*10^-8, 2, 9}, {2.06115*10^-9, 2, 
  10}, {3.05902*10^-7, 3, 5}, {7.58256*10^-10, 3, 7}, {3.77513*10^-11,
   3, 8}, {1.87953*10^-12, 3, 9}, {9.35762*10^-14, 3, 
  10}, {6.9144*10^-13, 4, 7}, {1.26642*10^-14, 4, 8}, {2.31952*10^-16,
   4, 9}, {4.24833*10^-18, 4, 10}, {1.38879*10^-11, 5, 
  5}, {6.30512*10^-16, 5, 7}, {2.86261*10^-20, 5, 9}, {1.94854*10^-22,
   5, 10}, {5.74949*10^-19, 6, 7}, {1.4236*10^-21, 6, 
  8}, {3.74594*10^-24, 6, 9}, {-5.64139*10^-25, 6, 
  10}, {5.24831*10^-22, 7, 7}, {2.06509*10^-25, 7, 
  8}, {-1.67134*10^-25, 7, 9}, {5.40872*10^-26, 7, 
  10}, {-1.32346*10^-25, 8, 8}, {-1.35923*10^-26, 8, 
  9}, {4.91701*10^-27, 8, 10}, {3.00999*10^-27, 9, 
  9}, {9.54615*10^-28, 9, 10}, {7.64079*10^-28, 10, 10}}
$\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.