Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function callcramer(m)
  2. disp(' ');
  3. A = rand(m,m);
  4. b = rand(m,1);
  5. disp('Timing backslash:');
  6. tic
  7. x = A\b; % Backslash operator for solving A*x = b
  8. toc
  9. xc = cramersrule(A,b);
  10. disp(['norm of backslash residual = '
  11. num2str(norm(A*x-b),4)]);
  12. disp(['norm of Cramer''s Rule residual = '
  13. num2str(norm(A*xc-b),4)]);
  14. disp(' ');
  15. return
  16. end
  17.  
  18. function x = cramersrule(A,b) % Demo of Cramer's Rule for
  19. solving A*x = b
  20. disp('Timing Cramer''s Rule:');
  21. tic
  22. [m n] = size(b);
  23. z = zeros(m,1);
  24. Ai = A;
  25. for k=1:m
  26. Ai(:,k) = b;
  27. z(k) = det(Ai);
  28. Ai(:,k) = A(:,k);
  29. end
  30. detA = det(A);
  31. x = z / detA;
  32. toc
  33. disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
  34. disp(['abs(det(A)) = ' num2str(abs(detA),4)]);
  35. return
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement