Rewriting a function - function

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.

Related

Parse error in octave while I try to see the output

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 "#".

Verilog Function - Can't figure out my error

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

matlab functions

helo, I have the following function called stat.m
function [mean,stdev] = stat(x)
n = length(x)
mean = sum(x)/n
stdev = sqrt(sum((x-mean).^2/n))
I defined x as a vector which is [1,2,5,7,9]
how come when I type a = stat(x), matlab returns a = 5 for the last line at command prompt?
If you want to get both return values, you have to do this:
[a, b] = stat(x);
If you just do a = stat(x), MATLAB interprets that to mean that you only want the first return value.
because a gets the first argument mean
try to call it [a,b] = stat(x)

Calling function with changing input parameters in a loop Matlab

I have caught myself in a issue, I know its not that difficult but I couldnt figure out how to implement it. I have an m file that looks like
clear;
PVinv.m_SwF=20e3
for m=1:1:70;
PVinv.m_SwF=PVinv.m_SwF+1e3;
Lmin = PVinv.InductanceDimens();
Wa_Ac = PVinv.CoreSizeModel();
PVinv.CoreSelect(Wa_Ac);
[loss_ind_core,loss_ind_copper] = PVinv.InductorLossModel(PVinv.m_L_Selected);
Total_Inductor_Loss=loss_ind_core+loss_ind_copper
plot(PVinv.m_SwF,Total_Inductor_Loss,'--gs');
hold on
xlim([10e3 90e3])
set(gca,'XTickLabel',{'10';'20';'30';'40';'50';'60';'70';'80';'90'})
grid on
xlabel('Switching Frequency [kHz]');
ylabel('Power loss [W]');
end
And the function that is of interest is CoreSelect(Wa_Ac)
function obj = CoreSelect(obj, WaAc)
obj.m_Core_Available= obj.m_Core_List(i);
obj.m_L_Selected.m_Core = obj.m_Core_Available;
end
I want to change the value of i from obj.m_Core_List(1) to obj.m_Core_List(27) within that for loop of main m file. How can I get the value of the function coreselect when I call it in main m file
For eg for m=1 to 70 I want the function to take the value of i=1 then execute till plot command and then same with but i=2 and so on
Any suggestion would be really helpful
I'm not sure I understand your question perfectly, but I think you want to pass an index i to the CoreSelect function, and loop i from 1 to 27 outside of the function. Try this:
function obj = CoreSelect(obj, WaAc, i)
...
end
for i=1:27,
PVInv.CoreSelect(WaAc,i);
end

How to pass a function as argument

I'm using Scilab and I'm trying to make a function like the following:
function p = binary_search(myf,a,b)
The target is to make a binary_search to find such p that: myf(p) = 0 in [a,b].
I want to do something like this:
root = binary_search("x^3 - 10",1,2)
Where the first string is a definition of a function.
The only way I found is defining a function called x3:
function x = x3(p)
x = p^3 - 10;
endfunction
and then, inside binary_search, do something like:
fa = x3(a);
Any ideas?
Thank You!
If you have defined the function f(x) = x^3 - 10 , either using deff('y=f(x)','y=x^3-10') or the regular "function ... endfunction" syntax, then you can simply pass f as an argument: define
function r = binary_search(f,a,b)
% do the binary search here and store the result in r
endfunction
Then you can call
---> binary_search(f, 1, 2)
which works fine in SciLab.
In MATLAB/octave, the interpreter considers " f " as an equivalent for f(), i.e., it would execute the function f without arguments, which will result in an error "x undefined". To avoid this, you have to type an # in front of f:
---> binary_search( #f, 1, 2) %% in MATLAB/octave
Functions in Scilab can be passed as arguments to other functions. Therefore, if you have one function, f:
function y=f(x)
y = x^3 - 10
endfunction
you are free to pass that to another function,
root = binary_search("x^3 - 10",1,2)
deff is simply a way to quickly define a function- usually inline on the interpreter.
Alternatively, you can also pass an expression as a string to a function and have that evaluated using the evstr command:
function p = binary_search(expression, a, b)
evstr expression
//Rest of your code
endfunction
You would implement this on the interpreter thus:
expression = "a^3 - 10"
root = binary_search(expression, 1, 2)
I found a solution:
In the main window (the interpreter), I define the function like:
deff('[y] = square(x)','y=x^2')
Then, I call
bi(square,0,2)
In the function, I just do 'f(x)':
function [x] = bi(f,a,b)
fa = f(a);