Reputation: 21
Given a function f:R^600 -> R f(x)=0.5*norm(Ax-b)^2
, where A
is a 400x600 matrix, b
is 400x1, and both of them are given.
How can I get the gradient(f)
at some given x0
in MATLAB?
m=400
n=600
A=randn(m,n)
b=randn(m,1)
syms x
f= 0.5*norm(A*x-b)^2
gradient(f,x)
However it does not work because it seems it regard x
as scalar, not a vector.
Upvotes: 2
Views: 848
Reputation: 1663
Given the function:
the gradient is just:
Check this for further details.
Therefore, your MATLAB implementation is:
g = A'*(A*x-b);
where g is the gradient of your function for a given x.
Note that A, b and x are numerical, not symbolic.
Proof
Symbolic approach
You can use the following code:
N = 3;
A = [1 0 1;
2 1 -1;
3 1 0];
b = [1;
2;
3];
x = sym('x', [N, 1]);
f = 0.5*norm(A*x-b)^2;
g = sym('g', [N, 1]); % g is the gradient of f
for i=1:N
g(i) = diff(f,x(i));
end
Upvotes: 1