Hello everyone,
Consider following function:
~~~
function funca(problem, x)
if problem == 1
return x^2;
elseif
problem == 2
return sin(x);
end
end
~~~
I have shown only 2 cases. However, there are around 100 cases. These
`if ... else` statements are being evaluated unnecessarily. If I use
`switch` structure then it will be faster. But I want to do away with
these.
Since the arguments and their datatypes remain the same, I don't know
how to create multiple methods for function.
What is the best strategy to achieve this?
Background:
I am trying to implement a library of ODE solvers for my students.
$y`` + a(x)y` + b(x) = f(x) $
Above function calculates $a(x)$ for a given problem at a specific $x_0$.
So there should various ODE problems defined via the coefficients of
the derivative terms. My students will then implement various solution
strategies and compare their performance for these problems. So I need
to provide
them with functions `funca` `funcb` `funcf` which give the appropriate
coefficient depending on the problem.
--
Cheers,
Devendra