Octave Cheatsheet

1 minute read

Odes in Octave

Ode support comes packaged with Octave 4.4.1.

The following example simulates an RC circuit driven by an input voltage.

\[\frac{dV_c}{dt} = \frac{V_{in}(t) - V_c}{RC}\]
clear

# Represent the ODE state-space, dx/dt = Ax + Bu 
# x = vc, A = -1/RC, B = 1/RC, u = vin
function [xdot] = odeRC(t, x0, R, C, tin, vin)
    vf = interp1(tin, vin, t);
    vr = vf - x0(1);
    xdot = zeros(size(x0));
    xdot(1) = vr / (R * C);
endfunction

T0 = 0.0;
TN = 2e-2;

R = 1e3;
C = 1e-6;

Vs = 5.0;

tsim = linspace(T0, TN, 1001);
vin = Vs * ones(size(tsim));
vin(101:401) = vin(101:401) * 0.0;
vin(501:601) = vin(501:601) * 0.0;

x0 = [Vs];
opt = odeset('MaxStep', 1e-4, 'Abstol', 1e-6, 'RelTol', 1e-6);

[t, x] = ode45(@odeRC, [T0, TN], x0, ops, R, C, tsim, vin);

# Plotting
figure('color', [0.4, 0.0, 0.2]);
ax = axes();
plot(tsim, vin, ':', 'lineWidth', 3, t, x, 'lineWidth', 3);

legend({'Input Voltage', 'Capacitor Voltage'}, ...
    'TextColor', 'w', 'FontSize', 12, ...
    'color', 'k', 'Location', 'southeast');
xlabel('time (s)', 'color', 'w');
ylabel('voltage', 'color', 'w');
axis([T0, TN, -1, 6]);
grid on;
set(ax, 'color', 'k', 'ycolor', 'w', 'xcolor', 'w', ...
    'linewidth', 2,'gridlinestyle', '--', ...
    'gridalpha', 0.25, 'FontSize', 12);

Installing a Package from Forge

See octave forge for packages available

In an octave terminal:

pkg install -forge name

Alternatively, download the file locally and run:

pkg install name-ver.tar.gz

Include the package an octave script.m file:

pkg load name