[genius] Thu Oct 12 17:22:01 2017 Jiri (George) Lebl <jirka 5z com>



commit d7721103bac4b0ee45b13bb33607de575e1ebfde
Author: Jiri (George) Lebl <jiri lebl gmail com>
Date:   Thu Oct 12 17:22:28 2017 -0500

    Thu Oct 12 17:22:01 2017  Jiri (George) Lebl <jirka 5z com>
    
        * examples/strange-attractor.gel: make the variable names match the
          comment
    
        * examples/plot-ode-runge-kutta.gel: An example for plotting
          solutions to ODE using RungeKutta
    
        * examples/plot-ode-trajectory-runge-kutta.gel: An example for plotting
          trajectories to first order ODE systems.  Plots an trajectory
          starting at the mouse pointer

 ChangeLog                                    |   12 +++++
 examples/Makefile.am                         |    4 +-
 examples/duffing-equation-trajectories.gel   |    2 +-
 examples/plot-ode-runge-kutta.gel            |   61 ++++++++++++++++++++++++++
 examples/plot-ode-trajectory-runge-kutta.gel |   61 ++++++++++++++++++++++++++
 5 files changed, 138 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index bf6ed2c..b2eafba 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Thu Oct 12 17:22:01 2017  Jiri (George) Lebl <jirka 5z com>
+
+       * examples/strange-attractor.gel: make the variable names match the
+         comment
+       
+       * examples/plot-ode-runge-kutta.gel: An example for plotting
+         solutions to ODE using RungeKutta
+
+       * examples/plot-ode-trajectory-runge-kutta.gel: An example for plotting
+         trajectories to first order ODE systems.  Plots an trajectory
+         starting at the mouse pointer
+
 Wed May 10 17:37:33 2017  Jiri (George) Lebl <jirka 5z com>
 
        * Release 1.0.23
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 1b23026..0c09286 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -33,7 +33,9 @@ example_DATA = \
        complex-analysis-mesh.gel \
        complex-analysis-argument-principle.gel \
        riemann-integral.gel \
-       riemann-integral-darboux.gel
+       riemann-integral-darboux.gel \
+       plot-ode-runge-kutta.gel \
+       plot-ode-trajectory-runge-kutta.gel
 
 EXTRA_DIST = \
        $(example_DATA)
diff --git a/examples/duffing-equation-trajectories.gel b/examples/duffing-equation-trajectories.gel
index a28541f..388e26b 100644
--- a/examples/duffing-equation-trajectories.gel
+++ b/examples/duffing-equation-trajectories.gel
@@ -7,7 +7,7 @@
 
 # The Duffing equation (converted to an ODE system)
 # x_1' = x_2, x_2' = -0.05x_2 - x_1^3 + 8cos(t)
-function duf(x,y) = [y@(2),-0.05*y@(2)-(y@(1))^3+8*cos(x)];
+function duf(t,x) = [x@(2),-0.05*x@(2)-(x@(1))^3+8*cos(t)];
 
 LinePlotDrawLegends = false;
 PlotWindowPresent(); # Make sure the window is raised
