在 Matlab 中使用匿名 fnc 时如何将值存储到数组中以绘制它们
How to store values into an array to plot them when using an anonymous fnc in Matlab
我在绘制迭代次数与成本函数 (f_star1) 时遇到问题。我无法将迭代(niter)和在每次迭代中评估的函数保存到数组中。下面是我的代码。 while 循环中注释中指定的行是我卡住的地方!
% define initial guess
x = [1 1]';
% define parameters
h = 1; % initial step size
beta = 0.7; % Armijo's Rule: Beta = (0,1)
alpha = 0.2; % Armijo's Rule: Alpha = (0,.5)
grad = 1; % initialize gradient
epsilon = 1e-2;
niter = 0; % iterations counter
f = @costf; % objective fcn assigned to variable f
i = 1;
n=0;
% Loop
while norm(grad) > epsilon
grad = gradient(x(:,end));
while f(x(:,end)-h*grad) > (f(x(:,end))-alpha*h*grad) %backtracking line search
h = h*beta;
end
x(:,end+1) = x(:,end) - h*grad;
%This is the part I am stuck!!!
x1G = x(1,:)';
x2G = x(2,:)';
x_star1 = [(x(1,:));(x(2,:))]';
f_star1(i,:) = costf(x_star1(i,:));
% figure(1), plot(iterations,f_star1,'*')
niter= niter+1;
end
% plot(f_star1,niter,'*')
%Cost Function
function f = costf(x)
f = exp(x(1)+3*x(2)-0.1) + exp(x(1)-3*x(2)-0.1) + exp(-x(1)-0.1);
end
% find gradient with central difference
function df = gradient(x0)
f = @costf; % assign cost fcn function to variable f
h_step = .01; % perturbation step
dx1 = (f(x0+[h_step;0])-f(x0-[h_step;0]))/(2*h_step); %slope
dx2 = (f(x0+[0;h_step])-f(x0-[0;h_step]))/(2*h_step);
df = [dx1;dx2];
end
首先,要使用 plot(niter, f_star1)
函数,niter
和 f_star1
应该是在您的情况下长度相等的两个向量。问题是 while 循环中的 i
。您需要在迭代中增加i
。
% define initial guess
x = [1 1]';
% define parameters
h = 1; % initial step size
beta = 0.7; % Armijo's Rule: Beta = (0,1)
alpha = 0.2; % Armijo's Rule: Alpha = (0,.5)
grad = 1; % initialize gradient
epsilon = 1e-2;
niter = 0; % iterations counter
f = @costf; % objective fcn assigned to variable f
i = 1;
n=0;
% Loop
while norm(grad) > epsilon
grad = gradient(x(:,end));
while f(x(:,end)-h*grad) > (f(x(:,end))-alpha*h*grad) %backtracking line search
h = h*beta;
end
x(:,end+1) = x(:,end) - h*grad;
%This is the part I am stuck!!!
x1G = x(1,:)';
x2G = x(2,:)';
x_star1 = [(x(1,:));(x(2,:))]';
f_star1(i,:) = costf(x_star1(i,:));
niter(i,:)= i-1;
i = i+ 1;
end
plot(niter,f_star1,'*-')
title('f\_star1 vs niter');
xlabel('niter');
ylabel('f\_star1');
%Cost Function
function f = costf(x)
f = exp(x(1)+3*x(2)-0.1) + exp(x(1)-3*x(2)-0.1) + exp(-x(1)-0.1);
end
% find gradient with central difference
function df = gradient(x0)
f = @costf; % assign cost fcn function to variable f
h_step = .01; % perturbation step
dx1 = (f(x0+[h_step;0])-f(x0-[h_step;0]))/(2*h_step); %slope
dx2 = (f(x0+[0;h_step])-f(x0-[0;h_step]))/(2*h_step);
df = [dx1;dx2];
end
输出:
我在绘制迭代次数与成本函数 (f_star1) 时遇到问题。我无法将迭代(niter)和在每次迭代中评估的函数保存到数组中。下面是我的代码。 while 循环中注释中指定的行是我卡住的地方!
% define initial guess
x = [1 1]';
% define parameters
h = 1; % initial step size
beta = 0.7; % Armijo's Rule: Beta = (0,1)
alpha = 0.2; % Armijo's Rule: Alpha = (0,.5)
grad = 1; % initialize gradient
epsilon = 1e-2;
niter = 0; % iterations counter
f = @costf; % objective fcn assigned to variable f
i = 1;
n=0;
% Loop
while norm(grad) > epsilon
grad = gradient(x(:,end));
while f(x(:,end)-h*grad) > (f(x(:,end))-alpha*h*grad) %backtracking line search
h = h*beta;
end
x(:,end+1) = x(:,end) - h*grad;
%This is the part I am stuck!!!
x1G = x(1,:)';
x2G = x(2,:)';
x_star1 = [(x(1,:));(x(2,:))]';
f_star1(i,:) = costf(x_star1(i,:));
% figure(1), plot(iterations,f_star1,'*')
niter= niter+1;
end
% plot(f_star1,niter,'*')
%Cost Function
function f = costf(x)
f = exp(x(1)+3*x(2)-0.1) + exp(x(1)-3*x(2)-0.1) + exp(-x(1)-0.1);
end
% find gradient with central difference
function df = gradient(x0)
f = @costf; % assign cost fcn function to variable f
h_step = .01; % perturbation step
dx1 = (f(x0+[h_step;0])-f(x0-[h_step;0]))/(2*h_step); %slope
dx2 = (f(x0+[0;h_step])-f(x0-[0;h_step]))/(2*h_step);
df = [dx1;dx2];
end
首先,要使用 plot(niter, f_star1)
函数,niter
和 f_star1
应该是在您的情况下长度相等的两个向量。问题是 while 循环中的 i
。您需要在迭代中增加i
。
% define initial guess
x = [1 1]';
% define parameters
h = 1; % initial step size
beta = 0.7; % Armijo's Rule: Beta = (0,1)
alpha = 0.2; % Armijo's Rule: Alpha = (0,.5)
grad = 1; % initialize gradient
epsilon = 1e-2;
niter = 0; % iterations counter
f = @costf; % objective fcn assigned to variable f
i = 1;
n=0;
% Loop
while norm(grad) > epsilon
grad = gradient(x(:,end));
while f(x(:,end)-h*grad) > (f(x(:,end))-alpha*h*grad) %backtracking line search
h = h*beta;
end
x(:,end+1) = x(:,end) - h*grad;
%This is the part I am stuck!!!
x1G = x(1,:)';
x2G = x(2,:)';
x_star1 = [(x(1,:));(x(2,:))]';
f_star1(i,:) = costf(x_star1(i,:));
niter(i,:)= i-1;
i = i+ 1;
end
plot(niter,f_star1,'*-')
title('f\_star1 vs niter');
xlabel('niter');
ylabel('f\_star1');
%Cost Function
function f = costf(x)
f = exp(x(1)+3*x(2)-0.1) + exp(x(1)-3*x(2)-0.1) + exp(-x(1)-0.1);
end
% find gradient with central difference
function df = gradient(x0)
f = @costf; % assign cost fcn function to variable f
h_step = .01; % perturbation step
dx1 = (f(x0+[h_step;0])-f(x0-[h_step;0]))/(2*h_step); %slope
dx2 = (f(x0+[0;h_step])-f(x0-[0;h_step]))/(2*h_step);
df = [dx1;dx2];
end
输出: