Geometric way of interpreting equations

Let's consider the following non-linear ordinary differential equation $$\frac{d}{dt}x = \sin x$$

The solution to this equation is given by:

$$t = \ln \frac{\csc x_0 + \cot x_0}{\csc x + \cot x}$$

Let us look at how the solution looks like. We make an implicit plot of the following form: $$\exp{t}\times (\csc x + \cot x) = (\csc x_0 + \cot x_0)$$

In [1]:
set(0, "defaultlinelinewidth", 2);
set (0, "defaulttextfontname", "TimesNewRoman")
set (0, "defaulttextfontsize", 20)
set (0, "DefaultAxesFontName", "TimesNewRoman")
set(0, 'DefaultAxesFontSize', 20)

close all
clear h

graphics_toolkit qt

h.ax = axes ("position", [0.05 0.4 0.5 0.5]);

function update_plot (obj, init = false)

  ## gcbo holds the handle of the control
  h = guidata (obj);
  replot = false;
  recalc = false;
  switch (gcbo)
    case {h.print_pushbutton}
      fn =  uiputfile ("*.png");
      print (fn);
    case {h.grid_checkbox}
      v = get (gcbo, "value");
      grid (merge (v, "on", "off"));
    case {h.minor_grid_toggle}
      v = get (gcbo, "value");
      grid ("minor", merge (v, "on", "off"));
    case {h.plot_title_edit}
      v = get (gcbo, "string");
      set (get (h.ax, "title"), "string", v);
    case {h.isovalue_slider}
      recalc = true;
  end

  if (recalc || init)
    x0 = 0.1+ 6.1*get (h.isovalue_slider, "value");
    x = linspace(1e-6, 2*pi, 100);
    t = linspace(1e-6, 10, 100);
    [X, T] = meshgrid(x, t);
    y =  exp(T).*(1./sin(X) + 1./tan(X));
    isoval = (1./sin(x0) + 1./tan(x0));
  
    h.plot = contour(T, X, y, [isoval isoval]);
    hold on;
    h.plot = contour(T, X, X, [pi/2 3*pi/2]);
    hold off;
    set (h.isovalue_label, "string", sprintf ("isovalue: %.1f", x0));

        
      guidata (obj, h);
    else
      #set (h.plot, "ydata", y);
   end
end


## plot title
h.plot_title_label = uicontrol ("style", "text",
                                "units", "normalized",
                                "string", "plot title: (text)",
                                "horizontalalignment", "left",
                                "position", [0.6 0.85 0.35 0.08]);

h.plot_title_edit = uicontrol ("style", "edit",
                               "units", "normalized",
                               "string", "Please fill me! (edit)",
                               "callback", @update_plot,
                               "position", [0.6 0.80 0.35 0.06]);

## grid
h.grid_checkbox = uicontrol ("style", "checkbox",
                             "units", "normalized",
                             "string", "show grid\n(checkbox)",
                             "value", 0,
                             "callback", @update_plot,
                             "position", [0.6 0.65 0.35 0.09]);

h.minor_grid_toggle = uicontrol ("style", "togglebutton",
                                 "units", "normalized",
                                 "string", "minor\n(togglebutton)",
                                 "callback", @update_plot,
                                 "value", 0,
                                 "position", [0.77 0.65 0.18 0.09]);

## print figure
h.print_pushbutton = uicontrol ("style", "pushbutton",
                                "units", "normalized",
                                "string", "print plot\n(pushbutton)",
                                "callback", @update_plot,
                                "position", [0.6 0.45 0.35 0.09]);
## isovalue
h.isovalue_label = uicontrol ("style", "text",
                           "units", "normalized",
                           "string", "isovalue:",
                           "horizontalalignment", "left",
                           "position", [0.05 0.25 0.35 0.08]);

h.isovalue_slider = uicontrol ("style", "slider",
                            "units", "normalized",
                            "string", "slider",
                            "callback", @update_plot,
                            "value", 0.2,
                            "position", [0.05 0.20 0.35 0.06]);
                   



set (gcf, "color", get(0, "defaultuicontrolbackgroundcolor"))
guidata (gcf, h)
update_plot (gcf, true);
error: graphics_toolkit: qt toolkit is not available
error: called from
    graphics_toolkit at line 81 column 5
Inline plot failed, consider trying another graphics toolkit
error: __gnuplot_draw_figure__: unknown object class, uicontrol
error: called from
    _make_figures>safe_print at line 125 column 7
    _make_figures at line 49 column 13

image.png

In [2]:
x = linspace(0, 2*pi);
dxdt = sin(x);
plot(x, dxdt);
hold on;
plot(x, 0*x);
hold off;
In [3]:
set(0, "defaultlinelinewidth", 5);
set (0, "defaulttextfontname", "TimesNewRoman")
set (0, "defaulttextfontsize", 20)
set (0, "DefaultAxesFontName", "TimesNewRoman")
set(0, 'DefaultAxesFontSize', 20)

