Accessing argument inside function - octave

While trying to access function argument in octave i am getting following error : error: 's' undefined near line 5 column 18.
Below is the attached code snippet
function index = find_template_1D(t, s)
% TODO: Locate template t in signal s and return index
for i = size(s)
if (s(i)==t(1) && s(i+1) == t(2) && s(i+2)==t(3))
index = i;
endif
endfor
endfunction
index = find_template_1D(t, s);
Can someone help me to figure out what is the exact issue ?

Related

Please explain this Octave Error Code: op1 is 1x12, op2 is 1x3

Code:
function result = assess_polynomial_n(y,n)
[coeff, t, pred_accum] = fit_data(y, n);
[avg, std_dev] = compute_error(coeff,y);
[coeff1, t, pred_accum1] = fit_data(y, n+1);
[avg1, std_dev1] = compute_error(coeff1,y);
if avg1>avg
result = n;
elseif avg<0.1 && std_dev<0.1
result = n;
elseif pred_accum1.*(1)<0.0001
result = n;
else
result = 0;
end
end
***Error***
error: compute_error: operator -: nonconformant arguments (op1 is 1x12, op2 is 1x3)
error: called from
compute_error at line 11 column 17
assess_polynomial_n at line 19 column 18
__tester__.octave at line 38 column 1
Does anyone know why I could be getting this error? We're supposed to be working in MATLAB but pasting our code into Moodle which uses Octave if that clears anything up.
I also will post the functions I'm calling for reference since the error is occurring in Compute_Error. This is a part of an overall project where I was to define the functions I'm calling before creating assess_polynomial_n.
function [coeff, t, pred_accum] = fit_data(y, n)
len = length(y);
t = 10*[0:len-1];
coeff = polyfit(t, y, n);
pred_accum = polyval(coeff, t);
end
function [avg std_dev] = compute_error(x,y)
s=[];
for i=1:length(x)
error=abs(x(i)-y(i));
s=[s,error];
end
avg = mean(s);
std_dev = std(s);
end
The problem lies in this line of code:
[avg1, std_dev1] = compute_error(coeff1,y);
You are trying to compute the error between the original data and the coefficients of the fitted polynomial. It doesn't make sense, right?
Instead, you should compute the error between the original data and the fitted data, which is the polynomial's output at each t value. The correct code would be:
[avg1, std_dev1] = compute_error(pred_accum1,y);

Verilog conditional assign outputs X where there should be 1

