main.m 2.2 KB

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