Sample Calls to Octave
If y is a function of t and satisfies the following differential
equation
y'' + .1* y' + sin(y) = 0;
If we use x(1) to denote a variable 'x' with subscript 1,
then this may be coded in octave as a first order matrix equation:
y = x(1),
y' = x(2)
so eliminating y altogether:
x(1)' = x(2)
x(2)' = -sin(x(1) - .1 * x(2)
In Octave we define a vector function 'pend' which encodes this, and
then call the 'lsode' linear solver. The t is ranging from values 0
to 40, with 200 steps. Then we plot the solution vector [x(1),x(2)]
as two plots against t. The [0.1,0.2] correspond to an initial value
of X. The initial value is at the first value of t (in this case 0)
of the range requested.
The plot will have 2 curves in this case, and the 'line 1' corresponds
to x(1), the 'line 2' corresponds to x(2). Remember x = [y,y'] so
that line 1 correpsonds to y and line 2 to y'. The "-@" says plot
lines and points.
Octave has complete documentation.
function xdot = pend(x,t)
xdot(1) = x(2); xdot(2) = -sin( x(1)) - 0.1*x(2);
end
sol=lsode( "pend",[0.1, 0.2], t = linspace(0,40, 200));
plot( t, sol,"-@")
Here are some more differential equation solutions.
- function xdot = bruss(x,t)
xdot(1) = 0;
xdot(2) = 0;
xdot(3) = x(1) - (x(2) + 1) * x(3) + x(3)*x(3)*x(4);
xdot(4) = x(2) - x(3)*x(3)*x(4);
endfunction
sol=lsode( "bruss",[0.5, 1.5,0.5,1.5], t = linspace(0,40, 200));
plot( t, sol,"-@")
You can graph two solutions to the same or different equations
on the same graph, just by putting them in the same function.
Here we select only the solutions, not their derivatives.
- function xdot = bruss(x,t)
xdot(1) = x(2);
xdot(2) = -x(1)+ sin(1.1*t) ;
xdot(3) = x(4);
xdot(4) = -x(3) ;
endfunction
sol=lsode( "bruss",[0,1.5,0,1.5], t = linspace(0,80, 400));
plot( t, [sol(:,1)';sol(:,3)'])
-
We change the damping factor in the pendulum above
function xdot = pend(x,t)
xdot(1) = x(2);
xdot(2) = -sin( x(1)) - 0.6*x(2)**2;
end
sol=lsode( "pend",[0.1, 0.2], t = linspace(0,20, 200));
plot( t, sol,"-@");
- function xdot = pend(x,t)
xdot(1) = .1*x(1) +.2* x(2)+.2*x(3);
xdot(2) = .1*x(1) + 2*x(2) + .3*x(3);
xdot(3) = .22*x(1)+x(2) + x(3);
end
sol=lsode( "pend",[1.0, 2.0,3.0], t = linspace(0.0,3.0, 200));
plot( t, sol,"-@")
Plotting
There are two commands for plotting: mesh and plot depending
for 3d and 2d plots.
mesh
This can take either 1 or 3 arguments, which are vectors or matrices.
After clicking on rotate in the window, you can rotate the plot to view
it from different angles. The original 3d coordinates of points are
displayed in the upper left corner.
- mesh for 3d with 1 argument
mesh(2*[0,0,0,0,0;1,1,1,1,1]-1,
2*[0,1,1,0,0;0,1,1,0,0]-1,2*[0,0,1,1,0;0,0,1,1,0]-1)
- make a matrix 15x5 as a product of a 15x1 and 1x5 matrix and plot it:
z = reshape(sin(linspace(-2,3,15)),15,1)*reshape(linspace(0,2,5),1,5);
mesh(z);
- plot a function of x and y as a surface 2-y2
z = ones(10,10); x=linspace(-2,2,10); y= linspace(-2,1,10);
for i=1:10; for j=1:10; z(i,j)= x(i)^2-y(j)^2; end ; end
mesh(x,y,z);
plot
Make 2 dimensional plots. This takes an arbitrary number of
arguments, which are basically matrices or format directives.
With just 1 argument it plots the values against the ticks,
eg a vector with 50 entries will have 1:50 plotted on the x axis
and the values on the y axis.
If there are two arguments the second is plotted against the first.
If a format argument such as "-@" (meaning plot points) then this is
applied to the preceding plot.
Then the whole procedure is repeated for the remaining arguments.
- Plot cos and sin against x on -5 to 5:
x= linspace(-5,5,50); plot(x,[sin(x);cos(x)])
- Note the following plots against the ticks rather than against x
Note also the use of "'"to take the transpose of the matrix.
x= linspace(-5,5,50); plot([sin(x);cos(x)]')
- the following is 4x2 matrix after transposing: [linspace(1,3,4);linspace(1,8,4)]'
- plot 2 curves the first with points displayed and the second just
the curves.
x= linspace(-5,5,50); plot(x,sin(x),"-@",x,cos(x));