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.
Related
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.
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
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
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.
I wanted to know if there exist a way to achieve someting like
-->80923=myCoolFunction(p,56,7)
p=24
in Scilab? Here is the current result when I type this:
-->8761.7635 = tauxcompose(p, 4, 25)
Warning: obsolete use of '=' instead of '=='.
!
!--error 4
Undefined variable: p
Thanks for any answer.
You must use fsolve, as documented # https://help.scilab.org/docs/6.1.0/en_US/fsolve.html
Example:
function r = myCoolFunction(p,a)
r = p .^2 .* exp(-a*p)
endfunction
p is the variable. a is an extra parameter (like in your example a=56, etc).
Now we search for p such that for instance myCoolFunction(p,1) == 100, assuming that there is at least one solution. We build
deff("out = gap(p,a,c)","out = myCoolFunction(p,a) - c")
Initial guess p0 : Display the plot of myCoolFunction(p,1) and see where it is around 100
p0 = -3;
Then we call fsolve:
[p,v,status] = fsolve(p0, list(gap,1,100))
Result:
--> [p,v,status] = fsolve(p0, list(gap,1,100))
p =
-2.6534493
v =
0. means: the found solution p is exact (up to the default relative tolerance 1e-10)
status =
1. means: fsolve() has well converged to a solution
Independent checking:
--> myCoolFunction(p,1)
ans =
100.
Now, what is p to get myCoolFunction(p, 2) == 1e4 ?
p = fsolve(-4, list(gap, 2, 1e4))
myCoolFunction(p, 2)
Result and checking:
--> p = fsolve(-4, list(gap, 2, 1e4))
p =
-3.3856301
--> myCoolFunction(p, 2)
ans =
10000.000