Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Option instead of a function argument: is it possible?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98073] Re: Option instead of a function argument: is it possible?
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sun, 29 Mar 2009 02:47:05 -0500 (EST)

On 3/28/09 at 5:44 AM, Alexei.Boulbitch at iee.lu (Alexei Boulbitch)
wrote:

>1. Consider a function that solves a system of 2 ordinary
>differential equations and draws a trajectory on the (x, y) plane:

>trajectory1[eq1_, eq2_, point_, tmax_] :=
>Module[{s, eq3, eq4},
>eq3 = x[0] == point[[1]];
>eq4 = y[0] == point[[2]];
>s = NDSolve[{eq1, eq2, eq3, eq4}, {x, y}, {t, tmax}];
>ParametricPlot[Evaluate[{x[t], y[t]} /. s], {t, 0, tmax},
>PlotRange -> All]]

>Equations can be fixed say, like these:

>eq1 = x'[t] == -y[t] - x[t]^2;
>eq2 = y'[t] == 2 x[t] - y[t]^3;

>and initial conditions are passed by the parameter point. The
>function can be called:

>trajectory1[eq1, eq2, {1, 1}, 30]

>2. Assume now that I need to specify the accuracy goal and MaxSteps
>parameters. Then the function will take a slightly different form:

<code with additional arguments snipped>

>However, I would like to achieve a function

>trajectory3[eq1_, eq2_, point_, tmax_]

>that can be addressed both as

>trajectory3[eq1, eq2, {1, 1}, 30]

>(if I agree with the default values of the AccuracyGoal and
>MaxSteps) and as

>trajectory3[eq1, eq2, {1, 1}, 30, AccuracyGoal->10,
>MaxSteps->10000],

>if a change in these options is necessary.  Is it possible?

Yes, rewriting your function as:

trajectory1[eq1_, eq2_, point_, tmax_, opts___] :=
  Module[{s, eq3, eq4},
   eq3 = x[0] == point[[1]];
       eq4 = y[0] == point[[2]];
   s = NDSolve[{eq1, eq2, eq3, eq4}, {x, y}, {t, tmax}, opts];
           ParametricPlot[Evaluate[{x[t], y[t]} /. s], {t, 0, tmax},
    PlotRange -> All]]

will do the trick

Even better would be to use the function FilterRules as:

trajectory1[eq1_, eq2_, point_, tmax_, opts___] :=
  Module[{s, eq3, eq4},
   eq3 = x[0] == point[[1]];
       eq4 = y[0] == point[[2]];
   s = NDSolve[{eq1, eq2, eq3, eq4}, {x, y}, {t, tmax}, Sequence@@FilterR=
ules[{opts},Options[NDSolve]]];
           ParametricPlot[Evaluate[{x[t], y[t]} /. s], {t, 0, tmax},
    PlotRange -> All]]

Using FilerRules will prevent passing invalid options to
NDSolve. Also, for more complex problems using FilterRules
allows you to include options for other functions and pass only
the appropriate options on to each function you have in the body
of your code.

Finally, I note you can alter the AccuracyGoal and MaxSteps
without changing your original code. For example, doing
SetOptions[NDSolve, AccuracyGoal->10] before evaluating your
code will cause it to work with an AccuracyGoal of 10.




  • Prev by Date: Re: Option instead of a function argument: is it possible?
  • Next by Date: Re: Using a variable set elsewhere within a function that has
  • Previous by thread: Re: Option instead of a function argument: is it possible?
  • Next by thread: Re: Option instead of a function argument: is it possible?