Thursday, September 13, 2018

Euler's Method - C#

Hey everybody. Today I'll be presenting the third of my C# programs that I've been working on while waiting for the new printer nozzle. Like I said last time, this one is a differential equation solver that makes use of Euler's method. As a little recap, Euler's method (first devised by, surprisingly, Euler) is a first-order numerical procedure that generates approximate solutions to ordinary differential equations (ODEs) given an initial value. The method works by calculating the tangent slopes of the ODE's solution at different points set from the initial value, thus producing a curve that approximately solves the differential equation. The formula for Euler's method is as follows:


Where  is the next y-value to be calculated, is the previous calculated y-value (or the initial y-value when n = 0), is the step by which the x-value is incremented, and is the differential equation calculated at the given x- and y-values. Next up will be the code I implemented to solve the above equation for any given ODE:

using System;
using System.Data;

namespace euler_method
{
    class Program
    {
            static void Euler(double x0, double y, double h, double x, string e)
            {
                while (x0 < x)
                {
                    string e0 = e.Replace("x", x0.ToString()).Replace("y", y.ToString());
                    y = y + h * Convert.ToDouble(new DataTable().Compute(e0, null));
                    x0 = x0 + h;
                    Console.WriteLine("Approximate solution at x = " + x + " is " + y);
                }
                Console.ReadLine();

            }
            public static void Main()
            {
                Console.WriteLine("Type in your initial x-value: ");
                float x0 = Convert.ToSingle(Console.ReadLine());
                Console.WriteLine("Type in your initial y-value: ");
                float y0 = Convert.ToSingle(Console.ReadLine());
                Console.WriteLine("Type in your step h: ");
                float h = Convert.ToSingle(Console.ReadLine());

                Console.WriteLine("Type in the x-value you need an approximation at: ");
                float x = Convert.ToSingle(Console.ReadLine());

                Console.WriteLine("Input your first-order differential equation: ");
                string e = Console.ReadLine();

                Euler(x0, y0, h, x, e);
            }
        }
    }
I first define a static void method Euler with parameters x0, y, h, x, and e (expression). I ask that the user input their initial values along with the differential equation, step h size, and to what x-value they want he or she would like an approximation at. From that point I create a new string e0 in which the initial ODE expression e is copied, except that the "x" and "y" strings are replaced with variables. Then, the actual Euler method equation is solved, with e0 being computed using the System_Data namespace and the calculated y-values being re-plugged into the equation with the corresponding x-values until the x-value reaches the desired number. The solution is printed in the console, and the program is complete.

The next few posts will probably be about the new batch of supplies I ordered for the airplane (once they arrive) and when the new printer component arrives, at which point I can resume working on the plane. I might also post a few other programs, depending on the timing of everything else. Hopefully it won't take too long...

No comments:

Post a Comment