Problem implementing function with input as string in octave - octave

I'm trying to implement a function astronomical_constants but its giving me error
'name' undefined near line 2 column 10
function val = astronomical_constants(name)
switch name
case 'gravitational_constant'
val.name = 'gravitational_constant';
val.value = 6.667e-11;
val.uncertainity = 1e-12;
case 'equitorial_radius_earth'
val.name = 'equitorial_radius_earth';
val.value = 6*10^(20);
val.uncertainty = 1e-12;
endswitch
endfunction
a = astronomical_constants('gravitational_constant').value
I'm not able to understand how I can implement it correctly. Pls help

Related

Substituting multiple inputs for a function with one function?

I am trying to see if I can simplify input by using a function that produces more than one output for use with another function. Is there any way I can do this? Do I HAVE to make a function to return single variables for each input?
--here is a snippet of what im trying to do (for a game)
--Result is the same for game environment and lua demo.
en = {
box ={x=1,y=2,w=3}
}
sw = {
box = {x=1,y=2,w=3}
}
function en.getbox()
return en.box.x,en.box.y,en.box.w,en.box.w
end
function sw.getbox()
return sw.box.x,sw.box.y,sw.box.w,sw.box.w
end
function sw.getvis()
return true
end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
if CheckCollision(en.getbox(),sw.getbox()) == true then
if sw.getvis() == true then
en.alive = false
end
end
print(tostring(en.alive))
I am expecting the enemy (en) to die (en.alive = false) but I am getting the error: input:25: attempt to perform arithmetic on a nil value (local 'w2')
You can find an explaination for the issue you are seeing here: Programming in Lua: 5.1 – Multiple Results
I suggest you read the whole page but here is a relevant section
A function call that is not the last element in the list always produces one result:
x,y = foo2(), 20 -- x='a', y=20
x,y = foo0(), 20, 30 -- x=nil, y=20, 30 is discarded
I suggest the following changes to get your code working. wrap your output from getbox into a table with clear keys that make it easy to understand.
function en.getbox()
return {
x = en.box.x,
y = en.box.y,
w = en.box.w,
h = en.box.w
}
end
function sw.getbox()
return {
x = sw.box.x,
y = sw.box.y,
w = sw.box.w,
h = sw.box.w
}
end
function CheckCollision(o1, o2)
return o1.x < o2.x + o2.w and
o2.x < o1.x + o1.w and
o1.y < o2.y + o2.h and
o2.y < o1.y + o1.h
end
Alternatively you can wrap the output of getbox "on the fly" like:
function CheckCollision(o1, o2)
return o1[1] < o2[1] + o2[3] and
o2[1] < o1[1] + o1[3] and
o1[2] < o2[2] + o2[4] and
o2[2] < o1[2] + o1[4]
end
if CheckCollision({en.getbox()}, {sw.getbox()}) == true then
if sw.getvis() == true then
en.alive = false
end
end
I do strongly encourage the first option over the last. The last option leads to code that is harder to follow and should be accompanied by clear comments explaining it.

TypeError: 'cv2.face_EigenFaceRecognizer' object is not callable

I ran into an error which i do not know where and what is causing it.
Please i need help.
def train(self,images,lables, recogType=0):
self.images = images
self.lables = np.array(lables)
'arg = recogType:[cv2.face.LBPHFaceRecognizer_create(),cv2.face.FisherFaceRecognizer_create(),cv2.face.EigenFaceRecognizer_create()'
recogs = cv2.face.LBPHFaceRecognizer_create(),cv2.face.FisherFaceRecognizer_create(),cv2.face.EigenFaceRecognizer_create()
self.recognizer = recogs[recogType]()
self.recognizer.train(self.images,self.lables)
The specific problem is with this line:
self.recognizer = recogs[recogType]()
By placing braces () on the end, you are trying to call the recognizer, as the error says. Change this to
self.recognizer = recogs[recogType]
//Disclaimer - there may be other problems.

Separate strings in a string with Actionscripts3

I'm trying to separate two part of a string, one is Title one is Value, RegExp is confused me. I need your help to solve this thanks
var pattern2:RegExp = new RegExp("TZ_NUM_ANSWER:Telegram code([0-9.-]+)");//TZ_NUM_ANSWER:Telegram code 32263
var data2:Object = pattern2.exec(response);
if (data2 != null && data2[1] != null)
{
var value2:Number = parseFloat(data2[1]);
trace("TZ_NUM_ANSWER " + value2);
txt_BUY1.text = String(value2);
}
Output:
TZ_NUM_ANSWER:Telegram code 32263
It must be:
"TZ_NUM_ANSWER:" "Telegram code 32263"
The result of split is an Array you can access to Array indexes and assign them to a variable.
var STR1:String = "TZ_NUM_ANSWER:Telegram code 32263";
var STR2:String;
var STR3:String;
trace(STR1.split(":"));
STR2 = STR1.split(":")[0];
STR3 = STR1.split(":")[1];
trace (STR2);
trace (STR3);
Result:
TZ_NUM_ANSWER
Telegram code 32263
Don't use RegEx for simple stuff. All you need is basic string methods:
response.split(":");

Octave function terminates inexplicably

I am implementing an interpolating function using Octave, and I have the following in a Sublime text file:
function root = HermiteInterp(x, y, yp)
Q = zeros(2*length(x), 2*length(x));
disp(Q);
z = zeros(1, 2*length(x));
new_y = zeros(1, 2*length(x));
for i = 1:length(x)
z((2*i)-1) = x(i);
z(2*i) = x(i);
new_y((2*i)-1) = y(i);
new_y(2*i) = y(i);
y_prime(2*i) = yp(i);
end
y_transpose = transpose(new_y);
disp(y_transpose);
yp_transpose = transpose(y_prime);
append_to_Q = [y_transpose, Q];
disp('test1');
disp('test2');
Yet the function never makes it to the display statement. What's causing this?
Use arrow keys to scroll up and down the GUI.

matlab: fmincon, pass variables into nonlcon

I know its a stupid question, but I have no idea how to solve it... Lets say I have something like:
x = fmincon(#myfun,x0,A,b,Aeq,beq,lb,ub,#mycon)
and later on :
function [c,ceq] = mycon(x)
c = ...
ceq = ...
How to pass additional variables into #mycon, such as
function [c,ceq] = mycon(x, variable)
if variable == 1
c = ...
ceq = ...
else
c = ...
ceq = ...
end
Thanks :)
You pass mycon as anonymous function:
x = fmincon(#myfun,x0,A,b,Aeq,beq,lb,ub,#(xx)mycon(xx,variable))
Note that variable is fixed at the moment the fmincon line is called.