Introduction to Computational Methods -...
Transcript of Introduction to Computational Methods -...
![Page 1: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/1.jpg)
Introduction to Computational Methods
Maria LeiteBenito Chen-Charpentier
Folashade Agusto
![Page 2: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/2.jpg)
Numerical Methods
Some references for numerical methods are:
Introduction to Numerical Methods
Lecture notes for MATH 3311
Jeffrey R. Chasnov
https://www.math.ust.hk/~machas/numerical-methods.pdf
![Page 3: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/3.jpg)
Introduction to Numerical Methods and Matlab Programming for Engineers
Todd Young and Martin J. Mohlenkamp
http://www.math.ohiou.edu/courses/math3600/book.pdf
![Page 4: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/4.jpg)
NUMERICAL METHODS IN ENGINEERING WITH
MATLAB
Jaan Kiusalaas
http://shoni2.princeton.edu/ftp/lyo/journals/Kiusalaas-NumMethodsEngineerMATLAB-CambUnivPress2005.pdf
![Page 5: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/5.jpg)
Numerical Methods and Modeling for Chemical Engineers
Mark E. Davis
https://authors.library.caltech.edu/25061/1/NumMethChE84.pdf
Concentrates on ODE’s and PDE’s.
![Page 6: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/6.jpg)
Programming Languages
Fortran 90
C
C++
Fast (compiled) but require more programmer effort and knowledge
![Page 7: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/7.jpg)
High Level Languages
Matlab
Gnu Octave
Scilab
R
Python
Slower (interpreted) but easier to use.
Good for small to medium size numerical simulations.
![Page 8: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/8.jpg)
Computer Algebra Systems
Maple
Mathematica
Maxima
Sage
Can do symbolic computations
![Page 9: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/9.jpg)
Specialized software
xpp (or xppaut)
http://www.math.pitt.edu/~bard/xpp/xpp.html
Numerical solutions of differential equations plus bifurcation diagrams using AUTO. Easier to use than AUTO.
The web page includes a tutorial and examples.
![Page 10: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/10.jpg)
AUTO-07p
AUTO is a software for continuation and bifurcation problems in ordinary differential equations.
https://sourceforge.net/projects/auto-07p/files/auto07p/
There is manual and examples at the home page:
http://indy.cs.concordia.ca/auto/
Harder to use than xppaut.
A good tutorial are the lecture notes:
NUMERICAL ANALYSIS of NONLINEAR EQUATIONS
Eusebius Doedel
http://indy.cs.concordia.ca/auto/notes.pdf
![Page 11: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/11.jpg)
Software for optimal control
bocop is an open-source toolbox for solving optimal control problems
http://www.bocop.org/
Very powerful with great minimization software but the problem description has to be written in C++. But there are examples that can be modified.
![Page 12: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/12.jpg)
Matlab
https://www.mathworks.com/
Widely available and used for numerical simulations.
Many methods and toolboxes.
Great support.
Relatively expensive.
Symbolic part lacks.
![Page 13: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/13.jpg)
Scilab
http://www.scilab.org/
Free.
Many libraries
Good support
Interfaces with other languages.
![Page 14: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/14.jpg)
Tutorial:
http://scilab.io/getting-started-content/
Good documentation
Not matlab compatible but the program will convert matlab to scilab and there is also a conversion table:
https://help.scilab.org/docs/5.5.2/en_US/section_36184e52ee88ad558380be4e92d3de21.html
Several books in English, French and other languages.
![Page 15: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/15.jpg)
Recommended software
Octave for numerical simulations and plotting
Maxima for symbolic calculations and plotting
Xppaut for bifurcation analysis
![Page 16: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/16.jpg)
Gnu Octave
https://www.gnu.org/software/octave/
Free
Very compatible with matlab.
Very good online documentation:
https://octave.org/doc/interpreter/
Tutorials:
http://www-mdp.eng.cam.ac.uk/web/CD/engapps/octave/octavetut.pdf
http://ais.informatik.uni-freiburg.de/teaching/ws11/robotics2/pdfs/rob2-03-octave.pdf
![Page 17: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/17.jpg)
Symbolic package weak.
Many packages.
![Page 18: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/18.jpg)
Programming for Computations
- A Gentle Introduction to Numerical Simulations with
MATLAB/Octave
Svein Linge and Hans Petter Langtangen
https://hplgit.github.io/prog4comp/doc/pub/p4c_Matlab.pdf
![Page 19: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/19.jpg)
Numerical Methods Library for OCTAVE, Users Guide
Lilian Calvet
http://www.ipb.pt/~balsa/teaching/MA08_09/UsersGuide.pdf
A library of common numerical methods extending the ones in Octave
![Page 20: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/20.jpg)
Octave GUI
Has a GUI with an editor
![Page 21: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/21.jpg)
Even though octave includes an editor, an external editor like notepad++ that has syntax highlighting for matlab/octave and other languages:
https://notepad-plus-plus.org/
Octave has very good numerical routines and graphics.
![Page 22: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/22.jpg)
Using Octave
Write two programs: rhs.m containing the function rhs that calculates the rhs of the equations and main.m with the main program, using either an editor or the octave GUI editor. Routines have to end with endfunction.
Use global to pass parameters.
Save them in the same folder. Open main.m from the editor in the GUI and run it using the tool bar.
![Page 23: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/23.jpg)
Command window
![Page 24: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/24.jpg)
Editor window
![Page 25: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/25.jpg)
Some examples in octave
The following examples illustrate the use of octave in typical epidemiological and ecological problems.
The code is included
Results are shown
![Page 26: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/26.jpg)
SIR model, main program
% SIR model with constant population. Recovered get immunity.
% Susceptibles, S(t)=x(1)
% Infectives, I(t)=x(2)
% Recovered, R(t)
% dS/dt=-a*S*I, dI/dt=a*S*I-b*I, dR/dt=b*I
% Use that the total population N=S+I+R to solve only first two equations
global a b;
a=.01;
b=.1;
%initial conditions
x0=[99;1];
%Total population
N=100;
![Page 27: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/27.jpg)
continuation
% time interval
t0 = 0;
tf =60;
% plot the 2-d vector field with arrows.
figure(1)
clf;
[S,I] = meshgrid(0:10:100);
%xp=sirrhs(1,[S,I]);
Sq = -a*S.*I;
Iq = a*S.*I-b*I;
quiver(S,I,Sq,Iq,1);
print -depsc2 sir_fig1.eps;
![Page 28: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/28.jpg)
continuation
[t x] = ode45('sirrhs',[t0 tf],x0);
%Calculate recovered
R= N-x(:,1)-x(:,2);
figure(2)
clf
plot(t,x(:,1),'b-','LineWidth',2,t,x(:,2),'r-','LineWidth',2,t,R,'g-','LineWidth',2);
ylabel('Population Size','fontsize',15);
xlabel('Time','fontsize',15);
legend('Susceptibles', 'Infectives', 'Recovered');
print -depsc2 sir_fig2.eps;
![Page 29: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/29.jpg)
continuation
figure(3)
clf
quiver(S,I,Sq,Iq,1);
hold on;
plot(x(:,1),x(:,2),'b-','LineWidth',2);
ylabel('Infectives','fontsize',15);
xlabel('Susceptibles','fontsize',15);
%legend('Susceptibles', 'Infectives', 'Recovered');
hold off;
print -depsc2 sir_fig3.eps;
![Page 30: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/30.jpg)
sirrhs.m, rhs of differential system
% the rhs of the SIR system. Saved as sirrhs.m
function xp = sirrhs(t,x)
global a b;
xp= zeros(2,1);
xp(1) = -a*x(1)*x(2) ;
xp(2) = a*x(1)*x(2)-b*x(2) ;
endfunction
![Page 31: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/31.jpg)
![Page 32: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/32.jpg)
![Page 33: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/33.jpg)
![Page 34: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/34.jpg)
Lotka-Volterra predator-prey model
In order to calculate fixed points, need to write the rhs as a function in a slightly different form
lotkafixed.m:
% the rhs of the Lotka-Volterra system. Saved as lotkafixed.m to calculate fixed points
function xp = lotkafixed(x)
global r b c m ;
xp= zeros(2,1);
xp(1) = r*x(1)-b*x(1)*x(2) ;
xp(2) = c*x(1)*x(2)-m*x(2) ;
endfunction
![Page 35: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/35.jpg)
Lotka.m, main program
% dN/dt=r*N-c*N*P, dP/dt=b*N*P-m*P
global r b c m;
r=2.;
b=1;
c=.5;
m=.8;
%initial conditions
x0=[2;1];
%find fixed points
[fixed,fval, info]= fsolve ('lotkafixed', x0)
![Page 36: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/36.jpg)
continuation
% time interval
t0 = 0;
tf =10;
% for vector field.
[N,P] = meshgrid(0:.5:4);
Nq = r*N-b*N.*P;
Pq = c*N.*P-m*P;
%Solve system of ODE's
opt = odeset ( "RelTol", 1e-6);
[t x] = ode45('lotkarhs',[t0 tf],x0,opt);
![Page 37: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/37.jpg)
continuation
figure(1)
clf
plot(t,x(:,1),'b-','LineWidth',2,t,x(:,2),'r-','LineWidth',2);
ylabel('Population Size','fontsize',15);
xlabel('Time','fontsize',15);
legend('Prey', 'Predator');
print -depsc2 lotka_fig2.eps;
![Page 38: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/38.jpg)
continuation
figure(2)
clf
quiver(N,P,Nq,Pq);;
hold on;
plot(x(:,1),x(:,2),'b-','LineWidth',2);
ylabel('Prey','fontsize',15);
xlabel('Predator','fontsize',15);
hold off;
print -depsc2 lotka_fig3.eps;
![Page 39: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/39.jpg)
lotkarhs.m, rhs of ode system
% the rhs of the lotka-volterra system. Saved as lotkarhs.m
function xp = lotkarhs(t,x)
global r b c m ;
xp= zeros(2,1);
xp(1) = r*x(1)-b*x(1)*x(2) ;
xp(2) = c*x(1)*x(2)-m*x(2) ;
endfunction
![Page 40: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/40.jpg)
![Page 41: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/41.jpg)
![Page 42: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/42.jpg)
Rosenzweig-MacArthur model for predator prey. Main program
% Rosenzweig-MacArthur model for predator prey
% Prey, N(t)=x(1)
% Predator, P(t)=x(2)
% dN/dt=r*N(1-n/K)-c*N*P/(h+N), dP/dt=b*N*P/(h+N)-m*P
%Predator prey with logistic growth for prey an Holling type interaction
global r b c m K h;
r=2.; b=1; c=.5; m=.8; K=6; h=1;
%with these new b and c two solutions
%b=2; c=1.5;
%initial conditions
x0=[2;1];
![Page 43: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/43.jpg)
continuation
%find fixed points
[fixed,fval, info]= fsolve ('rosenzfixed', x0)
% time interval
t0 = 0;
tf =30;
% For vector field
[N,P] = meshgrid(0:.5:4);
Nq = r*N.*(1-N./K)-b*N.*P./(h.+N);
Pq = c*N.*P./(h.+N)-m*P;
![Page 44: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/44.jpg)
continuation
%Solve system of ODE's
opt = odeset ( "RelTol", 1e-6);
[t x] = ode45('rosenzrhs',[t0 tf],x0,opt);
figure(1)
clf
plot(t,x(:,1),'b-','LineWidth',2,t,x(:,2),'r-','LineWidth',2);
ylabel('Population Size','fontsize',15);
xlabel('Time','fontsize',15);
legend('Prey', 'Predator');
print -depsc2 rosenz_fig2.eps;
![Page 45: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/45.jpg)
continuation
figure(2)
clf
quiver(N,P,Nq,Pq);;
hold on;
plot(x(:,1),x(:,2),'b-','LineWidth',2);
ylabel('Prey','fontsize',15);
xlabel('Predator','fontsize',15);
hold off;
print -depsc2 rosenz_fig3.eps;
![Page 46: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/46.jpg)
rosenzrhs.m, rhs of ode system
% the rhs of the Rosenzweig-MacArthur system. Saved as rosenzrhs.m
function xp = rosenzrhs(t,x)
global r b c m K h;
xp= zeros(2,1);
xp(1) = r*x(1)*(1-x(1)/K)-b*x(1)*x(2)/(h+x(1)) ;
xp(2) = c*x(1)*x(2)/(h+x(1))-m*x(2) ;
endfunction
![Page 47: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/47.jpg)
rosenzfixed.m, for fixed points
% the rhs of the Rosenzweig-MacArthur system. Saved as rosenzfixed.m to calculate %fixed points
function xp = rosenzfixed(x)
global r b c m K h;
xp= zeros(2,1);
xp(1) = r*x(1)*(1-x(1)/K)-b*x(1)*x(2)/(h+x(1)) ;
xp(2) = c*x(1)*x(2)/(h+x(1))-m*x(2) ;
endfunction
![Page 48: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/48.jpg)
![Page 49: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/49.jpg)
![Page 50: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/50.jpg)
Maxima
Symbolic and numerical calculations and plotting
Two main GUI’s:
Wxmaxima (more powerful)
Xmaxima.
Or command line
Wxmaxima has toolbar with the most common commands
https://htmlpreview.github.io/?https://github.com/andrejv/wxmaxima/blob/master/info/wxmaxima.html
![Page 51: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/51.jpg)
wxmaxima
![Page 52: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/52.jpg)
wxmaxima
![Page 53: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/53.jpg)
Maxima
Good short introductions are:
Introduction to Maxima by Richard Rand
http://maxima.sourceforge.net/docs/manual/intromax.pdf
A micro introduction to Maxima at Harvard
http://www.math.harvard.edu/computing/maxima/
This one also has an equivalence table with Mathematica
And of course the manual:
http://maxima.sourceforge.net/docs/manual/maxima.pdf
![Page 54: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/54.jpg)
Even though commands can be entered directly into wxmaxima, or using its toolbar, it is easier to write a batch file in an editor, save it with a name ending in .mac, example file.mac, and the clicking on the toolabar file > Batch file …> file.mac
Or copy and paste.
![Page 55: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/55.jpg)
Brief Introduction to Maxima
Place cursor on input cell and type 1+1; shift+enter to evaluate
End command with ; or $ to suppress output
% is the previous output or use %o1, %o2, etc.
Maxima tries to keep calculations precise so it doesn’t evaluate
sqrt(2*%pi); use float(%); to evaluate it.
To assign a name to a variable: var_name: value;
Define a function and integrate it:
f(x):= x^3$
Integrate(f(y),y);
![Page 56: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/56.jpg)
continuation
To make sure integral is well defined:
assume(a>0)$
Integrate(1/(x+a),x,0,1);
forget(a>0);
Some commands:
solve(a*x^2+b*x+c=0,x);
A: matrix([1,0], [%e. %pi]);
B: invert(A);
C: A.B; /* . Is the matrix product (this is a comment in maxima)*/
![Page 57: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/57.jpg)
eigenvalues(A);
Differentiation: use diff or ‘diff unevaluated derivative:
f(x):= exp(x)$
diff(f(x),x);
g(y):= log(y)+y^2$
diff (g(f(x)), x);
Exact solution of ODE’s:
soln: ode(‘diff(x,t,2) + 4*x=0, x,t); /*second order */
ic2(soln, t=0, x=1, ‘diff(x,t)=0); /*initial conditions */
![Page 58: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/58.jpg)
plotting
wxplot2d([sin(x)], [x-%pi,%pi])$
More options, ex: [lines, linewidth, line color]
wxplot2d([f(x),g(x)], [x, -2, 10], [xlabel, “x”], [ylabel, “y”], [legend, “f”, “g”], [style, [lines, 1,1], [lines 2,4]]);
Solve one ODE and plot it:
sol:rk([-x],x,2.,[t,0,1,0.1])$
wxplot2d([discrete,sol],[style,lines],[y,0,2],
[gnuplot_preamble, "set grid;"]);
![Page 59: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/59.jpg)
System of 2 ODE’s:
sol:rk([rhs1,rhs2],[x,y],[1,0],[t,0,5,0.1])$
sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$
sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$
wxplot2d([[discrete,sol1],[discrete,sol2]],
[style,[points,1]],[y,0,2]);
Or use rkf45 /* load(rkf45 */
![Page 60: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/60.jpg)
Other useful commands
kill(a); /* remove value of a*/
kill(all); /*remove all assigned values */
expr, numer; /* convert to floating point */
expr, t=0, numer; /* replace t by 0 in expr */
expr, ratsimp; /* simplify expr */
expr, ratsimp, numer;
![Page 61: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/61.jpg)
Discrete systems
Solve y[n+1]=F(y[[n]):
y[0]: 1$
y[n] := F(y[n-1])$
y[10];
load(dynamics)$ /* for graphing*/
evolution(F(y), y0, max_n)$ /* plots y[n] vs n */
staircase(F(y),y0,max_n)$ /* plots y[n+1] vs y[n] */
![Page 62: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/62.jpg)
Some examples using maxima
The following examples illustrate using maxima to find fixed points and corresponding eigenvalues symbolically, evaluating them for given values, doing numerical simulations and plotting results.
![Page 63: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/63.jpg)
SIRS model
Maxima will find the fixed points and the eigenvalues of the linearized problem at the fixed points symbolically for general values of the parameters. Sometimes numerical values have to be given. Even though it can symbolically solve ODE’s, our systems require numerical solution.
It will plot trajectories, phase planes, etc.
![Page 64: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/64.jpg)
sirs.mac, maxima batch file
remvalue(all);
load(drawdf)$
load(draw)$
load(rkf45)$
/* Define the rhs */
eq1:-b*S*I+c*(N-S-I);
eq2:b*S*I-e*I;
fixed:solve([eq1,eq2],[S,I]);
J: jacobian([eq1,eq2],[S,I]);
J1: ev(J,fixed[1]);
J2: ev(J,fixed[2]);
![Page 65: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/65.jpg)
continuation
/* first list eigenvalues, second list multiplicities */
eigenvalues(J1);
eigenvalues(J2);
/* alternative way */
J11:subst(fixed[1],J);
eigenvalues(J11);
/* give values to parameters */
b:.01;
c:.05;
e:.1;
N:100;
![Page 66: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/66.jpg)
continuation
nfixed:ev(fixed);
neq1:ev(eq1);
neq2:ev(eq2);
Jn: jacobian([neq1,neq2],[S,I]);
J1n: ev(Jn,fixed[1]);
J2n: ev(Jn,fixed[2]);
eigenvalues(J1n);
eigenvalues(J2n);
![Page 67: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/67.jpg)
continuation
/* draw phase portrait with some trajectories */
wxdrawdf([neq1,neq2],[S,I],[S,0,100],[I,0,100], duration=40, solns_at([90,10], [70,20], [50,40], [10,80]))$
/* Do time integration */
soln1: rkf45([neq1,neq2],[S,I], [90,10], [t,0,50])$
/* output is a list of a list with entries t, S, I */
soln11: makelist([first(soln1[i]),second(soln1[i])], i,1,length(soln1))$
soln12: makelist([first(soln1[i]),third(soln1[i])], i,1,length(soln1))$
/* calculate R recovered */
soln13: makelist([first(soln1[i]), N-second(soln1[i])-third(soln1[i])],i,1,length(soln1))$
![Page 68: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/68.jpg)
continuation
/* plot solution */
wxdraw2d(points_joined = true,yrange=[0,100],line_width = 2,color = red,points(soln11) )$
wxdraw2d(points_joined = true,yrange=[0,100],line_width = 2,color = blue,points(soln12) )$
wxdraw2d (points_joined = true,yrange=[0,100],line_width = 2,color = green,points(soln13) )$
wxdraw2d (points_joined = true,yrange=[0,100],line_width = 2,point_size=0,color = red, points(soln11), color=blue, points(soln12), color = green,points(soln13) )$
![Page 69: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/69.jpg)
SIRS output
![Page 70: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/70.jpg)
![Page 71: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/71.jpg)
![Page 72: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/72.jpg)
![Page 73: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/73.jpg)
![Page 74: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/74.jpg)
![Page 75: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/75.jpg)
![Page 76: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/76.jpg)
![Page 77: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/77.jpg)
![Page 78: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/78.jpg)
Lotka_Volterra predator-prey model
/* Lotka-Volterra N is the prey and P the predator*/
/* used rk and plot2d */
kill(all);
load(drawdf)$
/*load(draw)$
load(rkf45)$*/
/* Define the rhs */
eq1:r*N-c*N*P;
eq2:b*N*P-m*P;
fixed:solve([eq1,eq2],[N,P]);
![Page 79: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/79.jpg)
continuation
J: jacobian([eq1,eq2],[N,P]);
J1: ev(J,fixed[1]);
J2: ev(J,fixed[2]);
/* first list eigenvalues, second list multiplicities */
eigenvalues(J1);
eigenvalues(J2);
/* give values to parameters */
r:2.;
b:.2;
c:.5;
m:.8;
![Page 80: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/80.jpg)
continuation
nfixed:ev(fixed);
neq1:ev(eq1);
neq2:ev(eq2);
Jn: jacobian([neq1,neq2],[N,P]);
J1n: ev(Jn,fixed[1]);
J2n: ev(Jn,fixed[2]);
eigenvalues(J1n);
eigenvalues(J2n);
![Page 81: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/81.jpg)
continuation
/* draw phase portrait with some trajectories */
wxdrawdf([neq1,neq2],[N,P],[N,0,20],[P,0,15], duration=40, solns_at([9,1], [7,2], [5,4], [1,8]))$
/* Do time integration */
soln1: rk([neq1,neq2],[N,P], [2,1], [t,0,50, .1])$
/* output is a list of a list with entries t, S, I */
soln11: makelist([first(soln1[i]),second(soln1[i])], i,1,length(soln1))$
soln12: makelist([first(soln1[i]),third(soln1[i])], i,1,length(soln1))$
/* plot solution */
wxplot2d([[discrete, soln11],[discrete,soln12]], [style, [lines,2]])$
![Page 82: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/82.jpg)
Results
![Page 83: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/83.jpg)
![Page 84: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/84.jpg)
Rosenzweig-MacArthur Predator-prey, only functions definition
/* Rosenzweig-MacArthur N is the prey and P the predator*/
/* used rkf45 and draw2d */
kill(all);
load(drawdf)$
load(draw)$
load(rkf45)$
/* Define the rhs */
eq1:r*N*(1-N/K)-c*N*P/(h+N);
eq2:b*N*P/(h+N)-m*P;
….
![Page 85: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/85.jpg)
Results
![Page 86: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/86.jpg)
![Page 87: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/87.jpg)
Bifurcation center to saddle
/* dx/dty=x+mu*y, dy/dt=x-y */
/*Use plotdf that includes sliders but plots only one trajectory */
/* at mu=-1 center to saddle */
kill(all);
load(plotdf)$
eq1:x+mu*y;
eq2:x-y;
fixed:solve([eq1,eq2],[x,y]);
J:jacobian([eq1,eq2],[x,y]);
![Page 88: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/88.jpg)
continuation
fixed1: ev(fixed[1], mu=0);
J1: ev(J, fixed1, mu=0);
eigenvalues(J1);
/* mu <-1 */
fixed11: ev(fixed[1], mu=-2);
J11: ev(J, fixed11, mu=-2);
eigenvalues(J11);
![Page 89: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/89.jpg)
continuation
plotdf([eq1,eq2], [x,y],
[parameters,"mu=1."],
[trajectory_at,1,2],[tstep,0.001],
[x,-10,10], [y,-10,10], [direction,forward],
[nsteps,300], [sliders,"mu=-5:5"], [versus_t,1])$
![Page 90: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/90.jpg)
Results
![Page 91: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/91.jpg)
![Page 92: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/92.jpg)
SIR bifurcation, only function definition
/* Zhang Bif analysis of SIR */
/*Use plotdf that includes sliders but plots only one trajectory */
/* use lambda as the parameter. Do NOT assign valu until plodf*/
kill(all);
load(plotdf)$
A:15;d:.1;eps:.01;mu:.1;r:.8;alpha:.1; k:.1; /*endemic stable*/
d1:d+eps+mu;
eq1:A-d*S-lambda*S*I/(1+k*I);
eq2:lambda*S*I/(1+k*I)-d1*I-r*I/(1+alpha*I);
…..
![Page 93: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/93.jpg)
Results
![Page 94: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/94.jpg)
![Page 95: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/95.jpg)
Notebooks in Maxima
From inside wxmaxima you can write a notebook that includes text and maxima commands.
The text can be edited and the commands can be executed or modified.
Commands executed out of order may give unexpected results.
Always clear memory before loading a notebook.
The saved notebook can include the output or not.
![Page 96: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/96.jpg)
![Page 97: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/97.jpg)
Maxima notes
Maxima has many packages including contrib_ode, lapack, minpack.
Octave is better numerically.
The Euler math tool box
http://euler.rene-grothmann.de/
is a math program combining numerical and symbolic tools using Maxima in one free package. It is quite similar to Matlab, but not compatible.
![Page 98: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/98.jpg)
sage or sagemath is a free mathematics software system. It builds on top of many existing open-source packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, Rand many more.
Very powerful with commands in python like syntax.
It can be run on the cloud using Cocalc
https://cocalc.com/?utm_source=sagemath.org&utm_medium=landingpage
or SageMathCell
http://sagecell.sagemath.org/
![Page 99: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/99.jpg)
![Page 100: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/100.jpg)
xpp or xppaut
Solves numerically ordinary differential equations, delay equations, difference equations, functional equations and stochastic equations. Includes auto for bifurcation analysis.
Also plots.
Comes with a GUI.
Book: Simulating, Analyzing, and Animating Dynamical Systems: A Guide to Xppaut for Researchers and Students (Software, Environments, Tools) (Software, Environments and Tools) 1st Edition
by Bard Ermentrout , SIAM
![Page 102: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/102.jpg)
Write your equations, parameters (values you may want to change), initial conditions and xpp and auto parameters in a file with suffix .ode
Example:
![Page 103: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/103.jpg)
Simple bifurcation example
# Simple bifurcation du/dx=mu x-x^3; dy/dt=-y
par mu=1
x'=mu*x-x^3
y'=-y
x(0)=1
y(0)=0
@ xp=x,yp=y,xlo=-.25,xhi=1.25,ylo=-.5,yhi=1,total=100
@ maxstor=10000
done
![Page 104: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/104.jpg)
xpp notes
The lines starting with @ change default values of xpp and auto parameters. xp and yp are the functions to be plotted in the x and y axes. Total is the total integration time. Maxstor is the maximum number of stored values during the numerical integration.
Lines starting with # are comments
The file ends with done
Inside xpp you can change the values of the parameters, equations, initial conditions and more.
![Page 105: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/105.jpg)
xpp GUI
Drag the .ode file and drop it on xpp.bat
![Page 106: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/106.jpg)
Equilibrium points and their stability
Click on Sing pts -> Go to get an equilibrium point.
It may be necessary to repeat click on mouse or montecar to get others.
The cmd.exe windows gives the values of the eigenvalues:
-2.003 + i 0.000
-1.000 + i 0.000
![Page 107: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/107.jpg)
Direction field
Click on Viewaxes to choose y vs x and ranges
Click on Dir.field/flow ->Direct field
On upper left corner give Grid and return
If desired click on nullclines and on Initialconds ->Mice to choose some trajectories
![Page 108: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/108.jpg)
![Page 109: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/109.jpg)
Auto
Auto needs a steady solution (or a periodic solution) as a starting point. In xpp click Initialcons->Go and the I L several times until solution doesn’t change.
Click File->auto
Check Parameter for the continuation parameter and Numerics. Ds>0 increases the parameter while Ds<0 decreases it. Then Run->steadystates to compute the branch
![Page 110: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/110.jpg)
![Page 111: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/111.jpg)
Auto marks special points:
EP Endpoint of a branch
LP Limit point or turning point of a branch
TR Torus bifurcation from a periodic
PD Period doubling bifurcation
UZ User defined function
MX Failure to converge
BP Bifurcation or branch point
HB Hopf bifurcation
![Page 112: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/112.jpg)
To continue from a special point Grab, choose point by using arrows and enter. Maybe change parameters (Ds?) and run again. For a Hopf bifurcation Run->periodic
To save a diagram use File->postscript
Esc exits from a command.
Also look at cmd.exe window
![Page 113: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/113.jpg)
![Page 114: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/114.jpg)
Problem with limit point
# Simple bifurcation du/dx=mu-x^2; dy/dt=-y
par mu=1
x'=mu-x^2
y'=-y
x(0)=1
y(0)=0
@ xp=x,yp=y,xlo=-.25,xhi=1.25,ylo=-.5,yhi=1,total=100
@ maxstor=10000
done
![Page 115: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/115.jpg)
![Page 116: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/116.jpg)
An epidemic model
# Backward bifurcation of an epidemic model with saturated treatment
# function, Xu Zhang , Xianning Liu
# give parameters
param lambda=.01, d=.1, eps=.01, mu=.1, r=.8; alpha=.1, k=.1
# derived variables
!d1=(d+eps+mu)
#!A=d*(d+ eps+mu+r)/lambda
A=15
![Page 117: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/117.jpg)
Continuation
#define ode's
S'=A-d*S-lambda*S*I/(1+k*I)
I'=lambda*S*I/(1+k*I)-d1*I-r*I/(1+alpha*I)
#Initial conditions
S(0)=30
I(0)=10
S'=A-d*S-lambda*S*I/(1+k*I)
I'=lambda*S*I/(1+k*I)-d1*I-r*I/(1+alpha*I)
#Initial conditions
S(0)=30
I(0)=10
![Page 118: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/118.jpg)
Continuation
#Some xpp parameters
@total=200,dt=.1,xhi=160,ylo=0.,yhi=20.,bound=500,maxstor=100000,nplot=1,xp=S,yp=I
@ toler=.00001,meth=gear
@ ntst=30,dsmin=1e-4,dsmax=0.02,ds=.001,parmin=.01,parmax=1.5,autoxmin=0.0
@ autoxmax=40,autoymin=0,autoymax=20
@ nmax=10000
@ npr=1000
@ EPSU=.0001,EPSS=.0001,EPSL=.0001
done
![Page 119: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/119.jpg)
Lambda=.005
![Page 120: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/120.jpg)
Lambda=.001
![Page 121: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/121.jpg)
![Page 122: Introduction to Computational Methods - ema2018.ucad.snema2018.ucad.sn/wp-content/uploads/2018/05/Introduction-to... · Using Octave Write two programs: rhs.m containing the function](https://reader033.fdocuments.in/reader033/viewer/2022052314/5c10ffba09d3f2423a8b7529/html5/thumbnails/122.jpg)
Check for negative values: