how drop near zero terms in Octave symbolic - octave

In this post I describe a symbolic computation I'm doing in Matlab that results in some terms remarkably small, near zero. I can run the same code in Octave (after loading the symbolic engine with pkg load symbolic in command window) and get the same results.
syms Zaa Zab Zac Zba Zbb Zbc Zca Zcb Zcc;
Z = [Zaa Zab Zac; Zba Zbb Zbc; Zca Zcb Zcc]
a = -1/2+sqrt(3)/2*1i;
A = [1 1 1; 1 a^2 a; 1 a a^2];
Z012 = inv(A)*Z*A
Is there a way to tell Octave to make those near-zero values actually zero?
Sample result: Zaa*(1/3 + i/36028797018963968)
Would like rounded to: Zaa*(1/3)
Update 1: My other post on this issue in Matlab has been answered - the solution being to use sqrt(sym(3)) in the a equation instead of just sqrt(3). That solution doesn't work in Octave however. If I run the code below in Octave I get the error blurb shown at bottom.
Code:
syms Zaa Zab Zac Zba Zbb Zbc Zca Zcb Zcc;
Z = [Zaa Zab Zac; Zba Zbb Zbc; Zca Zcb Zcc]
a = -1/2+sqrt(sym(3))/2*1i; % the sym(3) helps as
A = [1 1 1; 1 a^2 a; 1 a a^2];
Z012 = inv(A)*Z*A
vpa(3*Z012,2) % to clean up the display (remember to put a 1/3 factor in front of result)
Here is the resulting error blurb:
Symbolic pkg v3.0.0: Python communication link active, SymPy v1.10.1.
Z = (sym 3x3 matrix)
[Zaa Zab Zac]
[ ]
[Zba Zbb Zbc]
[ ]
[Zca Zcb Zcc]
warning: passing floating-point values to sym is dangerous, see "help sym"
warning: called from
double_to_sym_heuristic at line 50 column 7
sym at line 379 column 13
plus at line 53 column 5
symbolic_similarity_transformation at line 11 column 3
error: octave_base_value::map_value(): wrong type argument 'scalar'
Final update: This code runs in both Octave and Matlab and gives identical results.
syms Zaa Zab Zac Zba Zbb Zbc Zca Zcb Zcc;
Z = [Zaa Zab Zac; Zba Zbb Zbc; Zca Zcb Zcc]
a = -1/2+sqrt(sym(3))/2*1i;
A = [[1 1 1]; 1 a^2 a; 1 a a^2];
Z012 = inv(A)*Z*A
vpa(3*Z012,2) % to clean up the display (remember to put a 1/3 factor in front of result)

If a row contains all non-sym entries then you need to define such a row as a row vector to concatenate it with sym rows. i.e. A should be this:
A = [[1 1 1]; 1 a^2 a; 1 a a^2]; %works in both Octave and MATLAB
% ↑ ↑

Related

Please specify the error in this octave code

