MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Multiple Executions of .nb file part2

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90512] Re: Multiple Executions of .nb file part2
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Fri, 11 Jul 2008 02:04:46 -0400 (EDT)
  • References: <g54op3$f50$1@smc.vnet.net>

Dave wrote:
> Hi,
> 
> I think I maybe over simplified my situation. My project uses a series of 
> nested differential equations. These run over a given time and then print 
> either a graph or the plot points or both (depending on my needs).
> 
> An example of the code is:
> 
> ****************************
> 
> 
> Off[General::spell]
> 
> time = 500;
> 
> SmV = -80;
> 
> 
> 
> PD2mnax0[PD2u_] := 1/(1 + Exp[-(PD2u + 24.7)/5.29]);
> 
> PD2hnax0[PD2u_] := 1/(1 + Exp[(PD2u + 48.9)/5.18]);
> 
> PD2mk10x[PD2u_] := 1/(1 + Exp[-(PD2u + 14.2)/11.8]);
> 
> PD2taumnax[PD2u_] := 1.32 - 1.26/(1 + Exp[-(PD2u + 120)/25]);
> 
> PD2tauhnax[PD2u_] := (0.67/(1 + Exp[-(PD2u + 62.9)/10]))*(1.5 + 1/(1 + Exp[(
> 
> PD2u + 34.9)/3.6]));
> 
> PD2taumk1[PD2u_] := 7.2 - 6.4/(1 + Exp[-(PD2u + 28.3)/19.2]);
> 
> 
> 
> 
> 
> solution = NDSolve[{
> 
> 
> PD2mnax'[t] == (PD2mnax0[PD2z[t]] - PD2mnax[t])*(1/
> 
> PD2taumnax[PD2z[t]]), PD2mnax[0] == PD2mnax0[-80]*(1/
> 
> PD2taumnax[-80]), PD2hnax'[t] == (PD2hnax0[PD2z[t]] - PD2hnax[
> 
> t])*(1/PD2tauhnax[PD2z[
> 
> t]]), PD2hnax[
> 
> 0] == PD2hnax0[-80]*(1/PD2tauhnax[-80]),
> 
> PD2mkx'[t] == (PD2mk10x[PD2z[t]] - \
> 
> PD2mkx[t])*(1/PD2taumk1[PD2z[t]]),
> 
> PD2mkx[0] == PD2mk10x[-80]*(1/PD2taumk1[-80]), PD2z'[t] == (-1*(0
> 
> + 1*1100*(PD2mnax[t]^3)*PD2hnax[t]*(PD2z[t] - 50)
> 
> + 1*150*(PD2mkx[t]^4)*(PD2z[t] + 80)
> 
> + 1*0.00081*(PD2z[t] + 50)
> 
> )
> 
> *(1/6)),
> 
> PD2z[0] == SmV
> 
> 
> },
> 
> 
> 
> 
> {PD2z, PD2mnax,
> 
> PD2hnax, PD2mkx}, {t, 0, time}, Method -> Automatic, MaxSteps -> \
> 
> Infinity];
> 
> 
> 
> ParametricPlot[Evaluate[{t, PD2z[
> 
> t]} /. solution], {t, 0, time},
> 
> PlotPoints -> time, PlotRange -> {-80, 50}, PlotLabel -> "PD Axon"];
> 
> (*
> 
> mkw = Table[0, {time-1}];
> 
> For[w=1,w<(time),mkw[[w]]=PD2z[w]/.solution;w++];
> 
> mkw
> 
> *)
> 

> 
> This produces what looks like a flat line on a graph.
> 
> I need to run this file several times changing the variable SmV. Can I 
> encapsulate the whole thing in a function? I have tried but it doesn't work.
> 
> 
> fun[]:= (SmV;
> 
> ***code***
> 
> )
> 
> fun[-80]
> 
> out- fun[-80]
> 
> 
> 
> Thank you
> 
> David Fourie
> 
> 
The real answer to this problem is to change your perspective a little. 
The best way to write Mathematica code is as a series of function 
definitions. You have already got some definitions in the above code - 
e.g. PD2taumk1, so you want to start to think about more complex 
functions that use Module to give you local variables, and contain 
multiple statements:

performTask[x_]:=Module[{tmp},
         tmp=x^2;
         D[tmp,x]
       ]

So you get:

performTask[q]

2 q


Once you have mastered functions defined using Module, you can add loops 
(or use other more advanced techniques), make functions call other 
functions, etc. as required.

The absolutely crucial point is that your function definitions can be 
spread across as many notebooks as required - when you execute the 
definitions (e.g. by executing the whole notebook) these definitions 
will all end up in the kernel, where they can be used in combination.

It is so easy to start off driving Mathematica using top level 
statements - as you have done - and then find things more and more 
clumsy as you try to scale things up.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: Problem parsing date formats using Import
  • Next by Date: Re: What does FullForm[ ] actually do?
  • Previous by thread: Re: Multiple Executions of .nb file part2
  • Next by thread: Re: Multiple Executions of .nb file part2