0

How to output data after solving an equation? That is, the output x, and the y value corresponding to x.

ysol = NDSolveValue[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]

In addition, is there any way to improve the calculation accuracy of NDsolveValue? I want the higher the accuracy, the better.

gufe
  • 65
  • 5
  • @xzczd Your answer in this link (https://mathematica.stackexchange.com/questions/261065/numerical-solutions-of-active-1d-wave-equations(Section Quick Fix for the FDM Code of OP)) , I found that the result of h=1/4 or 1/6 is the same, and the result is always larger than the real value. Do you know what caused this? – gufe Apr 09 '22 at 05:42
  • When you talk about accuracy, is it possible that you mean more significant digits? – bmf Apr 09 '22 at 06:38
  • By default NDSolveValue will use machine precision calculations. To use arbitrary-precision, specify a WorkingPrecision, i.e., add the option WorkingPrecision -> 20 where the 20 is set to whatever required precision. – Bob Hanlon Apr 09 '22 at 13:03

1 Answers1

1
ysol = NDSolveValue[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, 
  y[x], {x, 0, 30}]

xdata = Table[xx, {xx, 0, 30}];
ydata = Table[ysol /. x -> xx, {xx, 0, 30}];

output = Transpose[{xdata, ydata}];

output // TableForm

table

Edit: to output the data you can save the notebook to some path. The following will create a file in that path.

SetDirectory@NotebookDirectory[]
Export["myFile.mx", Transpose[{xdata, ydata}]]

and then you can import

Import["myFile.mx"]

Edit: pertaining to WorkingPrecision

ysol = NDSolveValue[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, 
  y, {x, 0, 30}, WorkingPrecision -> 100, 
  Method -> "StiffnessSwitching", MaxSteps -> 10^6]

And now you can check the value of the function at $x=30$ for example -it works in the same way for the other values

ysol[x] /. x -> 30

wp

bmf
  • 15,157
  • 2
  • 26
  • 63
  • If I want to output to a file, what should I do? – gufe Apr 09 '22 at 05:44
  • @sfks please have a look at the edit :) Is this what you wanted? – bmf Apr 09 '22 at 05:48
  • The result is not two columns. I want two columns without brackets. – gufe Apr 09 '22 at 05:55
  • @sfks if you could spend some time to check Import["myFile.mx"] // Dimensions you would have seen that the result has two columns. Are you familiar with Parts and the Mathematica documentation? If not you can find more information online in the docs. I am saying this, because your request is very obscure. You said that you wanted the x and the corresponding y values. I did that for you and now you say you don't want that. I am confused – bmf Apr 09 '22 at 06:00
  • Thank you very much, you have helped me solve the problem of data output. – gufe Apr 09 '22 at 06:19
  • @sfks glad I was able to help :-) – bmf Apr 09 '22 at 06:19
  • @sfks I added another edit showing you how you can obtain more significant digits in your result. Can you have a look and let me know if that's what you wanted? – bmf Apr 09 '22 at 16:45
  • Thank you very much! – gufe Apr 10 '22 at 15:11
  • @sfks Glad I was able to help. If you found this answer helpful, you might want to consider accepting it by clicking the checkmark next to it – bmf Apr 10 '22 at 15:50