Gradient of a cell array with optimizer
1 view (last 30 days)
Show older comments
Hi everyone, I have a function with various variables z, as can be seen in the example below. I would like to take the gradient in x,y (coordinates) and then use an optimizer to get 1000 z values to minimize the function. The problem is that gradient function does not work with cell arrays. If I try storing the function in symbolic matrix then simulated annealing doesn't work cause only there is only one z.
Cells approach example:
A=rand(10^3,1);
for k2=1:10^3
a{k2}=@(z) z*A(k2);
end
%preparing the array for the gradient
l=1;
for k3=1:10
for k4=1:10
for k5=1:10
q2(k3).p{k4,k5}=@(z) a{l}(z);
l=l+1;
end
end
end
clear k1 k2 k3 k4 k5 l
% now I want to do what I have done with the matrix above but I have functions this returns 1 value so doesn't work
for k1=1:10
[part_x2{k1}.p,part_y2{k1}.p]=@(z) (cellfun(@gradient,q2(k1)));
end
%this doesn't work either
for k1=1:10^3
[part_x2{k1}.p,part_y2{k1}.p]=arrayfun(@(z) gradient(q2(k1)));
end
Symbolic matrix approach example, this does work but then the optimizer doesn't work:
A=rand(2,2);
syms x
f = sin(x) * A;
for k1=1:2
for k2=1:2
F(k1,k2)=gradient(f(k1,k2),x);
end
end
% now the optimizer doesn't work
fun=F(1,:);
z0 = zeros(length(A(1,:)),1);
LB(1:length(A(1,:)),1) = -2; % LB - lower bound vector or array, must be the same size as z0
UB(1:length(A(1,:)),1)= 2; % UB - upper bound vector or array, must be the same size as z0
[z_ref,P_T]=simulannealbnd(fun,z0,LB,UB); %simulated annealing
0 Comments
Answers (0)
See Also
Categories
Find more on Simulated Annealing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!