[genius] Fri Sep 05 15:25:28 2014 Jiri (George) Lebl <jirka 5z com>



commit 8e77bf584c7bf78e8be8291e60b4ad8244149a1c
Author: Jiri (George) Lebl <jiri lebl gmail com>
Date:   Fri Sep 5 15:25:33 2014 -0500

    Fri Sep 05 15:25:28 2014  Jiri (George) Lebl <jirka 5z com>
    
        * tutors/*: Add whole bunch of tutorials from what I've been using
          in classes (those that weren't a mess)

 ChangeLog                           |    5 ++
 tutors/Makefile.am                  |   15 ++++++-
 tutors/dalemb-pulse.gel             |   36 +++++++++++++++++
 tutors/eulers-method-exp-run.gel    |   20 +++++++++
 tutors/eulers-method-graphs-exp.gel |   74 +++++++++++++++++++++++++++++++++++
 tutors/linapprox.gel                |   35 ++++++++++++++++
 tutors/newton-calc-sqrt2.gel        |   38 ++++++++++++++++++
 tutors/newton-sqrt2.gel             |   39 ++++++++++++++++++
 tutors/shocks.gel                   |   54 +++++++++++++++++++++++++
 tutors/sierpinski.gel               |   29 ++++++++++++++
 tutors/standing-waves.gel           |    2 +-
 tutors/strange-attractor.gel        |    2 +-
 tutors/taylor-exp.gel               |   10 +++++
 tutors/taylor-sin.gel               |   16 +++++++
 14 files changed, 371 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5cc66b4..6e67334 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Sep 05 15:25:28 2014  Jiri (George) Lebl <jirka 5z com>
+
+       * tutors/*: Add whole bunch of tutorials from what I've been using
+         in classes (those that weren't a mess)
+
 Fri Sep 05 13:01:51 2014  Jiri (George) Lebl <jirka 5z com>
 
        * src/tutors.c: sort the tutors by category and then name.
diff --git a/tutors/Makefile.am b/tutors/Makefile.am
index a63c321..af95a06 100644
--- a/tutors/Makefile.am
+++ b/tutors/Makefile.am
@@ -1,10 +1,21 @@
 tutordir = \
        $(datadir)/genius/tutors/
 tutor_DATA = \
-       strange-attractor.gel \
        cantor.gel \
+       dalemb-pulse.gel \
+       eulers-method-exp-run.gel \
+       eulers-method-graphs-exp.gel \
        laplace-fdm.gel \
+       linapprox.gel \
        lorenz.gel \
-       standing-waves.gel
+       newton-calc-sqrt2.gel \
+       newton-sqrt2.gel \
+       shocks.gel \
+       sierpinski.gel \
+       standing-waves.gel \
+       strange-attractor.gel \
+       taylor-exp.gel \
+       taylor-sin.gel
+
 EXTRA_DIST = \
        $(tutor_DATA)
diff --git a/tutors/dalemb-pulse.gel b/tutors/dalemb-pulse.gel
new file mode 100644
index 0000000..47b36a8
--- /dev/null
+++ b/tutors/dalemb-pulse.gel
@@ -0,0 +1,36 @@
+# Category: Differential Equations
+# Name: D'Alembers solution animation of a plucked string
+
+#
+# Solution to the wave equation
+# The initial conditions are: position at F(x) and velocity at 0
+#
+
+# A pulse function.  Feel free to set this function to whatever you want,
+# though you should have ends at 0
+function F(x) = (
+    # This makes the functions periodic
+       while x < -1 do x = x + 2;
+       while x > 1 do x = x - 2;
+       
+       if x < 0 then
+               -F(-x)
+       else if x < 0.45 or x > 0.55 then
+               0
+       else (
+               if x < 1/2 then
+                       20*(x-0.45)
+               else                    
+                       20*(0.55-x)
+       )       
+);
+
+#t a paramater
+function yy(x) = (F(x-t) + F(x+t))/2;
+
+LinePlotDrawLegends = false;
+
+for n=1 to 1000 do (
+       t = n*0.005;
+       LinePlot(yy,[0,1,-1.1,1.1])
+)
diff --git a/tutors/eulers-method-exp-run.gel b/tutors/eulers-method-exp-run.gel
new file mode 100644
index 0000000..2d334a7
--- /dev/null
+++ b/tutors/eulers-method-exp-run.gel
@@ -0,0 +1,20 @@
+# Category: Differential Equations
+# Name: Estimation of exp(4) using Euler's Method 
+
+# we will measure time it takes
+ct=CurrentTime;
+sp=(CurrentTime-ct);
+
+print("Estimation of e^4 with Euler's method");
+print("h is the step size we use");
+
+# Rounding to specific number of digits
+function ourround(num) = round(num*10000.0)/10000.0;
+function ourroundb(num) = round(num*100000000.0)/100000000.0;
+
+for k=1 to 21 do (
+  ct = CurrentTime;
+  eu = EulersMethod(`(x,y)=y,0,1,4,2^k);
+  ct2 = (CurrentTime-ct-sp);
+  print("h=2^"+(3-k)+" gets "+ourroundb(eu)+" (e^4=" + ourroundb(e^4) + ", error="+ourroundb(|eu-e^4|)+") .. 
in " + ourround(ct2) + " secs");
+);
diff --git a/tutors/eulers-method-graphs-exp.gel b/tutors/eulers-method-graphs-exp.gel
new file mode 100644
index 0000000..03b846e
--- /dev/null
+++ b/tutors/eulers-method-graphs-exp.gel
@@ -0,0 +1,74 @@
+# Category: Differential Equations
+# Name: Euler's Method graphs for the exponential
+
+# You can play around with different 
+# differential equations, the equation is dy/dx = g(x,y)
+function g(x,y)=y;
+LinePlotWindow=[-0.2,4,0,e^4+0.2];
+
+LinePlotDrawLegends=false;
+LinePlotClear();
+LinePlotDrawPoints(0,1,"thickness",10,"color","red");
+
+AskButtons("The initial point (0,1)","Next");
+
+# h is step size, x0, y0 are the initial conditions
+h=1;
+x0=0;
+y0=1;
+
+xx=[x0];yy=[y0];
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = yy@(j-1)+g(xx@(j-1),yy@(j-1))*h;
+);
+LinePlotDrawLine ([xx',yy'],"color","darkgreen");
+AskButtons("Step size h = 1","Next");
+
+h=(1/2);
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = yy@(j-1)+g(xx@(j-1),yy@(j-1))*h
+);
+LinePlotDrawLine ([xx',yy'],"color","blue");
+AskButtons("h = 0.5 = 1/2","Next");
+
+h=(1/4);
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = yy@(j-1)+g(xx@(j-1),yy@(j-1))*h
+);
+LinePlotDrawLine ([xx',yy'],"color","orange");
+AskButtons("h = 0.25 = 1/4","Next");
+
+h=(1/8);
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = yy@(j-1)+g(xx@(j-1),yy@(j-1))*h
+);
+LinePlotDrawLine ([xx',yy'],"color","brown");
+AskButtons("h = 0.125 = 1/8","Next");
+
+h=(1/16);
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = yy@(j-1)+g(xx@(j-1),yy@(j-1))*h
+);
+LinePlotDrawLine ([xx',yy'],"color","darkgreen");
+AskButtons("h = 0.0625 = 1/16","Next");
+
+h=(1/32);
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = yy@(j-1)+g(xx@(j-1),yy@(j-1))*h
+);
+LinePlotDrawLine ([xx',yy'],"color","blue");
+AskButtons("h=0.03125=1/32","Next");
+
+# The real solution
+for j=2 to (4/h)+1 do (
+  xx@(j) = xx@(j-1)+h;
+  yy@(j) = e^(xx@(j))
+);
+LinePlotDrawLine ([xx',yy'],"color","red");
+AskButtons("The real solution","End");
diff --git a/tutors/linapprox.gel b/tutors/linapprox.gel
new file mode 100644
index 0000000..6ab73a8
--- /dev/null
+++ b/tutors/linapprox.gel
@@ -0,0 +1,35 @@
+# Category: Calculus
+# Name: Linear approximation by a tangent graphically
+
+function f(x) = sin(ln(x+1))^2;
+df = SymbolicDerivative(f);
+
+# Where to approximate
+a = 2;
+
+# The approximation
+function lf(x) = df(a)*(x-a)+f(a);
+
+function DoGraph(x) = (
+  PlotCanvasFreeze();
+  LinePlot(f,lf,[-0.5,5,-0.1,2]);
+  LinePlotDrawLine([x,0,x,min(f(x),lf(x))],"color","orange");
+  LinePlotDrawLine([x,min(f(x),lf(x)),x,max(f(x),lf(x))],"color","red");
+  LinePlotDrawLine([-0.5,f(x),5,f(x)],"color","black","thickness",1);
+  LinePlotDrawLine([-0.5,lf(x),5,lf(x)],"color","purple","thickness",1);
+  PlotCanvasThaw();
+  print("at x=" + x + "  error = |" + lf(x) + " - " + f(x) + "| = " + |lf(x)-f(x)|);
+);
+
+for x=2 to 5 by 0.0222 do (
+  DoGraph(x);
+  wait(0.003)
+);
+for x=5 to -0.5 by -0.0222 do (
+  DoGraph(x);
+  wait(0.06)
+);
+for x=-0.5 to 2 by 0.0222 do (
+  DoGraph(x);
+  wait(0.003)
+);
diff --git a/tutors/newton-calc-sqrt2.gel b/tutors/newton-calc-sqrt2.gel
new file mode 100644
index 0000000..6bdf20d
--- /dev/null
+++ b/tutors/newton-calc-sqrt2.gel
@@ -0,0 +1,38 @@
+# Category: Calculus
+# Name: Newton's method for sqrt(2)
+#
+# Calculate some number of iterations of Newton's method for sqrt(2)
+
+# Try different functions here to try to get roots of.  Note that
+# if genius can't take derivative you can set the derivative function
+# df below.
+function f(x)=x^2-2;
+
+#initial guess
+c1 = 2;
+
+#iterations to run
+iters = 9;
+
+# how long to pause after each iteration
+thepause = 1;
+
+df = SymbolicDerivative(f);
+
+old_MaxDigits = MaxDigits;
+old_FloatPrecision = FloatPrecision;
+
+MaxDigits = 0;
+FloatPrecision = 192;
+
+c = c1;
+for n = 1 to iters do (
+  print("c("+n+") = ");
+  print(float(c));
+  nc = c-float(f(c)/df(c));
+  wait(thepause);
+  c = nc;
+);
+
+MaxDigits = old_MaxDigits;
+FloatPrecision = old_FloatPrecision;
diff --git a/tutors/newton-sqrt2.gel b/tutors/newton-sqrt2.gel
new file mode 100644
index 0000000..272ce35
--- /dev/null
+++ b/tutors/newton-sqrt2.gel
@@ -0,0 +1,39 @@
+# Category: Calculus
+# Name: Newton's method graphically computing sqrt(2)
+
+function f(x)=x^2-2;
+c1 = 2;
+LinePlotWindow=[1,2.5,-3,4];
+
+df = SymbolicDerivative(f);
+
+LinePlotDrawLegends=false;
+LinePlotClear();
+
+LinePlot(f);
+LinePlotDrawLine(c1,-100,c1,100,"color","red","thickness",1);
+
+AskButtons("We're starting with an estimate at x=2","OK");
+
+LinePlot(f,`(x)=df(c1)*(x-c1)+f(c1));
+c2=c1-f(c1)/df(c1);
+LinePlotDrawLine(c1,-100,c1,100,"color","black","thickness",1);
+LinePlotDrawLine(c2,-100,c2,100,"color","red","thickness",1);
+AskButtons(float(c2)+" (real sqrt(2) is " + sqrt(2) + ")","OK");
+
+LinePlot(f,
+         `(x)=df(c2)*(x-c2)+f(c2));
+c3=c2-f(c2)/df(c2);
+LinePlotDrawLine(c1,-100,c1,100,"color","black","thickness",1);
+LinePlotDrawLine(c2,-100,c2,100,"color","black","thickness",1);
+LinePlotDrawLine(c3,-100,c3,100,"color","red","thickness",1);
+AskButtons(float(c3)+" (real sqrt(2) is " + sqrt(2) + ")","OK");
+
+LinePlot(f,
+         `(x)=df(c3)*(x-c3)+f(c3));
+c4=c3-f(c3)/df(c3);
+LinePlotDrawLine(c1,-100,c1,100,"color","black","thickness",1);
+LinePlotDrawLine(c2,-100,c2,100,"color","black","thickness",1);
+LinePlotDrawLine(c3,-100,c3,100,"color","black","thickness",1);
+LinePlotDrawLine(c4,-100,c4,100,"color","red","thickness",1);
+AskButtons(float(c4)+" (real sqrt(2) is " + sqrt(2) + ")","OK");
diff --git a/tutors/shocks.gel b/tutors/shocks.gel
new file mode 100644
index 0000000..0d164b5
--- /dev/null
+++ b/tutors/shocks.gel
@@ -0,0 +1,54 @@
+# Category: Differential Equations
+# Name: Characteristics and shocks for the traffic flow equation
+#
+# We are looking at characteristics (looking for shocks)
+# for the equation u_t + g(u) u_x = 0, with initial condition
+# u(x,0) = phi(x)
+
+# this is for flux u*(2-u)
+#function g(u) = 2-2*u;
+
+# Quadratic flow for flux u^2 / 2
+function g(u) = u;
+
+# ramp down
+function phi(x) = (
+  if x < 0 then 1
+  else if x < 1 then 1-x
+  else 0
+);
+
+# rampup
+#function phi(x) = (
+#  if x < 0 then 0
+#  else if x < 1 then x
+#  else 1
+#);
+
+
+# we'll have x going from
+xstart = -3;
+xend = 3;
+xstep = 0.1;
+# end at this t, start at 0
+tend = 1.5;
+
+LinePlotWindow = [xstart,xend,-0.1,tend+0.1];
+LinePlotDrawLegends = false;
+LinePlotClear();
+
+# color in [red,green,blue]
+# Color according to phi
+function the_color(x0) = [phi(x0), #red
+                          0.2, # green
+                          1.0-phi(x0)]; #blue
+
+# Color according to x0
+#function the_color(x0) = [(x0-xstart)/(xend-xstart), #red
+#                          0.2, # green
+#                          1.0-(x0-xstart)/(xend-xstart)]; #blue
+
+# Draw characteristics
+for x0=xstart to xend by xstep do (
+  LinePlotDrawLine([x0,0,x0+g(phi(x0))*tend,tend], "color", the_color (x0))
+)
diff --git a/tutors/sierpinski.gel b/tutors/sierpinski.gel
new file mode 100644
index 0000000..9ec0053
--- /dev/null
+++ b/tutors/sierpinski.gel
@@ -0,0 +1,29 @@
+# Category: Chaos
+# Name: Draw Sierpinski's triangle using the chaos game
+
+function dist(p1,p2) = sqrt((p1@(1)-p2@(1))^2+(p1@(2)-p2@(2))^2);
+
+p = [0,0;1,0;0.5,sqrt(3)/2];
+LinePlotClear();
+
+x=[0.5,0.5];
+LinePlotWindow = [-0.1,1.1,-0.1,sqrt(3)/2+0.1];
+points = null;
+for n=1 to 10000 do (
+  j=randint(3)+1;
+  x = 0.5*x + 0.5*p@(j,);
+  points = [points;x];
+  # Draw every 100 iterations 
+  if n%100 == 0 then (
+    # The Freeze and Thaw make the animation smoother, avoids flicker
+    PlotCanvasFreeze();
+    LinePlotClear();
+    LinePlotDrawPoints(points,"color","blue");
+    PlotCanvasThaw();
+  )
+);
+
+PlotCanvasFreeze();
+LinePlotClear();
+LinePlotDrawPoints(points,"color","blue");
+PlotCanvasThaw();
diff --git a/tutors/standing-waves.gel b/tutors/standing-waves.gel
index 1b4b471..348a94e 100644
--- a/tutors/standing-waves.gel
+++ b/tutors/standing-waves.gel
@@ -1,5 +1,5 @@
 # Category: Differential Equations
-# Name: Standing Waves
+# Name: Standing waves
 
 the_answer = AskButtons("Number of dimensions?", "2D", "3D");
 
diff --git a/tutors/strange-attractor.gel b/tutors/strange-attractor.gel
index 536b9c0..921f55b 100644
--- a/tutors/strange-attractor.gel
+++ b/tutors/strange-attractor.gel
@@ -1,5 +1,5 @@
 # Category: Chaos
-# Name: Strange Attractor (Duffing's equation)
+# Name: Strange attractor (Duffing's equation)
 #
 # Creates a strange attractor by strobing the forced Duffing
 # equation
diff --git a/tutors/taylor-exp.gel b/tutors/taylor-exp.gel
new file mode 100644
index 0000000..43d343d
--- /dev/null
+++ b/tutors/taylor-exp.gel
@@ -0,0 +1,10 @@
+# Category: Calculus
+# Name: Taylor approximations of the exponential
+
+LinePlotWindow = [-3,3,-1,10];
+LinePlotDrawLegends=false;
+for M=0 to 8 do (
+  print("Taylor polynomial of degree = " + M);
+  LinePlot(`(x)=e^x,`(x)=(sum n=0 to M do (1/(n!))*x^n));
+  wait(1)
+); 
diff --git a/tutors/taylor-sin.gel b/tutors/taylor-sin.gel
new file mode 100644
index 0000000..7a22852
--- /dev/null
+++ b/tutors/taylor-sin.gel
@@ -0,0 +1,16 @@
+# Category: Calculus
+# Name: Taylor approximations of sine
+
+LinePlotWindow = [-5,5,-2,2];
+LinePlotDrawLegends=false;
+function c(n) = (
+  if IsEven(n) then
+    0
+  else
+    ((-1)^((n-1)/2))/(n!)
+);
+for M=0 to 12 do (
+  print("Taylor polynomial of degree = " + M);
+  LinePlot(`(x)=sin(x),`(x)=(sum n=0 to M do c(n)*(x^n)));
+  wait(1)
+); 


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