solution.m 941 B

123456789101112131415161718192021222324252627282930313233
  1. function [b,sol]=solution(A,i_rhs)
  2. %
  3. % [b,sol] = solution(A,i_rhs)
  4. %
  5. % This function allows to generate a right hand side b for
  6. % the linear system A*x=b so as that the exact solution is known.
  7. %
  8. % Input :
  9. % A : the coefficient matrix of the linear system A*x=b
  10. % i_rhs : integer (i_rhs = 1 or 2 or 3)
  11. % if i_rhs = 1 then the entries of the exact solution of the linear system
  12. % are generated randomly in [0, 1]
  13. % if i_rhs = 2 then the entries of the exact solution of the linear
  14. % system equals 1.
  15. % if i_rhs = 3 then the k-th entry of the exact solution of the linear
  16. % system equals k.
  17. %
  18. % Output :
  19. % b : the right hand side of the linear system A*x=b
  20. % sol : the exact solution of the linear system A*x=b
  21. %
  22. n = length(A)
  23. if (i_rhs==1)
  24. sol=rand(n,1);
  25. end
  26. if (i_rhs==2)
  27. sol=ones(n,1);
  28. end
  29. if (i_rhs==3)
  30. sol=[1:n]';
  31. end
  32. b=A*sol;