I try to compile this code in Modelsim and keep getting the error:
** Error: (vlog-13069) q3.sv(2): near "Dividerr":
syntax error, unexpected IDENTIFIER, expecting ';' or '('.
The error is referencing line 2 of my code. I'm not sure what I'm doing wrong. Can someone enlighten me or highlight what I'm missing here? I want to compile this module and be able to write a test bench for it and simulate eventually, but this error is stumping me. I'm used to verilog, but not systemverilog, I have a feeling it's an easy fix.
Here's the code:
module integerDiv#(parameter width=16)();
function Divider Divide;
input [width-1:0] A, B;
reg signed [width-1:0] D;
reg [width-1:0] Result, Remainder;
integer i;
divider.Result_ = {width{1'b0}};
for (i=width-1; i>=0; i=i-1) begin
Result = (divider.Result_ << 1 ) + A[i];
Remainder = divider.Result_<<1;
Remainder = {Remainder[width-1:1], A[i]};
D = Remainder - B;
if ( D < 0 ) begin
divider.D_[i] = 1'b0;
divider.Result_ = Result;
end else begin
divider.D_[i] = 1'b1;
divider.Result_ = D;
end
end
endfunction
endmodule
You haven’t defined the return type, or maybe you need to import the definition from a package. Also you are mixing up the type name with the function name inside the body of the function.
As Serge said, at a minimum, it seems you are you referencing a file/module with a typo (two "rr"s). Also, remember, Systemverilog/verilog is case-sensitive Divider/=divider
Related
I wrote a function in octave and got an error like this:
parse error near line 6 of file D:\Evan\Kuliah\Smt 4\METNUM\newton_method.m
syntax error
y = #3*x^2 - 4*x;e
code:
function y = df(x)
y = #3*x^2 - 4*x;
end
I've changed the function to something like this
function y = df
y = #3*x^2 - 4*x;
end
but the result remains the same
The # doesn't belong there, without it it should work fine. Also I recommend using the pointwise operators, so you can evaluate the function on whole arrays, otherwise they will be interpreted as matrix operations:
function y = df(x)
y = 3*x.^2 - 4*x;
end
The right syntaxis is:
y= #(x)3x^2-4x
You forgot to write the variable "x" betweent braces after "#".
In the following code I would like to replace the definition of the function f
f=#(x)-x(1)*x(2)*x(3);
x0=[1 1 1];
lb=[0 0 0];
nonlincon=#constr;
x=fmincon(f,x0,[],[],[],[],lb,[],nonlincon)
function [c,ceq] = constr(x)
c = [2*x(1)*x(2)+2*x(1)*x(3)+2*x(2)*x(3)-100 ; 1-x(1)*x(2)];
ceq = [];
end
I replaced it with
function erg = f(x)
erg = -x(1)*x(2)*x(3);
end
but unfortunatly it doesn't work. What am I doing wrong?
The error message is "Not enough input arguments."
In your first code snippet, f is a function handle, while in the second, its a function.
You can easily make it a function handle to be called by fmincon by calling x=fmincon(#f,x0,[],[],[],[],lb,[],nonlincon)
Otherwise matlab is trying to call f and give its output to fmincon, which is not what you want to do.
When the below function is executed, I get the following error message "Output argument "max_idx" (and maybe others) not assigned during call to "find_node"."
I think the problem is in the fact that some output is produced after recursive loop. How can I solve this?
function max_idx = find_node(wt)
persistent all_idx_max;
node_degree=sum(wt,2);
maxval = max(node_degree);
all_nodes_with_max_val = ismember(node_degree,maxval);
idx_max = find(all_nodes_with_max_val);
if isempty(all_idx_max)
all_idx_max=1:8;
end
if size(idx_max,1)==3
max_idx=all_idx_max(idx_max(1))
elseif size(idx_max,1)>1
all_idx_max=idx_max;
find_node(wt(idx_max,idx_max)); <--problem happens in this path
else
max_idx=all_idx_max(idx_max)
end
I forgot to add output to the recursive part
function max_idx = find_node(wt)
persistent all_idx_max;
node_degree=sum(wt,2);
maxval = max(node_degree);
all_nodes_with_max_val = ismember(node_degree,maxval);
idx_max = find(all_nodes_with_max_val);
if isempty(all_idx_max)
all_idx_max=1:8;
end
if size(idx_max,1)==3
max_idx=all_idx_max(idx_max(1))
elseif size(idx_max,1)>1
all_idx_max=idx_max;
***max_idx =*** find_node(wt(idx_max,idx_max)); <--problem happens in this path
else
max_idx=all_idx_max(idx_max)
end
As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points
I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.