comparison scripts/sparse/pcr.m @ 14363:f3d52523cde1

Use Octave coding conventions in all m-file %!test blocks * wavread.m, acosd.m, acot.m, acotd.m, acoth.m, acsc.m, acscd.m, acsch.m, asec.m, asecd.m, asech.m, asind.m, atand.m, cosd.m, cot.m, cotd.m, coth.m, csc.m, cscd.m, csch.m, sec.m, secd.m, sech.m, sind.m, tand.m, accumarray.m, accumdim.m, bitcmp.m, bitget.m, bitset.m, blkdiag.m, cart2pol.m, cart2sph.m, celldisp.m, chop.m, circshift.m, colon.m, common_size.m, cplxpair.m, cumtrapz.m, curl.m, dblquad.m, deal.m, divergence.m, flipdim.m, fliplr.m, flipud.m, genvarname.m, gradient.m, idivide.m, int2str.m, interp1.m, interp1q.m, interp2.m, interp3.m, interpft.m, interpn.m, isa.m, isdir.m, isequal.m, isequalwithequalnans.m, issquare.m, logspace.m, nargchk.m, narginchk.m, nargoutchk.m, nextpow2.m, nthargout.m, num2str.m, pol2cart.m, polyarea.m, postpad.m, prepad.m, profile.m, profshow.m, quadgk.m, quadv.m, randi.m, rat.m, repmat.m, rot90.m, rotdim.m, shift.m, shiftdim.m, sph2cart.m, structfun.m, trapz.m, triplequad.m, convhull.m, dsearch.m, dsearchn.m, griddata3.m, griddatan.m, rectint.m, tsearchn.m, __makeinfo__.m, doc.m, get_first_help_sentence.m, help.m, type.m, unimplemented.m, which.m, imread.m, imwrite.m, dlmwrite.m, fileread.m, is_valid_file_id.m, strread.m, textread.m, textscan.m, commutation_matrix.m, cond.m, condest.m, cross.m, duplication_matrix.m, expm.m, housh.m, isdefinite.m, ishermitian.m, issymmetric.m, logm.m, normest.m, null.m, onenormest.m, orth.m, planerot.m, qzhess.m, rank.m, rref.m, trace.m, vech.m, ans.m, bincoeff.m, bug_report.m, bzip2.m, comma.m, compare_versions.m, computer.m, edit.m, fileparts.m, fullfile.m, getfield.m, gzip.m, info.m, inputname.m, isappdata.m, isdeployed.m, ismac.m, ispc.m, isunix.m, list_primes.m, ls.m, mexext.m, namelengthmax.m, news.m, orderfields.m, paren.m, recycle.m, rmappdata.m, semicolon.m, setappdata.m, setfield.m, substruct.m, symvar.m, ver.m, version.m, warning_ids.m, xor.m, fminbnd.m, fsolve.m, fzero.m, lsqnonneg.m, optimset.m, pqpnonneg.m, sqp.m, matlabroot.m, __gnuplot_drawnow__.m, __plt_get_axis_arg__.m, ancestor.m, cla.m, clf.m, close.m, colorbar.m, colstyle.m, comet3.m, contourc.m, figure.m, gca.m, gcbf.m, gcbo.m, gcf.m, ginput.m, graphics_toolkit.m, gtext.m, hggroup.m, hist.m, hold.m, isfigure.m, ishghandle.m, ishold.m, isocolors.m, isonormals.m, isosurface.m, isprop.m, legend.m, line.m, loglog.m, loglogerr.m, meshgrid.m, ndgrid.m, newplot.m, orient.m, patch.m, plot3.m, plotyy.m, __print_parse_opts__.m, quiver3.m, refreshdata.m, ribbon.m, semilogx.m, semilogxerr.m, semilogy.m, stem.m, stem3.m, subplot.m, title.m, uigetfile.m, view.m, whitebg.m, compan.m, conv.m, deconv.m, mkpp.m, mpoles.m, pchip.m, poly.m, polyaffine.m, polyder.m, polyfit.m, polygcd.m, polyint.m, polyout.m, polyval.m, polyvalm.m, ppder.m, ppint.m, ppjumps.m, ppval.m, residue.m, roots.m, spline.m, intersect.m, ismember.m, powerset.m, setdiff.m, setxor.m, union.m, unique.m, autoreg_matrix.m, bartlett.m, blackman.m, detrend.m, fftconv.m, fftfilt.m, fftshift.m, freqz.m, hamming.m, hanning.m, ifftshift.m, sinc.m, sinetone.m, sinewave.m, unwrap.m, bicg.m, bicgstab.m, gmres.m, gplot.m, nonzeros.m, pcg.m, pcr.m, spaugment.m, spconvert.m, spdiags.m, speye.m, spfun.m, spones.m, sprand.m, sprandsym.m, spstats.m, spy.m, svds.m, treelayout.m, bessel.m, beta.m, betaln.m, factor.m, factorial.m, isprime.m, lcm.m, legendre.m, nchoosek.m, nthroot.m, perms.m, pow2.m, primes.m, reallog.m, realpow.m, realsqrt.m, hadamard.m, hankel.m, hilb.m, invhilb.m, magic.m, rosser.m, vander.m, __finish__.m, center.m, cloglog.m, corr.m, cov.m, gls.m, histc.m, iqr.m, kendall.m, kurtosis.m, logit.m, mahalanobis.m, mean.m, meansq.m, median.m, mode.m, moment.m, ols.m, ppplot.m, prctile.m, probit.m, quantile.m, range.m, ranks.m, run_count.m, runlength.m, skewness.m, spearman.m, statistics.m, std.m, table.m, var.m, zscore.m, betacdf.m, betainv.m, betapdf.m, betarnd.m, binocdf.m, binoinv.m, binopdf.m, binornd.m, cauchy_cdf.m, cauchy_inv.m, cauchy_pdf.m, cauchy_rnd.m, chi2cdf.m, chi2inv.m, chi2pdf.m, chi2rnd.m, discrete_cdf.m, discrete_inv.m, discrete_pdf.m, discrete_rnd.m, empirical_cdf.m, empirical_inv.m, empirical_pdf.m, empirical_rnd.m, expcdf.m, expinv.m, exppdf.m, exprnd.m, fcdf.m, finv.m, fpdf.m, frnd.m, gamcdf.m, gaminv.m, gampdf.m, gamrnd.m, geocdf.m, geoinv.m, geopdf.m, geornd.m, hygecdf.m, hygeinv.m, hygepdf.m, hygernd.m, kolmogorov_smirnov_cdf.m, laplace_cdf.m, laplace_inv.m, laplace_pdf.m, laplace_rnd.m, logistic_cdf.m, logistic_inv.m, logistic_pdf.m, logistic_rnd.m, logncdf.m, logninv.m, lognpdf.m, lognrnd.m, nbincdf.m, nbininv.m, nbinpdf.m, nbinrnd.m, normcdf.m, norminv.m, normpdf.m, normrnd.m, poisscdf.m, poissinv.m, poisspdf.m, poissrnd.m, stdnormal_cdf.m, stdnormal_inv.m, stdnormal_pdf.m, stdnormal_rnd.m, tcdf.m, tinv.m, tpdf.m, trnd.m, unidcdf.m, unidinv.m, unidpdf.m, unidrnd.m, unifcdf.m, unifinv.m, unifpdf.m, unifrnd.m, wblcdf.m, wblinv.m, wblpdf.m, wblrnd.m, kolmogorov_smirnov_test.m, kruskal_wallis_test.m, base2dec.m, bin2dec.m, blanks.m, cstrcat.m, deblank.m, dec2base.m, dec2bin.m, dec2hex.m, findstr.m, hex2dec.m, index.m, isletter.m, mat2str.m, rindex.m, str2num.m, strcat.m, strjust.m, strmatch.m, strsplit.m, strtok.m, strtrim.m, strtrunc.m, substr.m, validatestring.m, demo.m, example.m, fail.m, speed.m, addtodate.m, asctime.m, clock.m, ctime.m, date.m, datenum.m, datetick.m, datevec.m, eomday.m, etime.m, is_leap_year.m, now.m: Use Octave coding conventions in all m-file %!test blocks
author Rik <octave@nomad.inbox5.com>
date Mon, 13 Feb 2012 07:29:44 -0800
parents ce2b59a6d0e5
children 5d3a684236b0
comparison
equal deleted inserted replaced
14361:8de863b7126b 14363:f3d52523cde1
301 301
302 endfunction 302 endfunction
303 303
304 304
305 %!demo 305 %!demo
306 %! # Simplest usage of PCR (see also 'help pcr') 306 %! # Simplest usage of PCR (see also 'help pcr')
307 %! 307 %!
308 %! N = 20; 308 %! N = 20;
309 %! A = diag (linspace (-3.1,3,N)); b = rand (N,1); 309 %! A = diag (linspace (-3.1,3,N)); b = rand (N,1);
310 %! y = A \ b; # y is the true solution 310 %! y = A \ b; # y is the true solution
311 %! x = pcr (A,b); 311 %! x = pcr (A,b);
312 %! printf ("The solution relative error is %g\n", norm (x-y) / norm (y)); 312 %! printf ("The solution relative error is %g\n", norm (x-y) / norm (y));
313 %! 313 %!
314 %! # You shouldn't be afraid if PCR issues some warning messages in this 314 %! # You shouldn't be afraid if PCR issues some warning messages in this
315 %! # example: watch out in the second example, why it takes N iterations 315 %! # example: watch out in the second example, why it takes N iterations
316 %! # of PCR to converge to (a very accurate, by the way) solution 316 %! # of PCR to converge to (a very accurate, by the way) solution
317 317
318 %!demo 318 %!demo
319 %! # Full output from PCR 319 %! # Full output from PCR
320 %! # We use this output to plot the convergence history 320 %! # We use this output to plot the convergence history
321 %! 321 %!
322 %! N = 20; 322 %! N = 20;
323 %! A = diag (linspace(-3.1,30,N)); b = rand (N,1); 323 %! A = diag (linspace (-3.1,30,N)); b = rand (N,1);
324 %! X = A \ b; # X is the true solution 324 %! X = A \ b; # X is the true solution
325 %! [x, flag, relres, iter, resvec] = pcr (A,b); 325 %! [x, flag, relres, iter, resvec] = pcr (A,b);
326 %! printf ("The solution relative error is %g\n", norm (x-X) / norm (X)); 326 %! printf ("The solution relative error is %g\n", norm (x-X) / norm (X));
327 %! clf; 327 %! clf;
328 %! title ("Convergence history"); 328 %! title ("Convergence history");
329 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||/||b||)"); 329 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||/||b||)");
330 %! semilogy ([0:iter], resvec/resvec(1), "o-g;relative residual;"); 330 %! semilogy ([0:iter], resvec/resvec(1), "o-g;relative residual;");
331 331
332 %!demo 332 %!demo
333 %! # Full output from PCR 333 %! # Full output from PCR
334 %! # We use indefinite matrix based on the Hilbert matrix, with one 334 %! # We use indefinite matrix based on the Hilbert matrix, with one
335 %! # strongly negative eigenvalue 335 %! # strongly negative eigenvalue
336 %! # Hilbert matrix is extremely ill conditioned, so is ours, 336 %! # Hilbert matrix is extremely ill conditioned, so is ours,
337 %! # and that's why PCR WILL have problems 337 %! # and that's why PCR WILL have problems
338 %! 338 %!
339 %! N = 10; 339 %! N = 10;
340 %! A = hilb (N); A(1,1) = -A(1,1); b = rand (N,1); 340 %! A = hilb (N); A(1,1) = -A(1,1); b = rand (N,1);
341 %! X = A \ b; # X is the true solution 341 %! X = A \ b; # X is the true solution
342 %! printf ("Condition number of A is %g\n", cond (A)); 342 %! printf ("Condition number of A is %g\n", cond (A));
343 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],200); 343 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],200);
344 %! if (flag == 3) 344 %! if (flag == 3)
345 %! printf ("PCR breakdown. System matrix is [close to] singular\n"); 345 %! printf ("PCR breakdown. System matrix is [close to] singular\n");
346 %! end 346 %! end
347 %! clf; 347 %! clf;
348 %! title ("Convergence history"); 348 %! title ("Convergence history");
349 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)"); 349 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)");
350 %! semilogy ([0:iter], resvec, "o-g;absolute residual;"); 350 %! semilogy ([0:iter], resvec, "o-g;absolute residual;");
351 351
352 %!demo 352 %!demo
353 %! # Full output from PCR 353 %! # Full output from PCR
354 %! # We use an indefinite matrix based on the 1-D Laplacian matrix for A, 354 %! # We use an indefinite matrix based on the 1-D Laplacian matrix for A,
355 %! # and here we have cond(A) = O(N^2) 355 %! # and here we have cond(A) = O(N^2)
356 %! # That's the reason we need some preconditioner; here we take 356 %! # That's the reason we need some preconditioner; here we take
357 %! # a very simple and not powerful Jacobi preconditioner, 357 %! # a very simple and not powerful Jacobi preconditioner,
358 %! # which is the diagonal of A 358 %! # which is the diagonal of A
359 %! 359 %!
360 %! # Note that we use here indefinite preconditioners! 360 %! # Note that we use here indefinite preconditioners!
361 %! 361 %!
362 %! N = 100; 362 %! N = 100;
363 %! A = zeros (N,N); 363 %! A = zeros (N,N);
364 %! for i=1:N-1 # form 1-D Laplacian matrix 364 %! for i=1:N-1 # form 1-D Laplacian matrix
365 %! A(i:i+1,i:i+1) = [2 -1; -1 2]; 365 %! A(i:i+1,i:i+1) = [2 -1; -1 2];
366 %! endfor 366 %! endfor
367 %! A = [A, zeros(size(A)); zeros(size(A)), -A]; 367 %! A = [A, zeros(size(A)); zeros(size(A)), -A];
368 %! b = rand (2*N,1); 368 %! b = rand (2*N,1);
369 %! X = A \ b; # X is the true solution 369 %! X = A \ b; # X is the true solution
370 %! maxit = 80; 370 %! maxit = 80;
371 %! printf ("System condition number is %g\n", cond (A)); 371 %! printf ("System condition number is %g\n", cond (A));
372 %! # No preconditioner: the convergence is very slow! 372 %! # No preconditioner: the convergence is very slow!
373 %! 373 %!
374 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],maxit); 374 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],maxit);
375 %! clf; 375 %! clf;
376 %! title ("Convergence history"); 376 %! title ("Convergence history");
377 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)"); 377 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)");
378 %! semilogy ([0:iter], resvec, "o-g;NO preconditioning: absolute residual;"); 378 %! semilogy ([0:iter], resvec, "o-g;NO preconditioning: absolute residual;");
379 %! 379 %!
380 %! pause (1); 380 %! pause (1);
381 %! # Test Jacobi preconditioner: it will not help much!!! 381 %! # Test Jacobi preconditioner: it will not help much!!!
382 %! 382 %!
383 %! M = diag (diag (A)); # Jacobi preconditioner 383 %! M = diag (diag (A)); # Jacobi preconditioner
384 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],maxit,M); 384 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],maxit,M);
385 %! hold on; 385 %! hold on;
386 %! semilogy ([0:iter],resvec,"o-r;JACOBI preconditioner: absolute residual;"); 386 %! semilogy ([0:iter],resvec,"o-r;JACOBI preconditioner: absolute residual;");
387 %! 387 %!
388 %! pause (1); 388 %! pause (1);
389 %! # Test nonoverlapping block Jacobi preconditioner: this one should give 389 %! # Test nonoverlapping block Jacobi preconditioner: this one should give
390 %! # some convergence speedup! 390 %! # some convergence speedup!
391 %! 391 %!
392 %! M = zeros (N,N); k = 4; 392 %! M = zeros (N,N); k = 4;
393 %! for i=1:k:N # get k x k diagonal blocks of A 393 %! for i=1:k:N # get k x k diagonal blocks of A
394 %! M(i:i+k-1,i:i+k-1) = A(i:i+k-1,i:i+k-1); 394 %! M(i:i+k-1,i:i+k-1) = A(i:i+k-1,i:i+k-1);
395 %! endfor 395 %! endfor
396 %! M = [M, zeros(size (M)); zeros(size(M)), -M]; 396 %! M = [M, zeros(size (M)); zeros(size(M)), -M];
397 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],maxit,M); 397 %! [x, flag, relres, iter, resvec] = pcr (A,b,[],maxit,M);
398 %! semilogy ([0:iter], resvec, "o-b;BLOCK JACOBI preconditioner: absolute residual;"); 398 %! semilogy ([0:iter], resvec, "o-b;BLOCK JACOBI preconditioner: absolute residual;");
399 %! hold off; 399 %! hold off;
400 400
401 %!test 401 %!test
402 %! # solve small indefinite diagonal system 402 %! # solve small indefinite diagonal system
403 %! 403 %!
404 %! N = 10; 404 %! N = 10;