Run Octave function form shell - function

It is possible to write a function directly in the octave shell?
A=147;
B=26.3;
C=5.4;
d=0.35*A;
function S=function_test(A,B,C,d)
S=2*A*B*C*d;
end
I tried this but if I wanted to know the value of "S", this error appears:
error: 'S' undefined near line 1, column 1

Yes it is possible. You does it correctly. But you missed indicating how you call the function. For me, no error occurs:
≫ function_test(1,2,3,4)
ans = 48
≫ res = function_test(1,2,3,4)
res = 48
≫ S = function_test(-1,3,5,7)
S = -210
≫

Related

Octave error (error: max_recursion_depth exceeded) how to fix it?

I have run the following code in the Octave (Octave-6.2.0 (Local) (GUI)) and the output is shown. I have made an error intentionally, but when I fix it, it does not recognize it. Even when I have copied and pasted the following code which does not have an error it shows the same error message. By the way, it can be solved if I close Octave and open it again.
P=[1200 3000 4200 5500];
I=[10000];
function result = irrm(P,I)
result = 100*irr(P,I)
end
irrm(P,I);
Output:
>> rr
result = 11.798
The code with an error is:
P=[1200 3000 4200 5500];
I=[10000];
function result = irr(P,I)
result = 100*irr(P,I)
end
irrm(P,I);
The output of the code with an error is:
>> rr
error: max_recursion_depth exceeded
error: called from
irr
irr at line 5 column 10
irrm at line 5 column 10
rr at line 7 column 1

incremental search method script errors

I wrote my very first octave script which is a code for the incremental search method for root finding but I encountered numerous errors that I found hard to understand.
The following is the script:
clear
syms x;
fct=input('enter your function in standard form: ');
f=str2func(fct); % This built in octave function creates functions from strings
Xmax=input('X maximum= ');
Xinit=input('X initial= ');
dx=input('dx= ');
epsi=input('epsi= ');
N=10; % the amount by which dx is decreased in case a root was found.
while (x<=Xmax)
f1=f(Xinit);
x=x+dx
f2=f(x);
if (abs(f2)>(1/epsi))
disp('The function approches infinity at ', num2str(x));
x=x+epsi;
else
if ((f2*f1)>0)
x=x+dx;
elseif ((f2*f1)==0)
disp('a root at ', num2str );
x=x+epsi;
else
if (dx < epsi)
disp('a root at ', num2str);
x=x+epsi;
else
x=x-dx;
dx=dx/N;
x=x+dx;
end
end
end
end
when running it the following errors showed up:
>> Incremental
enter your function in standard form: 1+(5.25*x)-(sec(sqrt(0.68*x)))
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
mtimes at line 63 column 5
Incremental at line 3 column 4
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
mtimes at line 63 column 5
Incremental at line 3 column 4
error: wrong type argument 'class'
error: str2func: FCN_NAME must be a string
error: called from
Incremental at line 4 column 2
Below is the flowchart of the incremental search method:
The problem happens in this line:
fct=input('enter your function in standard form: ');
Here input takes the user input and evaluates it. It tries to convert it into a number. In the next line,
f=str2func(fct)
you assume fct is a string.
To fix the problems, tell input to just return the user's input unchanged as a string (see the docs):
fct=input('enter your function in standard form: ', 's');

pyodbc AccessDB TypeError: ('Params must be in a list, tuple, or Row', 'HY000')

Running Python 3.7.2 32 bit on Windows 7 and using pyodbc package 4.0.25-cp27 32bit
I have tried multiple ways of passing through the params and keep getting the above error:
TypeError: ('Params must be in a list, tuple, or Row', 'HY000')
my inputfile is a txt file containing this:
TEST ,EU ,Totals , 30, 0.61, 0.00000000,GOLD ,01/03/2019,
TEST ,EU ,SubTotals , 40, 0.63, 0.00000000,GOLD ,01/03/2019,
A few versions:
qry = """INSERT INTO newtable ([Col1], [Col2], [Col3], [Col4], [Col5], [Col6], [Col7], [Col8]) VALUES (?,?,?,?,?,?,?,?);"""
with open (inputfile, "r") as afile:
for line in afile:
params = tuple(line.split(','))
cursor.executemany(qry, params)
conn.commit()
for the params value also tried:
params = list(line.split(','))
Also tried inserting all values into the list one by one:
params = list(line.split(","))
a = params[0]
b = params[1]
c = params[2]
d = params[3]
e = params[4]
f = params[5]
g = params[6]
h = params[7]
dbparams = [a,b,c,d,e,f,g,h]
cursor.executemany(qry,dbparams)
cursor.execute(qry, params[0:8]) worked
The executemany was causing the error - params must be in list, tuple or row
and without the [0:8] the list was passing through a '\n' at the end of the list causing the error - the SQL contains 8 parameter markers, but 9 parameters were supplied
Winning answer was:
cursor.execute(qry, params[0:8]) worked
thanks to #gordthompson for his prompt