So I am trying to solve this problem from coursera regarding onevsAll classification. It comes under the ml course under Andrew NG. I am not able to find mistake in my code and it keeps showing this error
error: fmincg: operator +: nonconformant arguments (op1 is 401x1, op2 is 4x1)
error: called from
fmincg at line 87 column 5
oneVsAll at line 60 column 13
ex3 at line 77 column 13
I have tried reading the fmincg code but I cannot understand the problem.
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
for c = 1:num_labels
initial_theta = zeros(n+1,1) ;
options = optimset('GradObj', 'on', 'MaxIter', 50);
[theta] = fmincg (#(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);
all_theta(c, :) = theta;
end;
Program is showing error mentioned above regarding the fmincg file.

Octave - System of differential equations with lsode

here is my problem. I'm trying to solve a system of two differential equations thanks to the two functions below. The part of the code that give me some trouble is the variable "rho". "rho" is a function which values are given from a file and that I tried to put in the the variable rho.
function [xdot]=f2(x,t)
# Parameters of the equations
t=[1:1:35926];
x = dlmread('data_txt.txt');
rho=x(:,4);
beta = 0.68*10^-2;
g = 1.5;
l = 1.6;
# Definition of the system of 2 ODE's :
xdot(1) = ((rho-beta)/g)*x(1) + l*x(2);
xdot(2) = (beta/g)*x(1)-l*x(2);
endfunction
.
# Initial conditions for the two variables :
x0 = [0;1];
# Definition of the time-vector -> (initial time,temporal mesh,final time) :
t = linspace (1, 10, 10000);
# Resolution with the lsode routine :
x = lsode ("f2", x0, t);
# Plot of the two curves :
plot (t,x);
When I run my code, I get the following error:
>> resolution_effective2
error: f2: A(I) = X: X must have the same size as I
error: called from
f2 at line 34 column 9
resolution_effective2 at line 8 column 3
error: lsode: evaluation of user-supplied function failed
error: called from
resolution_effective2 at line 8 column 3
error: lsode: inconsistent sizes for state and derivative vectors
error: called from
resolution_effective2 at line 8 column 3
I know that my error comes from a mismatch of size between some variable but I have been looking for the error for days now and I don't see. Could someone try to give and explain me an effective correction ?
Thank you
The error might come from your function f2. Its first argument x should have the same dimension as x0 since x0 is the initial condition of x.
In your case, whatever you intent to be the first argument of f2 is ignored since in your function you do x = dlmread('data_txt.txt'); this seems to be a mistake.
Then, xdot(1) = ((rho-beta)/g)*x(1) + l*x(2); will be a problem since rho is a vector.
You need to check the dimensions of x and rho.

Octave: expecting scalar N, or 2-/4-element vector DOM-ezsurf Error

I need some kind of help about ezsurf
as I understand from my research matlab fplot is equal to ezplot in octave. And I write a script
fx = #(x,y) x.*sin(y);
fy = #(x,y) -x.*cos(y);
fz = #(x,y) y;
ezsurf(fx, fy, fz,[-5 5 -5 -2],'--','EdgeColor','g')
hold on
ezsurf(fx, fy, fz,[-5 5 -5 -2],'EdgeColor','none')
hold off
if I able to run the script I expect to see this image
However when I run the script I get this error
error: ezsurf: expecting scalar N, or 2-/4-element vector DOM
error: called from
__ezplot__ at line 260 column 7
ezsurf at line 78 column 19
file at line 4 column 1
What is the "expecting scalar N" what should I understand I how can I fix this.
Thank you so much in advance
fx = # (x,y) x .*sin(y);
fy = # (x,y) -x .*cos(y);
fz = # (x,y) y;
h1 = ezsurf (fx, fy, fz, [-5, 5, -5, -2]);
set (h1, 'edgecolor', 'g', 'linestyle', '--', 'linewidth', 0.1);
hold on
h2 = ezsurf (fx, fy, fz, [5, -5, 5, -2]);
set (h2, 'edgecolor', 'none');
hold off
colormap (parula (256)); % assuming you have the parula colormap installed

Octave leasqr only doing one iteration

As I'm trying to fit a function to some experimental data, I've written a function with three inputs, three parameters and one output:
qrfunc = #(x, p) exp(-1*p(1)*x(:,1) - p(2)*x(:,2))+p(3)*x(:,3)+20;
When I generate some input and output values:
pS = [0.5; 0.3; 0.3];
x1 = [1 1 1; 1 1.1 1; 1 1.1 1.1; 2 1.2 2];
y1 = qrfunc(x1, pS);
And call the leasqr function:
pin =[1; 1; 1];
[f1, p1, kvg1, iter1, corp1, covp1, covr1, stdresid1, Z1, r21] = leasqr(x1, y1, pin, qrfunc, 0.0001);
This works correct, the function makes 7 iterations and provides the right outputs.
But when I load x1 from my experimental data (a text file with three columns, about 1500 lines) as well as my y1 (a text file with the same amount of lines) and run the same function, it only makes one iteration, and does not change the parameters.
It even shows that the error margins are very high:
sqrt(diag(covp1))
ans =
3.0281e+004
3.7614e+005
1.9477e-002
What am I doing wrong? There are no error messages, no 'Convergence not achieved' or anything like that...
Edit:
The data is loaded with the command:
load "input.txt"
load "output.txt"
Proof of loading:
size(input)
ans =
1540 3
The first few lines from my input file:
10 0.4 5
20 0.4 5
30 0.4 5
40 0.4 5
50 0.4 5
The second and third parameters have different values further down the line.

GNU octave's mean function

K guys:
What is the mean of {1, 2, 3}
2 right?
Apparently not:
octave:50> B = [1, 2, 3]
B =
1 2 3
octave:51> mean(B)
ans =
0.42478
0.55752
0.73451
octave:52> B = [1;2;3]
B =
1
2
3
octave:53> mean(B)
ans =
0.42478
0.55752
0.73451
Do I just not know what a mean is?
You are probably using a different mean function than the default defined in Octave. I have tried your code and get 2 as an answer. To determine this, enter the following command at the octave prompt
which mean
This is my output
`mean' is a function from the file /usr/share/octave/3.4.3/m/statistics/base/mean.m
If I define mean by entering the following code
function retval = mean (v)
retval = v / e;
endfunction
I get a different answer for mean(B) when B = [1, 2, 3]
ans =
0.36788 0.73576 1.10364
If I enter the command which mean I now get this
`mean' is a command-line function