Multiple plot on same graph
조회 수: 3 (최근 30일)
이전 댓글 표시
I would like to have these 4 plots on the same graph sheet for easy comparison. Plotting the individual plot works but I am not sure what is the reason why all the 4 plots cannot be plotted at the same time. Relying on benevolent members for assitance.
rng default
n = 30;
% generate the A matrix
D = delsq(numgrid('L',n)); %same as matrix A
bGrid = ones(length(D),1); %same as RHS b vector
M = diag(diag(D));
tol = 1e-8;
maxit = 100;
P = inv(M);
L = ichol(M);
Q = ichol(M,struct('michol','on'));
% pcg
[x0,f0,r0,it0,rv0] = pcg(D,bGrid); %no precond
[x1,f1,r1,it1,rv1] = pcg(D,bGrid,tol,maxit,P); % inv
[x2,f2,r2,it2,rv2] = pcg(D,bGrid,tol,maxit,L,L'); % ichol
[x3,f3,r3,it3,rv3] = pcg(D,bGrid,tol,maxit,Q,Q'); % modified ichol
% plots on same graph
semilogy(0:length(rv0)-1,rv0/norm(bGrid),'-o')
hold on
semilogy(0:length(rv1)-1,rv1/norm(bGrid),'-o')
semilogy(0:length(rv2)-1,rv2/norm(bGrid),'-o')
semilogy(0:length(rv3)-1,rv3/norm(bGrid),'-o')
yline(tol,'r--');
legend('No Preconditioner', 'inv(M)', 'ichol(M)', 'michol(M)', 'Tolerance', 'Location','southwest')
title('Conjugant Gredient Algorithm');
legend('boxoff')
댓글 수: 0
채택된 답변
Walter Roberson
2021년 12월 3일
rng default
n = 30;
% generate the A matrix
D = delsq(numgrid('L',n)); %same as matrix A
bGrid = ones(length(D),1); %same as RHS b vector
M = diag(diag(D));
tol = 1e-8;
maxit = 100;
P = inv(M);
L = ichol(M);
Q = ichol(M,struct('michol','on'));
% pcg
[x0,f0,r0,it0,rv0] = pcg(D,bGrid); %no precond
[x1,f1,r1,it1,rv1] = pcg(D,bGrid,tol,maxit,P); % inv
[x2,f2,r2,it2,rv2] = pcg(D,bGrid,tol,maxit,L,L'); % ichol
[x3,f3,r3,it3,rv3] = pcg(D,bGrid,tol,maxit,Q,Q'); % modified ichol
% plots on same graph
semilogy(0:length(rv0)-1,rv0/norm(bGrid),'-or')
hold on
semilogy(0:length(rv1)-1,rv1/norm(bGrid),'--.g')
semilogy(0:length(rv2)-1,rv2/norm(bGrid),':^b')
semilogy(0:length(rv3)-1,rv3/norm(bGrid),'+k')
yline(tol,'r--');
legend('No Preconditioner', 'inv(M)', 'ichol(M)', 'michol(M)', 'Tolerance', 'Location','southwest')
title('Conjugant Gredient Algorithm');
legend('boxoff')
min(rv0),max(rv0)
min(rv1),max(rv1)
min(rv2),max(rv2)
min(rv3),max(rv3)
You are asking to solve the same system of linear equations with different preconditioners, but you are expecting that the differences between the results will be visible on the plot.
There is a notable difference on the first point of rv0 versus the remainder, but on the scale of the plot you cannot tell by the plot.
댓글 수: 4
Walter Roberson
2021년 12월 3일
M = diag(diag(D));
That is a sparse diagonal matrix whose diagonal is the constant 4.
L = ichol(M);
That is a sparse diagonal matrix whose diagonal is the constant 2.
Your arrays are "boring" -- the hints you are providing through the additional parameters are adding essentially no usable new information, so the outputs are all the same (except being different sizes in some cases.)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!