function y = f(N, t, K, r)
   y = r*N*(1-N/K);
end

K = 3;
r = 0.3;
N0 = K/3;
t=linspace(0,20,100)';

Ndot = @(N,t) f(N, t, K, r);
sol = lsode(Ndot, N0, t);
plot(t, sol, "-k", t, K*ones(size(t)), '--b');
xlabel("t"); ylabel("N(t)");
In [4]:
clear;
clc; close all;

h.ax = axes ("position", [0.05 0.4 0.5 0.5]);  #reduce plot windows size to accomodate sliders
function y = f(N, t, K, r)
  y = r*N*(1-N/K);
end  


function update_plot (obj, init = false)
  ## gcbo holds the handle of the control
  h = guidata (obj);
  replot = false;
  recalc = false;
  switch (gcbo)                       # If we make any change then we replot
    case {h.print_pushbutton}
      fn =  uiputfile ("*.png");
      print (fn);
    case {h.slider1}
      recalc = true;
    case {h.slider2}
      recalc = true;
     case {h.slider3}
      recalc = true;
    case {h.slider4}
      recalc = true; 
    end
    
    if (recalc || init)
      k = 1+ 9*get (h.slider1, "value");
      r = .1 + .85*get (h.slider2, "value");
      N0 =  0.1 + 20*get (h.slider3, "value");
      t_end = 10 + 40*get(h.slider4, "value");
      set (h.label1, "string", sprintf ("k: %.1f", k));
      set (h.label2, "string", sprintf ("r: %.1f", r));
      set (h.label3, "string", sprintf ("N0: %.1f", N0));
      set (h.label4, "string", sprintf ("t_end: %.1f", t_end));       
      t=linspace(0,t_end,100)';
      Ndot = @(N,t) f(N, t, k, r);
      sol = lsode(Ndot, N0, t);
      plot(t, sol, "-k", t, k*ones(size(t)), '--b', t, k/2*ones(size(t)), '--r');
      xlabel("t"); ylabel("N(t)");               
      
      guidata (obj, h);
    else
      set (h.plot, "ydata", y);
    end
  end
  
  # specify the GUI plot properties like sliders and label formatting
  
  ## print figure
  h.print_pushbutton = uicontrol ("style", "pushbutton",
  "units", "normalized",
  "string", "print plot\n(pushbutton)",
  "callback", @update_plot,
  "position", [0.6 0.45 0.35 0.09]);
  ## guess
  h.label1 = uicontrol ("style", "text",
  "units", "normalized",
  "string", "Guess:",
  "horizontalalignment", "left",
  "position", [0.05 0.25 0.35 0.08]);
  
  h.slider1 = uicontrol ("style", "slider",
  "units", "normalized",
  "string", "slider",
  "callback", @update_plot,
  "value", 0.1,
  "position", [0.05 0.20 0.35 0.06]);
  
  h.label2 = uicontrol ("style", "text",
  "units", "normalized",
  "string", "Iteration:",
  "horizontalalignment", "left",
  "position", [0.05 0.15 0.35 0.08]);
  
  h.slider2 = uicontrol ("style", "slider",
  "units", "normalized",
  "string", "slider",
  "callback", @update_plot,
  "value", 0.5,
  "position", [0.05 0.10 0.35 0.06]);
  
    h.label3 = uicontrol ("style", "text",
  "units", "normalized",
  "string", "Guess:",
  "horizontalalignment", "left",
  "position", [0.6 0.25 0.35 0.08]);
  
  h.slider3 = uicontrol ("style", "slider",
  "units", "normalized",
  "string", "slider",
  "callback", @update_plot,
  "value", 0.1,
  "position", [0.6 0.20 0.35 0.06]);
  
  h.label4 = uicontrol ("style", "text",
  "units", "normalized",
  "string", "Iteration:",
  "horizontalalignment", "left",
  "position", [0.6 0.15 0.35 0.08]);
  
  h.slider4 = uicontrol ("style", "slider",
  "units", "normalized",
  "string", "slider",
  "callback", @update_plot,
  "value", 0.5,
  "position", [0.6 0.10 0.35 0.06]);
  
  set (gcf, "color", get(0, "defaultuicontrolbackgroundcolor"))
  guidata (gcf, h)
  update_plot (gcf, true);
Inline plot failed, consider trying another graphics toolkit
error: __gnuplot_draw_figure__: unknown object class, uicontrol
error: called from
    _make_figures>safe_print at line 125 column 7
    _make_figures at line 49 column 13

image.png