K
K
kykyryky2015-04-11 02:34:35
Programming
kykyryky, 2015-04-11 02:34:35

How to choose which function to call during program execution?

Hello. Please do not scold me for the crazy name of the question, I have no idea how to call such a thing correctly.
I have a method that searches for the minimum of a function of two variables. In this method, to calculate the value of the function for given variables, a method is called that looks like this:

double f1(double x1, double x2)
        {
            return 18 * x1 * x1 - 18 * x1 - 12 * x1 * x2 + 8 * x2 * x2 - 12 * x2;
        }

To solve my problem for another equation, for example, I can write a function f2 with a similar body and do the same, but this will either replace f1 with f2 in the old method, or create the same new method, but with f2.
Is there a better way to implement this using C#?
So far, all that has come to mind is to present the function as a string and parse the expression. But that's not an option.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mrrl, 2015-04-11
@kykyryky

For example, like this:

double FindMinimum(Func<double,double,double>F,out double x1,out double x2){
     x1=x2=0;
     return F(x1,x2);
}

Then the call to find the minimum will be double zmin=FindMinimum(f1,out x,out y);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question