You can get it about 5-6 times faster if you define a separate function (to do Transpose
just once):
ClearAll[f];
f[x_?NumericQ] =
# * UnitStep[First @ #] * UnitStep[Last @ #] & /@
Transpose[{solutionsA[x, a, b], solutionsB[x, a, b]}],
and the presence of UnitStep
makes Select
unnecessary, with visually the same output. You can then plot it as e.g.
Plot[f[x], {x, 0, 10}, Exclusions -> {Cos[b*x] == 0, Sin[b*x] == 0}, PlotRange -> {Automatic, {0, 7}}]
A cleaner way to do this is to have a function generator that would embed your parameters, so that you don't have a dependence on global variables a
and b
:
makeF[a_, b_] :=
Function[
x,
# * UnitStep[First @ #] * UnitStep[Last @ #] & /@
Transpose[{solutionsA[x, a, b], solutionsB[x, a, b]}]
]
and then
ClearAll[f];
f = makeF[a,b]
and then use f
as before.
To get some further speed-up, you can compile this. Here is the corresponding function generator:
ClearAll[makeFC];
makeFC[a_, b_, opts___?OptionQ] :=
Block[{x},Compile[x, Evaluate[makeF[a, b][x], opts]]]
Now you can do
fc = makeFC[a, b];
and then plot fc
. I can't test now, but presumably by passing CompilationTarget -> "C"
to makeFC
, you can get the plotting faster still.