Nonlinear fits in Octave

currently I'm using nonlin_curvefit function from GNU Octave's 'optim' package to fit data with . But this time I did also need the uncertainty of the returned parameters to determine the quality of the fit. After reading through the documentation I tied using the function curvefit_stat.
However whenever I alwas get errors using this functiona and I can't make any sense of the error message. I'm using Octave 4.2.2 from Ubuntu 18.04's default repository.
Standalone minimal example and error messages below. Startparameters init_cvg usually produces good result, while using init_dvg usually results in poor fits:
1;
x = linspace(-2*pi, 2*pi, 600);
ydata = 3.4*sin(1.6*x) + rand(size(x))*1.3;
f = #(p, x) p(1)*sin(p(2).*x);
function y = testfun(p ,x)
y = p(1).*sin(p(2).*x);
endfunction
init_cvg = [1; 1.1];
init_dvg = [1; 1.0];
[pc, mod_valc, cvgc, outpc] = nonlin_curvefit(f, init_cvg, x, ydata);
[pd, mod_vald, cvgd, outpd] = nonlin_curvefit(f, init_dvg, x, ydata);
hold off
plot(x, yd, "b.");
hold on;
plot(x, mod_valc, "r-");
plot(x, mod_vald, "color", [0.9 0.4 0.3]);
settings = optimset("ret_covp", true);
covpc = curvefit_stat(f, pc, x, ydata, settings);
covpd = curvefit_stat(f, pd, x, ydata, settings);
puts("sqrt(diag(covpc))")
sqrt(diag(covpc))
puts("sqrt(diag(covpd))")
sqrt(diag(covpd))
The first error message occurs when I use f as a model function, the second occurs when I use testfun instead:
>> curvefit_stat_TEST
error: label of objective function must be specified
error: called from
__residmin_stat__ at line 566 column 7
curvefit_stat at line 56 column 7
curvefit_stat_TEST at line 25 column 7
>> curvefit_stat_TEST
error: 'p' undefined near line 8 column 7
error: called from
testfun at line 8 column 5
curvefit_stat_TEST at line 25 column 7
>>
Could somebody confirm this error ?
I would appreciate any help.
I found the problem.
I needed to add "abjf_type", "wls" as arguments to optimset.

Octave Get line number of error

The error I'm getting is not very descriptive, it will tell me
error: gradientDescent: product: nonconformant arguments (op1 is 1x97, op2 is 97x2)
I know the file is gradientDescent.m, but is there a way to get the line number of the error?
lasterror() contains a stack trace.
In my application, I'm getting the message "error: plot: properties must appear followed by a value", with no line number.
After I get the prompt back, I can example the stack trace like this:
octave:23> lasterror().stack
ans =
6x1 struct array containing the fields:
file
name
line
column
scope
context
octave:24> lasterror().stack.file
ans = /usr/share/octave/4.2.2/m/plot/draw/private/__plt__.m
ans = /usr/share/octave/4.2.2/m/plot/draw/plot.m
ans = /home/baccala/WAM/waveletauditory/SOM-Toolbox/som/vis_trajgui.m
ans = /home/baccala/WAM/waveletauditory/SOM-Toolbox/som/vis_trajgui.m
ans = /home/baccala/WAM/waveletauditory/SOM-Toolbox/som/vis_trajgui.m
ans = /home/baccala/WAM/waveletauditory/SOM-Toolbox/som/vis_trajgui.m
octave:25> lasterror().stack.line
ans = 96
ans = 223
ans = 616
ans = 567
ans = 548
ans = 253
octave:26>
Which tells me that the troublesome call to plot in on line 616 of vis_trajgui.m.