0

I used NDSolve to solve a system of ODE, and the resulting n functions y1[x], y2[x],... yn[x] were plotted with the Plot command, as follows,

Plot[{y1[x], y2[x],... yn[x]}/.%,{x,min,max}]

So I need to export the data in .txt multicolumn format. The answer to question [1] provided me a clue,

data = Cases[Plot[Sin@x, {x, 0, 2 Pi}], Line[data_] :> data, -4, 1][[1]]

however, I need the appropriate pattern/specification/etc for Cases. Or, alternatively, is there another way to export data?

Thank you

[1] Plot, extract data to a file

celsodad
  • 3
  • 2
  • 1
    Please include the complete code, including the part that generates the answer you are substituting into plot, or alternatively generate a minimal working example with fake data that still represents the structure of your problem. What you have may not be enough detail for us to help meaningfully. – MarcoB Nov 03 '21 at 12:34
  • What should "/.%" mean? – Daniel Huber Nov 03 '21 at 13:00
  • 1
    Hi @celsodad Welcome to MmaSE, there is so much to learn when one starts here. Please start by taking the [tour] now. It will help us to help you if you write an excellent question. Always edit your question if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive help, we hope you will give back too by voting and answering questions. Please keep the site useful, be kind, correct mistakes and share what you have learned. – rhermans Nov 03 '21 at 13:03
  • 1
    I think this question needs clarification. What is the motivation? Why extract data from a plot instead of using the data that created the plot on the first instance? What is the actual code used? This reduced version is not a minimum working example. The OP should show minimum due diligence, as the limited information provided requires speculation from anybody trying to offer an answer. – rhermans Nov 03 '21 at 13:05
  • "Or another way to export data to .txt": Try data = RandomReal[{-4, 4}, 20] and then: Export["C:\\data.txt", data] . – Syed Nov 03 '21 at 13:28
  • If you just want the Line data try for example: p1 = Plot[{x, x^2}, {x, 0, 2}]; Cases[p1, Line[x_] -> x, [Infinity]] – josh Nov 03 '21 at 13:44
  • Thank you. I reedited the text of the question for clarity. – celsodad Nov 03 '21 at 15:29
  • see also: https://mathematica.stackexchange.com/q/19507/169 – Albert Retey Nov 04 '21 at 10:02

2 Answers2

0

Let's say you solve the following equation:

sol = DSolve[x^2*y''[x] + 5*x*y'[x] + 6*y[x] == 0, y[x], x]
{{y[x] -> (C[2] Cos[Sqrt[2] Log[x]])/x^2 + (C[1] Sin[Sqrt[2] Log[x]])/
    x^2}}

Create a table: modify the range and increments as required.

dexp = Table[{x, y[x] /. sol[[1]]}, {x, 0.1, 0.5, 0.05}] /. {C[1] -> 
    1, C[2] -> 2}

Try both formats:

Export["C:\\dexp_1_2.txt", dexp]
Export["C:\\dexp_1_2.csv", dexp, "CSV"]

You can extend the Table above, by adding more entries according to your solution.

Syed
  • 52,495
  • 4
  • 30
  • 85
0

Thanks a lot. I succesfully adapted the above suggestion for n equations :

sol = NDSolve[{Table[y[i]''[x] + y[i][x], {j, 1, n} == 0, {i, 1, 100}], Table[y[i][0] == i, {i, 1, n}], Table[y[i]'[0] == 0, {i, 1, n}]}, {y[1], y[2], ... y[n]}, {x, 0, 15}]

dexp = Table[{t, y[1][x], y[2][x], ... y[n][x]} /. sol[[1]], {x, 0, 15, .01}]

Export["C:...\dexp_1_2.txt", sol]

(I used NDSolve to account for more complex ODEs)

celsodad
  • 3
  • 2