public static class Evaluator
{
public static double Evaluate(double x)
{
return (1 / x + 1 / (x + 1) - 2 * x * x) / 10;
}
};
It then compiles this code and gets a MethodInfo object representing the Evaluator class's Evaluate method (as described in the example Evaluate numeric expressions that are entered by the user in C#).
Next the program loops through x values to draw the graph (as done in the example Graph an equation y = F(x) in C# ). It uses the MethodInfo object's Invoke method to call the Evaluate function for the different x values and uses the results to draw the graph.
The only really new thing to notice here is that you only need to compile the function once and then you can invoke it many times. That's important because compiling the function takes a bit of time so recompiling every time you needed to evaluate the function would be relatively time consuming.
See the previous examples and download this example's code to see the details.
Comments