I am currently building a sign extender in Verilog based on the one present in the ARMv8 processor, but after the first result is extended, every subsequent result makes a 1 in the output into an X. How do I get rid of the X?
The module and the quick test bench I made are shown below.
Sign Extender:
`timescale 1ns / 1ps
module SignExtender(BusImm, ImmIns);
output [63:0] BusImm;
input [31:0] ImmIns;
wire extBit;
assign extBit = (ImmIns[31:26] == 6'bx00101) ? ImmIns[25]:
(ImmIns[31:24] == 8'bxxx10100) ? ImmIns[23]:
(ImmIns[31:21] == 11'bxxxx1000xx0) ? ImmIns[20]:
1'b0;
assign BusImm = (ImmIns[31:26] == 6'bx00101) ? {{38{extBit}}, ImmIns[25:0]}:
(ImmIns[31:24] == 8'bxxx10100) ? {{45{extBit}}, ImmIns[23:5]}:
(ImmIns[31:21] == 11'bxxxx1000xx0) ? {{55{extBit}}, ImmIns[20:12]}:
64'b0;
assign BusImm = 64'b0;
endmodule
Test Bench:
`timescale 1ns / 1ps
`define STRLEN 32
`define HalfClockPeriod 60
`define ClockPeriod `HalfClockPeriod * 2
module SignExtenderTest;
task passTest;
input [63:0] actualOut, expectedOut;
input [`STRLEN*8:0] testType;
inout [7:0] passed;
if(actualOut == expectedOut) begin $display ("%s passed", testType); passed = passed + 1; end
else $display ("%s failed: 0x%x should be 0x%x", testType, actualOut, expectedOut);
endtask
task allPassed;
input [7:0] passed;
input [7:0] numTests;
if(passed == numTests) $display ("All tests passed");
else $display("Some tests failed: %d of %d passed", passed, numTests);
endtask
reg [7:0] passed;
reg [31:0] in;
wire [63:0] out;
SignExtender uut (
.BusImm(out),
.ImmIns(in)
);
initial begin
passed = 0;
in = 32'hF84003E9;
#10;
begin
passTest(out, 63'b0, "Stuff", passed);
#10;
in = 32'hf84093ea;
#10;
passTest(out, 63'b0, "Stuff", passed);
end
end
endmodule
You seem to be treating x as a "don't-care" value in your comparisons, but it is not. x is a specific value which represents "unknown". Since you drive your input signals to all known values (0 or 1), all your == comparisons resolve to x, and your output has x in it. You should only compare bits you are interested in. For example, change:
(ImmIns[31:21] == 11'bxxxx1000xx0) ? {{55{extBit}}, ImmIns[20:12]}:
to:
( (ImmIns[27:24] == 4'b1000) && (ImmIns[21] == 1'b0) ) ? {{55{extBit}}, ImmIns[20:12]}:
You need to make similar changes to all your comparisons.
Also, you drive BusImm with 2 continuous assignments. Get rid of this line:
assign BusImm = 64'b0;
These changes get the x out of your output.
Also consider using casez. Refer to IEEE Std 1800-2017, section 12.5.1 Case statement with do-not-cares.

GNU Octave print_usage usage

If I define this function in a file
function retval = my_fact (N)
if nargin ~= 1
print_usage ()
endif
retval = factorial (N)
endfunction
and call it from an interactive session, I get
>> my_fact ()
error: print_usage: 'my_fact' not found
My question is: Where should I write a "docstring" for factorial or the string I want octave to display?

Solving system of ODEs using Octave

I am trying to solve a system of two ODEs using Octave, and in particular the function lsode.
The code is the following:
function xdot = f (x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
t = linspace(0, 5000, 200)';
x0 = [1000; 1000];
x = lsode ("f", x0, t);
set term dumb;
plot(t,x);
I am getting continuously the same error, that "x" is not defined, and I do not know why. The error is the following:
warning: function name 'f' does not agree with function file name '/home /Simulation 1/sim.m'
error: 'x' undefined near line 17 column 17
error: called from
sim at line 17 column 9
It would we great that any of you could help me with this code.
You have two errors. One, you are not saving your source code with the proper name. Two, variable "x" is a vector, and nothing in your script indicates that. You should add a line "x = zeros(1,2);" right after "xdot = zeros(2,1);".
Try the following code:
function ODEs
t = linspace(0, 5000, 200);
x0 = [1000; 1000];
x = lsode (#f, x0, t);
fprintf('t = %e \t\t x = %e\n',t,x);
endfunction
function xdot = f(x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
x = zeros(1,2);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
Save it as ODEs.m and execute it. It does not plot anything, but gives you an output with the results for the t range you supplied.

Passing 2d array to function in Lua

It is possible to pass 2d array to a function as a paramter ?
I initialized an array like this :
tab={}
for i=1, 10 do
tab[i]={}
for z=1, 10 do
tab[i][z]= 0
end
end
and i have function like this :
function foo(data)
...
x = data[i][z] -- here i got error
...
end
The gave the error message attempt to index field '?' (a nil value)
All variables are declared and initialized.
Your code should work if it is initialized properly.
For example, the below code sample will output 3:
function foo(data)
local i, z = 1, 2
print(data[i][z])
end
local tab={}
for i=1, 10 do
tab[i]={}
for z=1, 10 do
tab[i][z]= i + z
end
end
foo(tab)
Maybe you can share the rest of your code? The following runs with no error:
tab={}
for i=1, 10 do
tab[i]={}
for z=1, 10 do
tab[i][z]= 0
end
end
function foo(data)
print(data[3][2])
end
foo(tab)
The gave the error message attempt to index field '?' (a nil value)
I got such errors while changing metatable of some variable.