I need to draw and label contours of data given on a Delaunay grid. The function tricontour, part of the apparently no longer supported plot pkg, cannot do this, because the function returns a single argument (h):
pkg load plot
nx=ny=11;
[x,y]=ndgrid(linspace(-1,1,nx),linspace(-1,1,ny));
xp=[x(:),y(:)];
tp=[1,2,nx+1;nx+2,nx+1,2];
tp=kron(tp,ones(ny-1,1))+kron(ones(size(tp)),(0:ny-2)'*nx);
tp=kron(tp,ones(nx-1,1))+kron(ones(size(tp)),(0:nx-2)');
G=xp(:,1)+xp(:,2);
h=tricontour(tp,xp(:,1),xp(:,2),G,[-1:.5:1],'b');
[h]=clabel(c,h,[-4:2:6])
This MWE fails with
error: 'c' undefined near line 10 column 12
error: called from
Testricontourlabel at line 10 column 4
The matlab version of this function returns both c and h, and so is compatible with the matlab version of clabel.
Any ideas for a work around?
Related
I'm relying on broadcasting in the following element-by-element multiplication of a 2x2x2 matrix by a 1x2 matrix (broadcasting means the smaller matrix is repeated as many times as necessary to match the size of the larger one - see end for more explanation). One command gives a "nonconformant arguments" error when in a script run from the octave command line. But exactly the same command runs fine if typed at the octave command line.
Occasionally, the script doesn't give the error, but I haven't worked out exactly what arrangement the Universe has to be in to avoid the error (see later) -- whatever, running it within a script is as robust as a cheap garden strimmer.
I give a workround below, but I'd like to understand what's behind this error.
Here's my script file mwe_bcast.m:
# Minimum working example of broadcasting an element-by-element multiplication.
# The last line errors when run from octave as a script,
# but not when typed at the octave command line.
clear
test = [0,1;2,3];
test(:,:,2) = 3 - test;
test .*= 2;
factors = [1,2;3,4];
test .*= factors(1,:)
But here's the error when run as a script:
>> mwe_bcast
error: mwe_bcast: .*=: nonconformant arguments (op1 is 2x2x2, op2 is 1x2)
error: called from
mwe_bcast at line 9 column 6
Nonetheless, when I then type the last line at the command line there is no error, and the following output shows that the broadcasting has worked as expected:
>> test .*= factors(1,:)
test =
ans(:,:,1) =
0 4
4 12
ans(:,:,2) =
6 8
2 0
Even weirder: I checked whether this is repeatable after restarting octave. It is and it isn't! Sometimes, if I run the script any amount of times straight after starting octave (after just a cd to the appropriate directory), it runs without errors. But as soon as I edit the script in any way (just changing a comment, and sometimes even just opening it in the octave editor), then re-run the script, I get the error. But, other times, I get the error the first time I run the script after restarting octave.
Workround: I get no error from the script if I don't use the abbreviated assignment (.*=) for this broadcast element-by-element multiplication, i.e. replace the last line with:
test = test .* factors(1,:);
However, just as weirdly, note that the earlier use of .*= in the script to multiply the same matrix by a scalar always works without an error, even though it also relies on broadcasting.
Explanation of broadcasting: Essentially, the last line multiplies the matrix
test(:,:,1) = 0,1
2,3
test(:,:,2) = 3,2
1,0
element-by-element by [1,2] which broadcasts as if it were the matrix:
factors(:,:,1) = 1,2
1,2
factors(:,:,2) = 1,2
1,2
System:
GNU Octave, version 5.2.0
Ubuntu 20.04 LTE
Linux 5.15
I'm trying to solve the following ODE:
where R(T) is defined as:
This is my not so great attempt at using Octave:
1;
function xdot = f(t, T)
xdot = 987 * ( 0.0000696 * ( 1 + 0.0038 * ( T(t) - 25 ))) - ( 0.0168 * (T(t)-25 )) - (( 3.25 * 10 ^ (-13))) * ((T(t))^4 - (25^4));
endfunction
[x, istate, msg] = lsode( "f", 100, (t=linspace(0,3600,1000)'));
T_ref and T_infinity_sign are the same constant.
Why isn't my code correct?
If you type
debug_on_error(1)
on your octave session, and then run your code, you will see that the "f" part is called as expected, but then it fails inside lsode with the following error:
error: T(100): out of bound 1 (dimensions are 1x1)
error: called from
f at line 4 column 8
If you look at the documentation of lsode, it says it expects a function f whose first argument is a state vector x, and the second is a scalar, corresponding to time t at which that state vector occurs; f is expected to output the differential dx/dt at time t for state vector x.
From your code it seems that you are reversing the order of the arguments (and their meanings).
Therefore, when you passed T as a second argument to your function, lsode treats it like a scalar, so when you then try to evaluate T(t), it fails with an 'out_of_bounds_ error.
My advice would be, read the documentation of lsode and have a look at its examples carefully, and start playing with it to understand how it works.
PS. The debug_on_error directive launches the debugger if an error occurs during code execution. To exit the debugger type dbquit (or if you're using the GUI, click on the 'Quit Debugging Mode' button at the top right of the octave editor). If you don't know how to use the octave debugger, I recommend you spend some time to learn it, it is a very useful tool.
I'm attempting to run a density functional theory geometry optimization with the following script:
xyzToIonposOpt water.xyz 15
however, this relatively simple command returns the following error:
error: fun(0): subscripts must be either integers 1 to (2^63)-1 or logicals
error: called from
fminsearch>nmsmax at line 275 column 8
fminsearch at line 165 column 25
.tmp.m at line 43 column 8
here is line 275 of fminsearch.m:
f(1) = dirn * fun (x, varargin{:});
and line 165
[x, exitflag, output] = nmsmax (fun, x0, options, varargin{:});
A similar issue is described here: https://octave.1599824.n4.nabble.com/Issue-with-fminsearch-function-td4693803.html It seems to be an issue with Octave. however, I am not certain how to workaround this issue as I am not sure where the fminsearch function is called.
Thanks for your help!
The following is example code from Matlab. It doesn't run in Octave. The code is:
f = figure;
b = uicontrol(f,'Style','pushbutton');
b.Position = [100 100 50 20];
It is from the online documentation: https://www.mathworks.com/help/matlab/ref/matlab.ui.control.uicontrol-properties.html
In Octave, I get: error: scalar cannot be indexed with .
What change must be made to make this run in Octave?
MATLAB introduced the second version of the handle graphics system (HG2) a couple of years ago. Octave still uses the old system.
Every time you see handle.propery, you are dealing with HG2. In the original system, we used get(handle,'property') and set(handle,'property',newvalue). Note that MATLAB will not deprecate this original syntax any time soon, it is perfectly valid to use both forms with the newer versions of MATLAB. Thus, the set and get functions are to be preferred for compatibility reasons.
So you can replace
b.Position = [100 100 50 20];
with
set(b,'Position',[100 100 50 20]);
In octave what is the difference between log(a) and log a?
>>a
a =
1 2
>>log(a)
ans =
0.00000 0.69315
>>log a
ans = 4.5747
In the second example, Octave is interpreting 'a' as a char, converting 'a' to its ASCII representation (97) and then getting the natural logarithm.
log(97) = 4.5747
In general you have two ways to call functions: as a function or as a command. E.g.
save('test.txt')
save test.txt
When a function is used as a command, it assumes the input is a string.
Anyway newer version of Matlab and Octave have an error check for character input (there is little reason to compute the logarithm of the ASCII equivalent of a character).