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: [mg98053] Re: [mg98037] Option instead of a function argument: is it possible?
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sun, 29 Mar 2009 02:43:21 -0500 (EST)
  • References: <27334345.1238240356951.JavaMail.root@m02>

Alexei,

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

Options[trajectory2] = {TrajectoryAccuracyGoal -> Automatic, 
   TrajectoryMaxSteps -> 10000};

trajectory2[eq1_, eq2_, point_, tmax_, OptionsPattern[]] := 
 Module[{s, eq3, eq4, accuracyGoal,
   maxSteps},
  accuracyGoal = OptionValue[TrajectoryAccuracyGoal];
  maxSteps = OptionValue[TrajectoryMaxSteps];
  eq3 = x[0] == point[[1]];
  eq4 = y[0] == point[[2]];
  s = NDSolve[{eq1, eq2, eq3, eq4}, {x, y}, {t, tmax},
    AccuracyGoal -> accuracyGoal,
    MaxSteps -> maxSteps];
  ParametricPlot[Evaluate[{x[t], y[t]} /. s], {t, 0, tmax}, 
   PlotRange -> All]]


trajectory2[eq1, eq2, {1, 1}, 30]

trajectory2[eq1, eq2, {1, 1}, 30,
 TrajectoryMaxSteps -> 200,
 TrajectoryAccuracyGoal -> 2]

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  


From: Alexei Boulbitch [mailto:Alexei.Boulbitch at iee.lu] 

Dear Community,

If we need to write functions depending upon several parameters the 
latter are usually passed to the function as its arguments. I wonder 
however, if you know a way to pass some parameters to a function in the 
same way as options are passed to operators in Mathematica. That is, if 
the default value of the parameter in question is OK,  you  do not even 
mention such a parameter among the function arguments. If you need to 
specify such a parameter you include an argument having the form 
Option->optionValue.

Let me explain precisely within a simple example what I would like to:

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:

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

and also called:

trajectory2[eq1, eq2, {1, 1}, 30, 10, 1000]

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?
 
Best regards, Alexei


-- 
Alexei Boulbitch, Dr., habil.
Senior Scientist

IEE S.A.
ZAE Weiergewan
11, rue Edmond Reuter
L-5326 Contern
Luxembourg

Phone: +352 2454 2566
Fax:   +352 2454 3566

Website: www.iee.lu

This e-mail may contain trade secrets or privileged, undisclosed or
otherwise confidential information. If you are not the intended recipient
and have received this e-mail in error, you are hereby notified that any
review, copying or distribution of it is strictly prohibited. Please inform
us immediately and destroy the original transmittal from your system. Thank
you for your co-operation.






  • Prev by Date: Re: rules on vector sequence
  • Next by Date: Re: Using a variable set elsewhere within a function that has attribute
  • 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?