main.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. % main.m
  2. %
  3. %
  4. % May 2013
  5. %
  6. % This script allows to perform some numerical tests by giving the results
  7. % obtained when solving a linear system A*x=b by using the CMRH algorithm
  8. %
  9. % You have to choose :
  10. % - the size of the system,
  11. % - the coefficient matrix,
  12. % - the exact solution (so that we can compute the error),
  13. % - the tolerance
  14. %
  15. fprintf('Hello world \n')
  16. fprintf('Enter the matrix choice :\n')
  17. fprintf('1- A(i,j)= (alpha*j-beta) / (n-i+j) if j <= i \n')
  18. fprintf(' (alpha*i-beta) / (n-i+j) if j > i \n')
  19. fprintf('2- A(i,j)= i if j <= i \n')
  20. fprintf(' j if j > i \n')
  21. fprintf('3- A(i,j)= abs(i-j)+1/(i-j) if j < i and j > i \n')
  22. fprintf(' 0 if i = j \n')
  23. fprintf('4- A(i,j)= i+j \n')
  24. fprintf('5- helsing matrix \n')
  25. fprintf('6- Matrix Market matrix \n')
  26. matrixchoice=input('Enter your choice : ');
  27. if (matrixchoice <6)
  28. n=input('Enter the size of the matrix : ');
  29. if (matrixchoice <5)
  30. A = matrice(n,matrixchoice);
  31. end
  32. if (matrixchoice==5)
  33. A = helsing(n);
  34. end
  35. else
  36. filename = input('Enter the matrix file name :','s');
  37. [A,rows,cols,entries,rep,field,symm] = mmread(filename);
  38. n = rows;
  39. end
  40. fprintf('Enter the solution choice :\n')
  41. fprintf('1-sol=random vector \n')
  42. fprintf('2-sol=[1,1,...,1]^T \n')
  43. fprintf('3-sol=[1,2,...,n]^T \n')
  44. solchoice = input('Enter your choice : ');
  45. [b,sol] = solution(A,solchoice);
  46. tol = input('Enter the tolerance : ');
  47. iprint = input('Print results : 1-Yes, 2-No : ');
  48. V = A; bc = b;
  49. [x,pseudores,flag] = CMRH(A,b,tol,n,zeros(n,1),iprint);
  50. fid = fopen('pseudores.dat','w');
  51. for i=1:length(pseudores)
  52. fprintf(fid,'%i \n',pseudores(i));
  53. end
  54. fclose(fid);
  55. b = x;
  56. if (iprint==1)
  57. fprintf('\n approx. and exact solutions :\n');
  58. if (n < 10)
  59. for i = 1:n
  60. fprintf(' %6.4e %6.4e\n',b(i),sol(i));
  61. end
  62. else
  63. for i = 1:10
  64. fprintf(' %6.4e %6.4e\n',b(i),sol(i));
  65. end
  66. end
  67. % we compute the norm of the residu
  68. bc = -V*b+bc;
  69. re_cmrh = norm(bc,2);
  70. % we compute the norm of the error
  71. sol = -b+sol;
  72. er_cmrh = norm(sol,2);
  73. fprintf('residual norm: %6.4e error norm: %6.4e \n',re_cmrh,er_cmrh);
  74. end