How do I plot the value at each iteration?

My code below is to compute the minimum value and vector using the conjugate gradient method. How do i plot the value at each iteration? Thanks.
%this program computes the optimal solution of a nonlinear programming
%using the conjugate-gradient method
%xstar - the global optimal solution
%value - optimal solution
function[xstar, minv] = conj_grad(Q,x,q,c,n,tol)
tic;
g0 = (0.5*Q'*x) + (0.5*Q*x) + q; %g is the gradient
d = -g0;
b = (-g0) + Q*x;
g_old = g0;
for i = 1:n
alpha = -(g_old'*d)/(d'*Q*d)
x = x + (alpha*d)
g_new = (Q*x) - b
beta = (g_new'*Q*d)/(d'*Q*d)
res = norm(g_new) %res = residual which is the min distance
value = (0.5*x'*Q*x) +(q'*x) + c
if res < tol
xstar = x
minv = value
fprintf("The minimum value is %d and it converged after %d iterations \n", value, i)
break
end
d = (-g_new) + (beta*d)
g_old = g_new
end
toc
end

 採用された回答

Cris LaPierre
Cris LaPierre 2020 年 3 月 20 日

0 投票

I shared a simple example in a similar post recently. That might help.

7 件のコメント

SA
SA 2020 年 3 月 20 日
Hi Cris,
I went through the code but I don't need a need figure for each itereation. I just want one figure that has each itereation and its corresponding value
Cris LaPierre
Cris LaPierre 2020 年 3 月 20 日
There were two options shown in that post. The first one does what you want. The main difference here is that example used a while loop. You can replace it with a for loop instead.
SA
SA 2020 年 3 月 20 日
Hi Cris,
I don't really understand the code so I am unable to implement it in mine
Cris LaPierre
Cris LaPierre 2020 年 3 月 20 日
Fair enough. It's unclear to me which value(s) you want plotted. If you can write the plot command you want, I can help you insert it into your code.
SA
SA 2020 年 3 月 21 日
At each itreation (i) a res(residue) and value is obtained. I want to plot each i against its corresponding value and each i against its corresponding res, that is
plot(i,value)
plot(i,res).
thanks
Cris LaPierre
Cris LaPierre 2020 年 3 月 21 日
編集済み: Cris LaPierre 2020 年 3 月 21 日
This assumes res and value can be plotted on the same axes.
%this program computes the optimal solution of a nonlinear programming
%using the conjugate-gradient method
%xstar - the global optimal solution
%value - optimal solution
function[xstar, minv] = conj_grad(Q,x,q,c,n,tol)
tic;
g0 = (0.5*Q'*x) + (0.5*Q*x) + q; %g is the gradient
d = -g0;
b = (-g0) + Q*x;
g_old = g0;
for i = 1:n
alpha = -(g_old'*d)/(d'*Q*d)
x = x + (alpha*d)
g_new = (Q*x) - b
beta = (g_new'*Q*d)/(d'*Q*d)
res = norm(g_new) %res = residual which is the min distance
value = (0.5*x'*Q*x) +(q'*x) + c
% Plot res, value
plot(i,res,'ro',i,value,'gs')
hold on
if res < tol
xstar = x
minv = value
fprintf("The minimum value is %d and it converged after %d iterations \n", value, i)
break
end
d = (-g_new) + (beta*d)
g_old = g_new
end
hold off
toc
end
SA
SA 2020 年 3 月 21 日
It worked Cris. Thanks so much for the help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

SA
2020 年 3 月 20 日

コメント済み:

SA
2020 年 3 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by