diff --git a/examples/plot-ode-runge-kutta.gel b/examples/plot-ode-runge-kutta.gel
new file mode 100644
index 0000000..43fde5d
--- /dev/null
+++ b/examples/plot-ode-runge-kutta.gel
@@ -0,0 +1,61 @@
+# Category: Differential Equations
+# Name: Numerically plotting solutions of ODE
+#
+# Draw solutions of ODE.  We plot first order systems, and give two examples
+# that are converted second order equations: The Duffing equation of chaos
+# fame, and the Van der Pol oscillator.  Then we also include the Predator-Prey
+# system.  To plot these, uncomment the correct lines below, or write your own
+# function sys giving the system, t_range is the range, init_cond is the
+# initial condition, and number_of_points is the number of points to compute
+# and plot.
+
+# The Duffing equation
+# x'' = -0.5x' - x^3 + 8cos(t)
+# Converted to first order ODE system 
+# x_1' = x_2, x_2' = -0.05x_2 - x_1^3 + 8cos(t)
+function sys(t,x) = [x@(2), -0.05*x@(2)-(x@(1))^3+8*cos(t)];
+t_range = [0,20];
+number_of_points = 1000;
+init_cond = [2,3];
+
+# The Van der Pol oscillator
+# x'' = (1-x^2)x' - x
+# Converted to first order ODE system 
+# x_1' = x_2, x_2' = (1-x_1^2)x_2 - x_1
+#function sys(t,x) = [x@(2), (1-x@(1)^2)*x@(2)-x@(1)];
+#t_range = [0,30];
+#number_of_points = 500;
+#init_cond = [0.2,0];
+
+# The Predator-Prey (Lotka-Volterra)
+# x_1' = (0.4 - 0.01 x_2)x_1,  x_2' = (0.003 x_1 - 0.3) x_2
+#function sys(t,x) = [(0.4-0.01*x@(2))*x@(1), (0.003*x@(1)-0.3)*x@(2)]
+#t_range = [0,50];
+#number_of_points = 500;
+#init_cond = [20,50];
+
+LinePlotDrawLegends = true;
+PlotWindowPresent(); # Make sure the window is raised
+LinePlotClear ();
+
+# Compute the points
+pt = RungeKuttaFull(sys,t_range@(1),init_cond,t_range@(2),number_of_points);
+
+# Flatten the matrix
+pt = ExpandMatrix(pt);
+
+# Get the mins and the maxes
+min_val = min(pt@(,[2,3]));
+max_val = max(pt@(,[2,3]));
+
+#Add 5% padding to top and bottom
+LinePlotWindow = [t_range, min_val-(max_val-min_val)*0.05, max_val+(max_val-min_val)*0.05];
+
+# Draw the x_1
+LinePlotDrawLine(pt@(,[1,2]),"color","blue","legend","x_1(t)");
+
+# Draw the x_2
+LinePlotDrawLine(pt@(,[1,3]),"color","red","legend","x_2(t)");
+
+
+
diff --git a/examples/plot-ode-trajectory-runge-kutta.gel b/examples/plot-ode-trajectory-runge-kutta.gel
new file mode 100644
index 0000000..5a19c7f
--- /dev/null
+++ b/examples/plot-ode-trajectory-runge-kutta.gel
@@ -0,0 +1,61 @@
+# Category: Differential Equations
+# Name: Numerically plotting trajectories of ODE systems
+#
+# Draw trajectories of ODE starting at the mouse pointer.  We plot first order
+# systems, and give two examples that are converted second order equations: The
+# Duffing equation of chaos fame, and the Van der Pol oscillator.  Then we also
+# include the Predator-Prey system.  For autonomous systems this can be done
+# more easily when plotting vector fields.  To plot these, uncomment the
+# correct lines below, or write your own function sys giving the system,
+# t_range is the range, and number_of_points is the number of points to compute
+# and plot.
+
+# The Duffing equation
+# x'' = -0.5x' - x^3 + 8cos(t)
+# Converted to first order ODE system 
+# x_1' = x_2, x_2' = -0.05x_2 - x_1^3 + 8cos(t)
+function sys(t,x) = [x@(2), -0.05*x@(2)-(x@(1))^3+8*cos(t)];
+t_range = [0,30];
+LinePlotWindow = [-6,6,-6,6];
+number_of_points = 1000;
+init_cond = [2,3];
+
+# The Van der Pol oscillator
+# x'' = (1-x^2)x' - x
+# Converted to first order ODE system 
+# x_1' = x_2, x_2' = (1-x_1^2)x_2 - x_1
+#function sys(t,x) = [x@(2), (1-x@(1)^2)*x@(2)-x@(1)];
+#t_range = [0,30];
+#LinePlotWindow = [-4,4,-4,4];
+#number_of_points = 500;
+
+# The Predator-Prey (Lotka-Volterra)
+# x_1' = (0.4 - 0.01 x_2)x_1,  x_2' = (0.003 x_1 - 0.3) x_2
+#function sys(t,x) = [(0.4-0.01*x@(2))*x@(1), (0.003*x@(1)-0.3)*x@(2)]
+#t_range = [0,50];
+#LinePlotWindow = [0,250,0,90];
+#number_of_points = 500;
+
+LinePlotDrawLegends = false;
+PlotWindowPresent(); # Make sure the window is raised
+LinePlotClear ();
+
+while true do (
+       p = LinePlotMouseLocation ();
+       if IsNull(p) then break;
+
+       # Compute the points
+       pt = RungeKuttaFull(sys,t_range@(1),p,t_range@(2),number_of_points);
+
+       # Flatten the matrix
+       pt = ExpandMatrix(pt);
+
+       # Draw the trajectory
+       PlotCanvasFreeze();
+       LinePlotClear();
+       LinePlotDrawLine(pt@(,[2,3]),"color","blue");
+       PlotCanvasThaw();
+       
+       wait(0.03)
+);
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]