-1-
Hallo,
mit den nachfolgenden Klassen ist es möglich, Differentialgleichungen zu lösen.
Dabei sind die Methoden ( RK4, RK5, Euler) beliebig austauschbar:
Die IApproximation-Klasse
Die einzelnen Verfahren
Euler
RK4
und RK5
Die Vektor2 Klasse:
Beispiel:
Integriert wird die Funktion
y'=y
mit der Bedingung y(0)=1
Ergebnis:
Und das ist offensichtlich richtig:
Denn
f(x)= e^x
f'(x)= e^x
also f'(x)=f(x) oder y'=y
Dabei ist while(approx.Time <= n) dasselbe wie f(n)=e^n
und y'=n*y = f(x)=e^(n*x)
Viel Spaß.
EDIT: _ Gelernt mit: web.archive.org/web/2017042106….us/post/runge-kutta-in-c
_
Hallo,
mit den nachfolgenden Klassen ist es möglich, Differentialgleichungen zu lösen.
Dabei sind die Methoden ( RK4, RK5, Euler) beliebig austauschbar:
Die IApproximation-Klasse
Die einzelnen Verfahren
Euler
RK4
C#-Quellcode
- public class RK4 : IApproximation
- {
- public RK4(Vector2 _0, double delta)
- {
- Output = _0;
- Delta = delta;
- }
- public override Vector2 Run(Vector2 y)
- {
- Vector2 k1 = Delta * f(Time, y);
- Vector2 k2 = Delta * f(Time + .5 * Delta, y + .5 * k1);
- Vector2 k3 = Delta * f(Time + .5 * Delta, y + .5 * k2);
- Vector2 k4 = Delta * f(Time + Delta, y + k3);
- Time += Delta;
- Output += 1.0 / 6.0 * (k1 + 2 * k2 + 2 * k3 + k4);
- return Output;
- }
- }
und RK5
C#-Quellcode
- public class RK5 : IApproximation
- {
- public RK5(Vector2 _0, double delta)
- {
- Output = _0;
- Delta = delta;
- }
- public override Vector2 Run(Vector2 y)
- {
- Vector2 k1 = Delta * f(Time, y);
- Vector2 k2 = Delta * f(Time + ((3.0 * Delta) / 5.0), y + ((1 * 3.0) / 5.0) * k1);
- Vector2 k3 = Delta * f(Time + ((2.0 * Delta) / 5.0), y + ((4.0 * 1) / 15.0) * k1 + ((2.0 * 1) / 15.0) * k2);
- Vector2 k4 = Delta * f(Time + (Delta / 5.0), y + ((3.0 * 1) / 20.0) * k1 + (1 / 20.0) * k3);
- Vector2 k5 = Delta * f(Time + ((4.0 * Delta) / 5.0), y + ((-1 / 5.0)) * k1 + ((-2.0 * 1) / 5.0) * k2 + ((7.0 * 1) / 5.0) * k3);
- Vector2 k6 = Delta * f(Time + Delta, y + ((59.0 * 1) / 84.0) * k1 + ((40.0 * 1) / 21.0) * k2 + ((-165.0 * 1) / 28.0) * k3 + ((20.0 * 1) / 7.0) * k4 + ((10.0 * 1) / 7.0) * k5);
- Output += (k1 / 12.0) + ((25.0 * k3) / 72.0) + ((25.0 * k4) / 144.0) + ((25.0 * k5) / 72.0) + ((7.0 * k6) / 144.0);
- Time += Delta;
- return Output;
- }
- }
Die Vektor2 Klasse:
C#-Quellcode
- public struct Vector2
- {
- public double X { get; set; }
- public double Y { get; set; }
- public double Length()
- {
- return Math.Sqrt(X * X + Y * Y);
- }
- public Vector2(double x, double y)
- {
- X = x;
- Y = y;
- }
- public static Vector2 operator *(int a, Vector2 b)
- {
- return new Vector2(b.X * a, b.Y * a);
- }
- public static Vector2 operator *(double a, Vector2 b)
- {
- return new Vector2(b.X * a, b.Y * a);
- }
- public static Vector2 operator *(Vector2 b, double a)
- {
- return new Vector2(b.X * a, b.Y * a);
- }
- public static Vector2 operator /(Vector2 b, double a)
- {
- return new Vector2(b.X / a, b.Y / a);
- }
- public static Vector2 operator +(Vector2 a, Vector2 b)
- {
- return new Vector2(a.X + b.X, a.Y + b.Y);
- }
- public static Vector2 operator -(Vector2 a, Vector2 b)
- {
- return new Vector2(b.X - a.X, b.Y - a.Y);
- }
- public static Vector2 operator -( Vector2 b)
- {
- return new Vector2(-b.X, -b.Y);
- }
- public override string ToString()
- {
- return "X: " + X + ";" + "Y: " + Y;
- }
- public PointF ToPointF()
- {
- return new PointF((float)X, (float)Y);
- }
- }
Beispiel:
Integriert wird die Funktion
y'=y
mit der Bedingung y(0)=1
Ergebnis:
Und das ist offensichtlich richtig:
Denn
f(x)= e^x
f'(x)= e^x
also f'(x)=f(x) oder y'=y
Dabei ist while(approx.Time <= n) dasselbe wie f(n)=e^n
und y'=n*y = f(x)=e^(n*x)
Viel Spaß.
EDIT: _ Gelernt mit: web.archive.org/web/2017042106….us/post/runge-kutta-in-c
_
Und Gott alleine weiß alles am allerbesten und besser.
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „φConst“ ()