Computing Partial derivates of anonymous functions comprised of sum of standard functions
Hi there,
I am implementing a conjugate gradient based optimisation algorithm, and to achieve this i need to calculate the grad of the input function. I currently pass in an anonymous function, which is comprised as the sum of other anonymous functions, of which those are comprised of standard matlab functions. Here is how the objective function is defined
% one constraint for example – all 6 are similar form
c2 = @(x) mat.minSF_yield – mat.yield …
/ axial_stress(mat.F, mat.E, x(1), x(2));
transf_obj = @(x) obj(x) – r * (1 / (c1(x) + c2(x) + c3(x) + c4(x) + c5(x) + c6(x)));
the partial derivative function calculates the partial derivative anon functions and adds them into a cell. This allows the partials to be calculated before the optimisation loop saving computation time
function [grad_cell] = partials(func, x)
% Get dimensionality of inputs
n = numel(x);
% Convert the anonymous function to a symbolic expression
syms a [1 n]
funcSym = func(a);
% Initialize the array to hold partial derivative functions
grad_vect = zeros(1, n);
grad_cell = cell(1,n);
% Compute partial derivatives
for i = 1:n
% Compute symbolic partial derivative with respect to the i-th variable
partialDerivSym = diff(funcSym, a(i));
% Convert symbolic partial derivative back to an anon function
grad = matlabFunction(partialDerivSym, ‘Vars’, {a});
% Add anon function to cell
grad_cell{i} = grad;
end
end
To evalute the gradient at a specific point the following function is used
function [nabla] = compute_grad(cell, x)
nabla = zeros(1,numel(x))
for i =1:numel(x)
nabla(i) = cell{i}(x);
end
end
When evaluting any partial, for some values such as x = [14,3] something within the feasable domain, an empty array is returned. And for values such as x=[7.9,7], a value close to the results i was getting from using fmincon an error of the following is produced.
Error using diff
Difference order N must be a positive integer scalar.
Error in sym/matlabFunction>@(in1)(in1(:,1).*pi.*(3.0./2.5e+…
My current theory is that the conversion to and from symbolic variables to compute the derivative is not being achieved successfuly given the combination of anon functions and locally defined functions. I have tested the partials function with simple functions directly defined and it appears to work. This program is split across 3 files, the first which defines variables and calls the conj-grad m-file. This file then calls the partials.m file which is just the partials function.
%% Variable Definition …
…
%% Call ‘Conj_grad.m’
%% Call ‘partials.m’
%% Returns cell
%% compute at specific point, indexing from cell using compute_grad function
%% Error
Any help is much appreciated.Hi there,
I am implementing a conjugate gradient based optimisation algorithm, and to achieve this i need to calculate the grad of the input function. I currently pass in an anonymous function, which is comprised as the sum of other anonymous functions, of which those are comprised of standard matlab functions. Here is how the objective function is defined
% one constraint for example – all 6 are similar form
c2 = @(x) mat.minSF_yield – mat.yield …
/ axial_stress(mat.F, mat.E, x(1), x(2));
transf_obj = @(x) obj(x) – r * (1 / (c1(x) + c2(x) + c3(x) + c4(x) + c5(x) + c6(x)));
the partial derivative function calculates the partial derivative anon functions and adds them into a cell. This allows the partials to be calculated before the optimisation loop saving computation time
function [grad_cell] = partials(func, x)
% Get dimensionality of inputs
n = numel(x);
% Convert the anonymous function to a symbolic expression
syms a [1 n]
funcSym = func(a);
% Initialize the array to hold partial derivative functions
grad_vect = zeros(1, n);
grad_cell = cell(1,n);
% Compute partial derivatives
for i = 1:n
% Compute symbolic partial derivative with respect to the i-th variable
partialDerivSym = diff(funcSym, a(i));
% Convert symbolic partial derivative back to an anon function
grad = matlabFunction(partialDerivSym, ‘Vars’, {a});
% Add anon function to cell
grad_cell{i} = grad;
end
end
To evalute the gradient at a specific point the following function is used
function [nabla] = compute_grad(cell, x)
nabla = zeros(1,numel(x))
for i =1:numel(x)
nabla(i) = cell{i}(x);
end
end
When evaluting any partial, for some values such as x = [14,3] something within the feasable domain, an empty array is returned. And for values such as x=[7.9,7], a value close to the results i was getting from using fmincon an error of the following is produced.
Error using diff
Difference order N must be a positive integer scalar.
Error in sym/matlabFunction>@(in1)(in1(:,1).*pi.*(3.0./2.5e+…
My current theory is that the conversion to and from symbolic variables to compute the derivative is not being achieved successfuly given the combination of anon functions and locally defined functions. I have tested the partials function with simple functions directly defined and it appears to work. This program is split across 3 files, the first which defines variables and calls the conj-grad m-file. This file then calls the partials.m file which is just the partials function.
%% Variable Definition …
…
%% Call ‘Conj_grad.m’
%% Call ‘partials.m’
%% Returns cell
%% compute at specific point, indexing from cell using compute_grad function
%% Error
Any help is much appreciated. Hi there,
I am implementing a conjugate gradient based optimisation algorithm, and to achieve this i need to calculate the grad of the input function. I currently pass in an anonymous function, which is comprised as the sum of other anonymous functions, of which those are comprised of standard matlab functions. Here is how the objective function is defined
% one constraint for example – all 6 are similar form
c2 = @(x) mat.minSF_yield – mat.yield …
/ axial_stress(mat.F, mat.E, x(1), x(2));
transf_obj = @(x) obj(x) – r * (1 / (c1(x) + c2(x) + c3(x) + c4(x) + c5(x) + c6(x)));
the partial derivative function calculates the partial derivative anon functions and adds them into a cell. This allows the partials to be calculated before the optimisation loop saving computation time
function [grad_cell] = partials(func, x)
% Get dimensionality of inputs
n = numel(x);
% Convert the anonymous function to a symbolic expression
syms a [1 n]
funcSym = func(a);
% Initialize the array to hold partial derivative functions
grad_vect = zeros(1, n);
grad_cell = cell(1,n);
% Compute partial derivatives
for i = 1:n
% Compute symbolic partial derivative with respect to the i-th variable
partialDerivSym = diff(funcSym, a(i));
% Convert symbolic partial derivative back to an anon function
grad = matlabFunction(partialDerivSym, ‘Vars’, {a});
% Add anon function to cell
grad_cell{i} = grad;
end
end
To evalute the gradient at a specific point the following function is used
function [nabla] = compute_grad(cell, x)
nabla = zeros(1,numel(x))
for i =1:numel(x)
nabla(i) = cell{i}(x);
end
end
When evaluting any partial, for some values such as x = [14,3] something within the feasable domain, an empty array is returned. And for values such as x=[7.9,7], a value close to the results i was getting from using fmincon an error of the following is produced.
Error using diff
Difference order N must be a positive integer scalar.
Error in sym/matlabFunction>@(in1)(in1(:,1).*pi.*(3.0./2.5e+…
My current theory is that the conversion to and from symbolic variables to compute the derivative is not being achieved successfuly given the combination of anon functions and locally defined functions. I have tested the partials function with simple functions directly defined and it appears to work. This program is split across 3 files, the first which defines variables and calls the conj-grad m-file. This file then calls the partials.m file which is just the partials function.
%% Variable Definition …
…
%% Call ‘Conj_grad.m’
%% Call ‘partials.m’
%% Returns cell
%% compute at specific point, indexing from cell using compute_grad function
%% Error
Any help is much appreciated. anonymous functions, partial derivatives MATLAB Answers — New Questions