CoCalc Public Filesoctave.ipynb
Author: Harald Schilly
Views : 509
Description: octave 5.2.0
Compute Environment: Ubuntu 18.04 (Deprecated)
In [1]:
version()

ans = 5.2.0 
In [2]:
[2 3 4]' * [4 3 -1]

ans = 8 6 -2 12 9 -3 16 12 -4 
In [3]:
x = rand(3,3)^3

x = 1.06801 0.90711 1.08065 1.64741 1.47672 1.82312 1.73806 1.64319 2.03286 
In [4]:
save r-octave.mat x -7

In [5]:
scatter(sort(rand(1000, 1)), sort(randn(1000, 1)))

In [6]:
i = 0:.1:2*pi;
plot(i, sin(i))

In [7]:
pkg load dicom

In [8]:
dicomuid()

ans = 1.2.826.0.1.3680043.2.1143.2148357442172746621572640965233790846 
In [9]:
pkg load symbolic;
syms x;
f = sin(x);
diff(f,x)

Symbolic pkg v2.9.0: Python communication link active, SymPy v1.5.1. ans = (sym) cos(x) 
In [10]:
pkg load symbolic; syms x
f = 2 * (cos(x) + sin(x)^2)
f1 = diff(f, x)

f = (sym) 2 2⋅sin (x) + 2⋅cos(x) f1 = (sym) 4⋅sin(x)⋅cos(x) - 2⋅sin(x) 
In [11]:
xx = -10:0.1:10;
plot(xx, f(xx))

error: invalid indices: should be integers or boolean error: called from subsref at line 65 column 11 
In [12]:
pkg load image

In [13]:
a = ones(100, 100);
b = ones(100, 100);
b(3, 1) = .5;
psnr(a, b)

ans = 46.021 

This plot shows the famous 3D sombrero.

A quadratic meshgrid of $x$ and $y$ coordinates is evaluated via $\sqrt{x^2 + y^2} + \epsilon$ and the value $r$ is then the value plotted along the third dimension.

Reference: 3d plots

In [14]:
tx = ty = linspace (-8, 8, 41)';
[xx, yy] = meshgrid (tx, ty);
r = sqrt (xx .^ 2 + yy .^ 2) + eps;
tz = sin (r) ./ r;
mesh (tx, ty, tz);
xlabel ("tx");
ylabel ("ty");
zlabel ("tz");
title ("3-D Sombrero plot");

In [1]:
[x,y] = meshgrid(-16:0.5:16);
r = hypot(x,y)/2 + eps;
figure;
surf(sin(r)./r);
colormap(jet);

In [ ]:



This draws the set of points, where the given equation is satisfied. Here, it shows a tilted ellipse.

$x^2 + 3 (y-1)^2 + \frac{x y}{2} = 6$

Reference: ezplot

In [15]:
ezplot (@(x, y) x.^2 + 3 * (y - 1).^2 + .5 * x .* y - 6)


Imagine you want to evaluate a binary function $f(x,\,y) := x + 2 y$.

For evaluating it in vectorized notation, you need a grid for the cartesian product of all $x$ and $y$.

In [16]:
x = 0:3;
y = 0:4;
[xx, yy] = meshgrid(x, y);
xx + 2*yy

ans = 0 1 2 3 2 3 4 5 4 5 6 7 6 7 8 9 8 9 10 11 

## dsolve and sympy in symbolic

In [1]:
pkg load symbolic
syms y(x)
de = diff(y) == x;
f = dsolve(de, y(1) == 1)

Symbolic pkg v2.9.0: Python communication link active, SymPy v1.5.1. f = (sym) 2 x 1 y(x) = ── + ─ 2 2 

## Octave's ODE PKG in Action

In [17]:
pkg load odepkg;

In [18]:
dxdt = @(t, x) - 0.24 * x.^2 + t;
tsteps = [0:0.1:5];
[t, x] = ode45(dxdt, tsteps, [-1:0.5:3]);
plot(t, x)

In [ ]:


In [ ]:



You can run numerical optimizations via the optim package.

In this example we minimize the classical Rosenbrock function in 20 dimensions using BFGS.

In [20]:
pkg load optim;

function [obj_value, gradient] = objective(theta, location)
x = theta - location + ones(rows(theta),1); # move minimizer to "location"
endfunction

dim = 20;                 # dimension of Rosenbrock function
theta0 = zeros(dim+1,1);  # starting values
location = (0:dim)/dim;   # true values
location = location';
control = {Inf,1};        # maxiters, verbosity

bfgsmin("objective", {theta0, location}, control);

------------------------------------------------ bfgsmin final results: 65 iterations function value: 3.42399e-16 STRONG CONVERGENCE Function conv 1 Param conv 1 Gradient conv 1 used analytic gradient param gradient (n) change 0.00000 0.00000 -0.00000 0.05000 -0.00000 0.00000 0.10000 0.00000 0.00000 0.15000 -0.00000 0.00000 0.20000 -0.00000 0.00000 0.25000 0.00000 0.00000 0.30000 -0.00000 0.00000 0.35000 -0.00000 0.00000 0.40000 -0.00000 0.00000 0.45000 0.00000 0.00000 0.50000 -0.00000 0.00000 0.55000 0.00000 -0.00000 0.60000 -0.00000 0.00000 0.65000 -0.00000 0.00000 0.70000 0.00000 -0.00000 0.75000 -0.00000 0.00000 0.80000 0.00000 0.00000 0.85000 -0.00000 0.00000 0.90000 0.00000 0.00000 0.95000 -0.00000 0.00000 1.00000 0.00000 0.00000 
In [ ]: