I want to minimize a function with many variables
Suppose I have this function
function b = three_var(v,c)
p=length(v);
b = v(1)^2 + 2.5*sin(v(2)) -prod(v.*v)*c;
end
Then it is easy to do this
c=2;
fun=@(v)three_var(v,c);
v = [-0.6,-1.2,0.135, 1];
b_min = fminsearch(fun, v)
But suppose that the function is given by
function b = three_var(v,k,c)
p=length(v);
b = v(1)^2 + 2.5*sin(v(2)) +sum(k)-prod(v.*v)*c;
end
Where k is a vector. How can I find a way that minimizes my scalar b.
c=2;
fun=@(v,k)three_var(v,k,c);
v = [-0.6,-1.2,0.135, 1];
k=[3,2,1];
b_min = fminsearch(fun, v,k)
I am getting an obvious error because I am not using fminsearch in the right way but you know what i mean:
Error using fminsearch
Try Using Cell input
This did not work either, I tried rewriting the function as
function b = three_var(H,c)
v=H{1};
k=H{2};
p=length(v);
b = v(1)^2 + 2.5*sin(v(2)) +sum(k)-prod(v.*v)*c;
end
Then I wrote this
c=2;
H=cell(2,1);
H{1}=[-0.6,-1.2,0.135, 1];
H{2}=[3,2,1];
fun=@(v)three_var(v,c);
b_min = fminsearch(fun, H)
This gave me an error
Error using fminsearch (line 96)
FMINSEARCH accepts inputs only of data type double.
Can someone help me in this regards?