-
Notifications
You must be signed in to change notification settings - Fork 0
/
getfg.m
32 lines (27 loc) · 951 Bytes
/
getfg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% getfg.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input
% x % point
% prob % ingredients of test problem
%
% output
% f % function value
% res % gradient vector
%
% [f,res] = getfg(x,prob) compute f and res at x
% g = grtfg(res) computes the gradient at x
%
function [f,res] = getfg(x,prob)
A=prob.A; b=prob.b;
if nargout==2
res = A*x-b; f = 0.5*(res'*res);
elseif nargout==1 % x contains res, f returns g
f = A'*x;
else
error('at least one output needs')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%