I have two functions h[r]
and c[r]
defined by two differentials equation.
operh[h_, c_] := h D[D[h, r, r] + 1/r D[h, r] - 1/lc^2 h, r] + m D[c, r]
operc[h_, c_] :=
cmt h + ξ ((ϕ - H) (1 - ϕ) )/Sqrt[R^2 - r^2] -
1/r D[r h (1 + e^2/1680 h^4 (D[c, r])^2 ) D[c, r], r]
I numerically solve them with NDSolve (with solution called "s" here under). Then I plot them with no problem:
Plot[h[r] /. s, {r, rleft, rright}, PlotRange -> All, AxesOrigin -> {0, 0}]
Plot[c[r] /. s, {r, rleft, rright}, PlotRange -> All, AxesOrigin -> {0, 0}]
With same rright and rleft.
What I want is a .txt or .csv file with 3 columns, containing r, h[r]
and c[r]
. From what I have read on this site I tried:
ploth =
Plot[h[r] /. s, {r, rleft, rright}, PlotRange -> All, AxesOrigin -> {0, 0}]
pts = Cases[ploth, Line[pts_] :> pts, Infinity][[1]]
Export[SystemDialogInput["FileSave", "filename.csv"], pts]
It gave me a list of 157 pairs {r, h[r]}
. Then I did the same for c:
plotc =
Plot[c[r] /. s, {r, rleft, rright}, PlotRange -> All, AxesOrigin -> {0, 0}]
pts = Cases[plotc, Line[pts_] :> pts, Infinity][[1]]
Export[SystemDialogInput["FileSave", "filename.csv"], pts]
And it gave me a list of 226 pairs {r, c[r]}
. This is not the same number of data than for h(r), so I can't merge them.
I tried by plotting the two curves on a single graph but it gave me only one of the two data sets.
I also tried to use Table but it gave me an empty list:
tableh = Table[{r, ploth}, {r, rleft, rright}]
{}
Do you know how to extract h
and c
data for common r
values?
I'm on Mathematica 7.0
Answer
An approach to this issue I like is to rely on the fact that NDSolve
found the solution on the same grid points for both functions in the first place:
a simple example:
sol = First@NDSolve[ {f''[x] + g'[x] + 1 == 0 , g[x] == x^2, f[0] == 0,
f'[0] == 1}, {f, g}, {x, 0, 1}];
Plot[{f[x] /. sol, (g[x] /. sol)}, {x, 0, 1}]
here we do not actually use the interpolation but extract the grid values:
gridpoints = Flatten[(g /. sol)["Grid"]];
computeddata =
Transpose[{gridpoints,
(f /. sol)["ValuesOnGrid"],
(g /. sol)["ValuesOnGrid"]}];
ListPlot[{ computeddata[[All, {1, 2}]], computeddata[[All, {1, 3}]]},
Joined -> True, PlotMarkers -> Automatic]
here computeddata
is a nice array you can simply export with each row {x,f[x],g[x]}
An advantage to this is the point spacing is naturally refined as was needed by NDSolve
Comments
Post a Comment