Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

A few weeks ago I asked a question about manipulating parameters in NDSolve (Looping with NDSolve?) , and received some very helpful advice about the manipulate environment; now I want to take the result of these varying parameters and fit them to lab data. I have a series of equations to be solved, and two unknown variables $kme$ and $kmn$. I specify a range for these and a step size. The following code outlines shows what's happening; I solve am initial PDE ($Ox$ ) and use that result to solve two further PDEs, $Ef$ and $Eb$. Both of these depend on $kme$ and $kmn$, and I am interested in the output of $Eb$. The following code outlines what I'm doing so far - apologies in advance, the equation inside the manipulate command is extremely ugly!

(*Some constants used here*)

a = 7.5*10^-7;
omega = 3.0138*10^7;
Do2 = 2*10^-9;
po = 100;
ro = 300*10^-6;
micron = 1*10^-6;
k = 1;  
De = 5.5*10^-11;
eo = 100;
qm  = 10^-4;

(*Then we solve an initial equation and take values from it... *)

s = Quiet[NDSolve[{D[Ox[r, t], t] - Do2*(D[Ox[r, t], r, r] + (2/r)*(D[Ox[r, t], r])) + (a*
      omega)*((Ox[r, t])/(Ox[r, t] + k)) == 0, Ox[r, 0] == 0, 
 Ox[micron, t] == 0, Ox[ro, t] == po}, 
Ox, {r, micron, ro}, {t, 0, 14400}]];


p = Ox /. First[s];

(*Then we set up our other complicated expression in the MANIPULATE environment; *)


Quiet[Manipulate[Plot[Evaluate[Eb1[r, 14400] /. NDSolve[{D[Eb1[r, t], t] - 
     qm*((kme)/(kme + p[r, t])*((p[r, t])/(p[r, t] + kmn)))*
      First[Evaluate[Ef1[r, t] /. NDSolve[{D[Ef1[r, t], t] - 
             De*(D[Ef1[r, t], r, r] + (2/r)*(D[Ef1[r, t], r])) + qm*((kme)/(kme + 
                p[r, t])*((p[r, t])/(p[r, t] + kmn)))*Ef1[r, t] ==
             0, Ef1[r, 0] == 0, Ef1[micron, t] == 0, Ef1[ro, t] == eo}, 
          Ef1, {r, micron, ro}, {t, 0, 14400}]]] == 0, Eb1[r, 0] == 0}, Eb1, {r, micron, ro}, {t, 0, 14400}]], {r, 
micron, ro}, PlotRange -> All], {kme, 0.1, 60, 0.1}, {kmn, 0.1, 60, 0.1}]]

Getting this far gives me a workable manipulate environment; now I have some lab data I want to compare to this output. I have 600 values of $Kmn$ and 600 values of $Kme$ - is it possible to "output" the result for each of these in some automated fashion to compare it against the lab data? Either in a format I can use inside Mathematica or an interpolating function for each that can be exported to MATLAB / excel etc? I would be very grateful for any guidance on how to do this and the syntax!

share|improve this question
    
If you've got experimental data that you wish to fit to interpolated functions from something like NDSolve, then ParametricNDSolve may be of use as it was here. –  bobthechemist Feb 17 at 19:04
    
How about using Show[]? Inside, you Plot[] the function you have above and then ListPlot[] the corresponding data. Show will superimpose the two plots. –  bill s Feb 17 at 19:16
    
Thanks for suggestions, but there's a further complication; the data is unscaled, so I have to find the scaling factor ( where maximum of the simulation output matches relative data maximum) then perform a fit analysis; for this reason I just want an automated way to export the outputs and save them into a file that I can write a MATLAB script to handle; any ideas ? I could manually export each iteration but this would take a very, very long time! –  DRG Feb 18 at 14:37
    
Perhaps I could dump each function or the output of each function to various files with naming syntax data_kme_kmn_j . xls or .csv - would that be possible? And does anyone know how to automate this? –  DRG Feb 18 at 14:46
    
You can also write directly to Matlab .mat files using Export and you can send more complex information between Mathematica and Matlab using MatLink. matlink.org –  bill s Feb 18 at 19:57

1 Answer 1

I have a partial answer, but it's not very useful; I can export the interpolating functions but not extract values from them. To do this, I need to increase the Java memory with;

Needs["JLink`"];
ReinstallJava[JVMArguments -> "-Xmx6000m"];

and then using table and export;

w = Quiet[Table[Evaluate[Eb1[r, 14400] /. 
  NDSolve[{D[Eb1[r, t], t] - 
      qm*((kme)/(kme + 
             p[r, t])*((p[r, t])/(p[r, t] + 
              kmn)) + (1 - (p[r, t])/(p[r, t] + kmn))*j)*
       First[Evaluate[
         Ef1[r, t] /. 
          NDSolve[{D[Ef1[r, t], t] - 
              De*(D[Ef1[r, t], r, r] + (2/r)*(D[Ef1[r, t], r])) + 
              qm*((kme)/(kme + 
                p[r, t])*((p[r, t])/(p[r, t] + 
                kmn)) + (1 - (p[r, t])/(p[r, t] + kmn))*j)*
               Ef1[r, t] == 0, Ef1[r, 0] == 0, 
            Ef1[micron, t] == 0, Ef1[ro, t] == eo}, 
           Ef1, {r, micron, ro}, {t, 0, 14400}]]] == 0, 
    Eb1[r, 0] == 0}, Eb1, {r, micron, ro}, {t, 0, 14400} ]], {kme,
  0.1, 15, 0.1}, {kmn, 0, 15, 0.1}, {j, 0, 0.2, 0.01}]];
Export["testoutput.xlsx", w ];

This gives me an output file filled with interpolating functions (Kme is sheet number, j is x-axis and kmn is y-axis) but as of yet I have no way of importing these into MATLAB as values. Anyone with any better ideas and I'd be grateful!

share|improve this answer
    
I tried adding a line like this; U = Eb1[{r, mic, ro, mic}, 14400] /. w; , but it just didn't work. Can anyone steer me right on this? –  DRG Feb 18 at 18:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.