comparison scripts/sparse/pcg.m @ 21633:dcf8922b724b

Deprecate printf, puts, and scanf. Make fputs a M-file. * libinterp/corefcn/file-io.cc: extracted printf, puts, and scanf to M-files. * scripts/deprecated/printf.m: new function, equivalent to version file-io.cc. * scripts/deprecated/puts.m: new function, equivalent to version file-io.cc. * scripts/deprecated/scanf.m: new function, equivalent to version file-io.cc. * scripts/deprecated/module.mk: added printf, puts, and scanf. * scripts/io/fputs.m: new function, equivalent to version file-io.cc. * scripts/io/module.mk: added fputs. * doc/interpreter/io.txi: adapted documentation to current implementation. * NEWS: announced changes. * doc/interpreter/basics.txi: replaced printf by fprintf. * doc/interpreter/stmt.txi: replaced printf by fprintf. * doc/interpreter/var.txi: replaced printf by fprintf. * doc/refcard/refcard.tex: removed printf and scanf. * examples/code/@FIRfilter/display.m: replaced printf by fprintf. * examples/code/@polynomial/display.m: replaced printf by fprintf. * scripts/@ftp/display.m: replaced printf by fprintf. * scripts/general/inputParser.m: replaced printf by fprintf. * scripts/general/methods.m: replaced printf by fprintf. * scripts/general/profexplore.m: replaced printf by fprintf. * scripts/general/profshow.m: replaced printf by fprintf. * scripts/help/help.m: replaced puts by fputs and printf by fprintf. * scripts/help/lookfor.m: replaced puts by fputs and printf by fprintf. * scripts/help/which.m: replaced printf by fprintf. * scripts/image/imformats.m: replaced printf by fprintf. * scripts/io/beep.m: replaced puts by fputs. * scripts/io/textread.m: replaced printf by fprintf. * scripts/java/javaclasspath.m: replaced printf by fprintf. * scripts/java/javamem.m: replaced printf by fprintf. * scripts/miscellaneous/dir.m: replaced printf by fprintf. * scripts/miscellaneous/dos.m: replaced printf by fprintf. * scripts/miscellaneous/fact.m: replaced printf by fprintf. * scripts/miscellaneous/info.m: replaced printf by fprintf. * scripts/miscellaneous/license.m: replaced printf by fprintf. * scripts/miscellaneous/ls.m: replaced puts by fputs. * scripts/miscellaneous/menu.m: replaced printf by fprintf. * scripts/miscellaneous/mkoctfile.m: replaced printf by fprintf. * scripts/miscellaneous/private/display_info_file.m: * scripts/miscellaneous/unix.m: replaced printf by fprintf. * scripts/miscellaneous/ver.m: replaced printf by fprintf. * scripts/miscellaneous/what.m: replaced printf by fprintf. * scripts/ode/ode23.m: replaced printf by fprintf. * scripts/ode/ode45.m: replaced printf by fprintf. * scripts/optimization/fminbnd.m: replaced printf by fprintf. * scripts/optimization/optimset.m: replaced puts by fputs and printf by fprintf. * scripts/optimization/sqp.m: replaced printf by fprintf. * scripts/pkg/pkg.m: replaced printf by fprintf. * scripts/pkg/private/describe.m: replaced printf by fprintf. * scripts/pkg/private/install.m: replaced printf by fprintf. * scripts/pkg/private/installed_packages.m: replaced printf by fprintf. * scripts/pkg/private/list_forge_packages.m: replaced puts by fputs and printf by fprintf. * scripts/pkg/private/rebuild.m: replaced printf by fprintf. * scripts/plot/util/__gnuplot_drawnow__.m: replaced puts by fputs and printf by fprintf. * scripts/signal/stft.m: replaced printf by fprintf. * scripts/sparse/bicg.m: replaced printf by fprintf. * scripts/sparse/bicgstab.m: replaced printf by fprintf. * scripts/sparse/cgs.m: replaced printf by fprintf. * scripts/sparse/pcg.m: replaced printf by fprintf. * scripts/sparse/pcr.m: replaced printf by fprintf. * scripts/sparse/qmr.m: replaced printf by fprintf. * scripts/statistics/models/logistic_regression.m: replaced printf by fprintf. * scripts/statistics/tests/anova.m: replaced printf by fprintf. * scripts/statistics/tests/bartlett_test.m: replaced printf by fprintf. * scripts/statistics/tests/chisquare_test_homogeneity.m: replaced printf by fprintf. * scripts/statistics/tests/chisquare_test_independence.m: replaced printf by fprintf. * scripts/statistics/tests/cor_test.m: replaced printf by fprintf. * scripts/statistics/tests/f_test_regression.m: replaced printf by fprintf. * scripts/statistics/tests/hotelling_test.m: replaced printf by fprintf. * scripts/statistics/tests/hotelling_test_2.m: replaced printf by fprintf. * scripts/statistics/tests/kolmogorov_smirnov_test.m: replaced printf by fprintf. * scripts/statistics/tests/kolmogorov_smirnov_test_2.m: replaced printf by fprintf. * scripts/statistics/tests/kruskal_wallis_test.m: replaced printf by fprintf. * scripts/statistics/tests/manova.m: replaced printf by fprintf. * scripts/statistics/tests/mcnemar_test.m: replaced printf by fprintf. * scripts/statistics/tests/prop_test_2.m: replaced printf by fprintf. * scripts/statistics/tests/run_test.m: replaced printf by fprintf. * scripts/statistics/tests/sign_test.m: replaced printf by fprintf. * scripts/statistics/tests/t_test.m: replaced printf by fprintf. * scripts/statistics/tests/t_test_2.m: replaced printf by fprintf. * scripts/statistics/tests/t_test_regression.m: replaced printf by fprintf. * scripts/statistics/tests/u_test.m: replaced printf by fprintf. * scripts/statistics/tests/var_test.m: replaced printf by fprintf. * scripts/statistics/tests/welch_test.m: replaced printf by fprintf. * scripts/statistics/tests/wilcoxon_test.m: replaced printf by fprintf. * scripts/statistics/tests/z_test.m: replaced printf by fprintf. * scripts/statistics/tests/z_test_2.m: replaced printf by fprintf. * scripts/strings/strtok.m: replaced printf by fprintf. * scripts/testfun/__run_test_suite__.m: replaced puts by fputs and printf by fprintf. * scripts/testfun/demo.m: replaced printf by fprintf. * scripts/testfun/example.m: replaced printf by fprintf. * scripts/testfun/private/compare_plot_demos.m: replaced printf by fprintf. * scripts/testfun/rundemos.m: replaced printf by fprintf. * scripts/testfun/runtests.m: replaced puts by fputs and printf by fprintf. * scripts/testfun/speed.m: replaced printf by fprintf. * scripts/testfun/test.m: replaced printf by fprintf. * scripts/time/calendar.m: replaced puts by fputs and printf by fprintf. * test/io.tst: replaced printf by fprintf.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 20 Apr 2016 17:53:10 +0200
parents ecce63c99c3f
children 96518f623c91
comparison
equal deleted inserted replaced
21632:e3c44a120a8c 21633:dcf8922b724b
393 %! 393 %!
394 %! N = 10; 394 %! N = 10;
395 %! A = diag ([1:N]); b = rand (N, 1); 395 %! A = diag ([1:N]); b = rand (N, 1);
396 %! y = A \ b; # y is the true solution 396 %! y = A \ b; # y is the true solution
397 %! x = pcg (A, b); 397 %! x = pcg (A, b);
398 %! printf ("The solution relative error is %g\n", norm (x - y) / norm (y)); 398 %! fprintf ("The solution relative error is %g\n", norm (x - y) / norm (y));
399 %! 399 %!
400 %! ## You shouldn't be afraid if pcg issues some warning messages in this 400 %! ## You shouldn't be afraid if pcg issues some warning messages in this
401 %! ## example: watch out in the second example, why it takes N iterations 401 %! ## example: watch out in the second example, why it takes N iterations
402 %! ## of pcg to converge to (a very accurate, by the way) solution 402 %! ## of pcg to converge to (a very accurate, by the way) solution
403 403
407 %! 407 %!
408 %! N = 10; 408 %! N = 10;
409 %! A = diag ([1:N]); b = rand (N, 1); 409 %! A = diag ([1:N]); b = rand (N, 1);
410 %! X = A \ b; # X is the true solution 410 %! X = A \ b; # X is the true solution
411 %! [x, flag, relres, iter, resvec] = pcg (A, b); 411 %! [x, flag, relres, iter, resvec] = pcg (A, b);
412 %! printf ("The solution relative error is %g\n", norm (x - X) / norm (X)); 412 %! fprintf ("The solution relative error is %g\n", norm (x - X) / norm (X));
413 %! title ("Convergence history"); 413 %! title ("Convergence history");
414 %! semilogy ([0:iter], resvec / resvec(1), "o-g"); 414 %! semilogy ([0:iter], resvec / resvec(1), "o-g");
415 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||/||b||)"); 415 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||/||b||)");
416 %! legend ("relative residual"); 416 %! legend ("relative residual");
417 417
421 %! 421 %!
422 %! N = 10; 422 %! N = 10;
423 %! A = hilb (N); b = rand (N, 1); 423 %! A = hilb (N); b = rand (N, 1);
424 %! X = A \ b; # X is the true solution 424 %! X = A \ b; # X is the true solution
425 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], 200); 425 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], 200);
426 %! printf ("The solution relative error is %g\n", norm (x - X) / norm (X)); 426 %! fprintf ("The solution relative error is %g\n", norm (x - X) / norm (X));
427 %! printf ("Condition number estimate is %g\n", eigest(2) / eigest(1)); 427 %! fprintf ("Condition number estimate is %g\n", eigest(2) / eigest(1));
428 %! printf ("Actual condition number is %g\n", cond (A)); 428 %! fprintf ("Actual condition number is %g\n", cond (A));
429 %! title ("Convergence history"); 429 %! title ("Convergence history");
430 %! semilogy ([0:iter], resvec, ["o-g";"+-r"]); 430 %! semilogy ([0:iter], resvec, ["o-g";"+-r"]);
431 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)"); 431 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)");
432 %! legend ("absolute residual", "absolute preconditioned residual"); 432 %! legend ("absolute residual", "absolute preconditioned residual");
433 433
444 %! A(i:i+1, i:i+1) = [2 -1; -1 2]; 444 %! A(i:i+1, i:i+1) = [2 -1; -1 2];
445 %! endfor 445 %! endfor
446 %! b = rand (N, 1); 446 %! b = rand (N, 1);
447 %! X = A \ b; # X is the true solution 447 %! X = A \ b; # X is the true solution
448 %! maxit = 80; 448 %! maxit = 80;
449 %! printf ("System condition number is %g\n", cond (A)); 449 %! fprintf ("System condition number is %g\n", cond (A));
450 %! ## No preconditioner: the convergence is very slow! 450 %! ## No preconditioner: the convergence is very slow!
451 %! 451 %!
452 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit); 452 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit);
453 %! printf ("System condition number estimate is %g\n", eigest(2) / eigest(1)); 453 %! fprintf ("System condition number estimate is %g\n", eigest(2) / eigest(1));
454 %! title ("Convergence history"); 454 %! title ("Convergence history");
455 %! semilogy ([0:iter], resvec(:,1), "o-g"); 455 %! semilogy ([0:iter], resvec(:,1), "o-g");
456 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)"); 456 %! xlabel ("Iteration"); ylabel ("log(||b-Ax||)");
457 %! legend ("NO preconditioning: absolute residual"); 457 %! legend ("NO preconditioning: absolute residual");
458 %! 458 %!
459 %! pause (1); 459 %! pause (1);
460 %! ## Test Jacobi preconditioner: it will not help much!!! 460 %! ## Test Jacobi preconditioner: it will not help much!!!
461 %! 461 %!
462 %! M = diag (diag (A)); # Jacobi preconditioner 462 %! M = diag (diag (A)); # Jacobi preconditioner
463 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit, M); 463 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit, M);
464 %! printf ("JACOBI preconditioned system condition number estimate is %g\n", eigest(2) / eigest(1)); 464 %! fprintf ("JACOBI preconditioned system condition number estimate is %g\n", eigest(2) / eigest(1));
465 %! hold on; 465 %! hold on;
466 %! semilogy ([0:iter], resvec(:,1), "o-r"); 466 %! semilogy ([0:iter], resvec(:,1), "o-r");
467 %! legend ("NO preconditioning: absolute residual", ... 467 %! legend ("NO preconditioning: absolute residual", ...
468 %! "JACOBI preconditioner: absolute residual"); 468 %! "JACOBI preconditioner: absolute residual");
469 %! 469 %!
473 %! M = zeros (N, N); k = 4; 473 %! M = zeros (N, N); k = 4;
474 %! for i = 1 : k : N # form 1-D Laplacian matrix 474 %! for i = 1 : k : N # form 1-D Laplacian matrix
475 %! M(i:i+k-1, i:i+k-1) = A(i:i+k-1, i:i+k-1); 475 %! M(i:i+k-1, i:i+k-1) = A(i:i+k-1, i:i+k-1);
476 %! endfor 476 %! endfor
477 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit, M); 477 %! [x, flag, relres, iter, resvec, eigest] = pcg (A, b, [], maxit, M);
478 %! printf ("BLOCK JACOBI preconditioned system condition number estimate is %g\n", eigest(2) / eigest(1)); 478 %! fprintf ("BLOCK JACOBI preconditioned system condition number estimate is %g\n", eigest(2) / eigest(1));
479 %! semilogy ([0:iter], resvec(:,1), "o-b"); 479 %! semilogy ([0:iter], resvec(:,1), "o-b");
480 %! legend ("NO preconditioning: absolute residual", ... 480 %! legend ("NO preconditioning: absolute residual", ...
481 %! "JACOBI preconditioner: absolute residual", ... 481 %! "JACOBI preconditioner: absolute residual", ...
482 %! "BLOCK JACOBI preconditioner: absolute residual"); 482 %! "BLOCK JACOBI preconditioner: absolute residual");
483 %! hold off; 483 %! hold off;