Assming that I have a function myFunc
which has some options.
Options[myFunc]={ firstOpt->1, secondOpt->"A", thirdOpt->True };
where, I set the values of firstOpt
to 1 or 2,and set the value secondOpt
to "A" or "B".
myFunc[arg1_,arg2_,OptionsPattern[]]:=
Module[{method},
method= OptionValue/@{firstOpt,secondOpt,thirdOpt};
Switch[
method,
{1,"A",True},subFunc1[...],
{2,"A",True},subFunc2[...],
{1,"B",True},subFunc3[...],
{2,"B",True},subFunc4[...],
{1,"A",False},subFunc4[...],
{2,"A",False},subFunc5[...],
{1,"B",False},subFunc6[...],
{2,"B",False},subFunc7[...],
]
]
Obviously, this is a fussy and awkward solution.So I would like to know how to deal with condition when myFunc
has many options.
Or is it possible to know Mathematica how to deal with many options? For instance,
Length@Options@ArrayPlot
(*48*)
An example(Implementation of Runge-Kutta Algorithm)
(*MiddlePoint formula*)
middlePointOrderTwo[{xn_, yn_}, step_, func_] :=
Module[{K1, K2},
K1 = func[xn, yn];
K2 = func[xn + 1/2 step, yn + 1/2 step K1];
{xn + step, yn + step K2}
]
(*Henu formula of order 2*)
henuOrderTwo[{xn_, yn_}, step_, func_] :=
Module[{K1, K2},
K1 = func[xn, yn];
K2 = func[xn + 2/3 step, yn + 2/3 step K1];
{xn + step, yn + 1/4 step (K1 + 3 K2)}
]
(*Henu formula of order 2*)
henuOrderThree[{xn_, yn_}, step_, func_] :=
Module[{K1, K2, K3},
K1 = func[xn, yn];
K2 = func[xn + 1/3 step, yn + 1/3 step K1];
K3 = func[xn + 2/3 step, yn + 2/3 step K2];
{xn + step, yn + 1/4 step (K1 + 3 K3)}
]
(*Kutta formula of order 3*)
kuttaOrderThree[{xn_, yn_}, step_, func_] :=
Module[{K1, K2, K3},
K1 = func[xn, yn];
K2 = func[xn + 1/2 step, yn + 1/2 step K1];
K3 = func[xn + step, yn - step K1 + 2 step K2];
{xn + step, yn + 1/6 step (K1 + 4 K2 + K3)}
]
(*Runge-Kutta formula of order 4*)
rungeKuttaOrderFour[{xn_, yn_}, step_, func_] :=
Module[{K1, K2, K3, K4},
K1 = func[xn, yn];
K2 = func[xn + 1/2 step, yn + 1/2 step K1];
K3 = func[xn + 1/2 step, yn + 1/2 step K2];
K4 = func[xn + step, yn + step K3];
{xn + step, yn + 1/6 step (K1 + 2 K2 + 2 K3 + K4)}
]
rungeKuttaFormula[{a_, b_}, ya_, step_, func_, OptionsPattern[]] :=
Module[{OrderMethod, num},
OrderMethod = OptionValue[SolvingOrderMethod];
num = IntegerPart[(b - a)/step];
Switch[
OrderMethod,
{2, "Henu"},
NestList[
henuOrderTwo[#, step, func] &, {a, ya}, num],
{2, "MiddlePoint"},
NestList[
middlePointOrderTwo[#, step, func] &, {a, ya}, num],
{3, "Henu"},
NestList[
henuOrderThree[#, step, func] &, {a, ya}, num],
{3, "Kutta"},
NestList[
kuttaOrderThree[#, step, func] &, {a, ya}, num],
{4, "RungeKutta"},
NestList[
rungeKuttaOrderFour[#, step, func] &, {a, ya}, num]]
]
WorkingPrecision
in theRKSolve
like the built-inNSolve
. For instaneNSolve[x^2 + 2 x - 2 == 0, x, WorkingPrecision -> 20]
gives the result{{x -> -2.7320508075688772935}, {x -> 0.73205080756887729353}}
. However, I didn't know how to implement it. Could you give me some suggestions or hints. Thanks:) $\endgroup$N[]
anywhere in your code, so you will need to insertN[#, OptionValue[WorkingPrecision]] &
in the appropriate places. $\endgroup$