Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function callcramer(m)
- disp(' ');
- A = rand(m,m);
- b = rand(m,1);
- disp('Timing backslash:');
- tic
- x = A\b; % Backslash operator for solving A*x = b
- toc
- xc = cramersrule(A,b);
- disp(['norm of backslash residual = '
- num2str(norm(A*x-b),4)]);
- disp(['norm of Cramer''s Rule residual = '
- num2str(norm(A*xc-b),4)]);
- disp(' ');
- return
- end
- function x = cramersrule(A,b) % Demo of Cramer's Rule for
- solving A*x = b
- disp('Timing Cramer''s Rule:');
- tic
- [m n] = size(b);
- z = zeros(m,1);
- Ai = A;
- for k=1:m
- Ai(:,k) = b;
- z(k) = det(Ai);
- Ai(:,k) = A(:,k);
- end
- detA = det(A);
- x = z / detA;
- toc
- disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
- disp(['abs(det(A)) = ' num2str(abs(detA),4)]);
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement