Euler's programming function : differential equation as a parameter

99 views Asked by At

I have a programming function written for Eurler's approximations. Currently the function only takes 3 parameters.

  1. step size
  2. starting f(x)
  3. endting f(x) which is what we are approximating

Each time I have to use Euler, I have to keep on changing my function's differential equation.

E.g. euqation 1

f'(x) = 3x^{2} - 7

equation 2

f'(x) = f(x) + 2

I want to send differential equation as a paramter. How can I do so?

I am using C#, VBA. Don't have Matlab installed at the moment. But I am willing to try out in Python although I am new to it.

ps: I checked on this question. Quite hard to understand the case there...

1

There are 1 answers

1
penjepitkertasku On

perhaps this can help :

I use C# as an example

public static double equation(double x, Func<double, double> f)
{
    return f(x);
}

static void Main(string[] args)
{
    //f'(x) = 3x^{2} - 7
    double result1 = equation(5, x => Math.Pow(3 * x, 1 / 2) - 7);

    //f'(x) = f(x) + 2
    double result2 = equation(5, x => x + 2); 

    Console.WriteLine(result1);
    Console.WriteLine(result2);
}