Getting started copypasta for Octave's symbolic package.

cplusplus latex octave symbolic-computation

I worked with the Octave symbolic package recently, and had more trouble than I would have thought remembering the syntax and how to use it. So here’s a little program that works, future self!

Also, there are C Code and latex\(^1\) converters!

This little program that computes the Euclidean distance between two 3D points.

function [] = TestSymbolic()
  
  
pkg load symbolic
 
syms p1 p2 p3 q1 q2 q3 P Q real
P = [p1; p2; p3];
Q = [q1; q2; q3];
 
expr = norm(P - Q)
 

ppoint = [25; 0; 1];
qpoint = [10; 0; 1]; 

euclideanDistance = norm(ppoint - qpoint);

expr0 = expr;
expr0 = subs(expr0, q1, qpoint(1));
expr0 = subs(expr0, q2, qpoint(2));
expr0 = subs(expr0, q3, qpoint(3));

expr0

ccode(expr0)

latex(expr0)


expr1 = expr;
expr1 = subs(expr1, q1, p1);

expr1

The latex version of expr0 looks like this: \(\sqrt{\left(p_{2} - q_{2}\right)^{2} + \left(p_{3} - q_{3}\right)^{2}}\).

There’s some weirdness with matrices and the symbolic package, which I did not get into. Read about it at the subs function reference.

\(^1\) No, I am not going to use the weird formatting for latex here, or worry about the difference between tex and latex.

© Amy Tabb 2018 - 2026. All rights reserved. The contents of this site reflect my personal perspectives and not those of any other entity.