필터 지우기
필터 지우기

Can anybody help me point out why I cant display x(result) when I call my function?

조회 수: 2 (최근 30일)
function x = cramer(A, b)
r = size(A, 1);
c = size(A, 2);
n = size(b, 1);
if r ~= c
disp('Oops! Please enter a square matrix');
end
if r == c == n
D = det(A);
if D == 0
disp('Oops! Either, there are a family of soultions or no unique solution')
end
if D ~= 0
result = zeros(n, 1);
for i = 1:n
A_x = A;
A_x(:, i) = b;
Dx = det(A_x);
result(i,1) = Dx/D;
end
x = result
end
end
end
  댓글 수: 2
Kazi Anisha Islam
Kazi Anisha Islam 2018년 9월 25일
Say A = [2 1 3; 0 -1 2; -2 3 1] and b = [1; 0; -3] and then call the function
x = cramer(A,b)

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2018년 9월 25일
편집: John D'Errico 2018년 9월 25일
There are soooo many problems in this code, that I stopped looking early. Sorry, but true. Hey, at least you know to use semi-colons at the end of your lines. So, what did I notice?
1. When you get an error condition, you display a message using disp, but then the rest of the code keeps on running, trying to execute anyway. Bad idea! You did that in several spots. You should consider using the function error when an error arises, instead of disp. Or, you need to learn how to terminate the code after you report an error using disp. Just use error.
2. if r == c == n
Sigh. You seem to think this test is valid in MATLAB. Sadly, it will NEVER do what you think it does. It is equivalent to the statement if (r==c) == n. Is r==c? If so, the result is 1. If not, then the result of the first test is 0. So, unless n==1 or n==0 as appropriate, then the combined test will never be true. If you want to do that test, you NEED to do both tests.
if (r==c) && (c==N)
Do you need the parens there? Well, not really. But MATLAB does not charge extra for a few extra characters. To know when you can be safe and assume you know the order of operations, read this:
https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
Memorize it. Or, be safe, and use parens when you are not sure.
3. You test for exact equality of det(A). Sigh. BAD idea for several reasons. First, det will often returns a non-integer result. Ok, that won't happen in theory for exactly integer input. Or will it? A proof is most easily given by counter-example.
A = randi([-5,5],6,6)
A =
0 -1 1 1 -2 5
-4 -2 -5 2 3 1
0 1 5 -1 -4 5
-1 2 -2 -4 3 1
4 1 -4 1 -1 -1
-1 0 -4 0 -2 -3
det(A)
ans =
5328
det(A) == 5328
ans =
logical
0
What the heck happened? det does not compute an exact determinant! In fact, it uses floating point arithmetic to do the work, and it uses linear algebra to compute that value.
sprintf('%0.55f',det(A))
ans =
'5328.0000000000009094947017729282379150390625000000000000000'
Not an integer, even though we know that in theory, the result must be an integer. Worse, testing using det is still a bad idea. If you want to test for numerical singularity of a matrix, use rank, despite what your teacher might have told you. Det is a terribly poor way to do that test. The simplest way to see that is to ask if a diagonal matrix, with all ones on the diagonal is singular. Does it have an inverse? What if I divide each element by 10?
det(eye(400)) == 0
ans =
logical
0
det(eye(400)/10) == 0
ans =
logical
1
Clearly, det thinks the second matrix is singular, yet we know it is not.
4. Using Cramer's rule to compute the solution to a system of linear equations is a god-awfully bad way to do it. Did your teacher tell you to use that? If so, I hope it was just to teach you why that is a bad idea. Need I talk about det again? My guess is they never taught you the better ways so far in class.
My guess is your problem lies in one of the issues I listed, but there could easily be others. I'd start with #2 on that list. Then I'd deal with #1. #3 will be important at some point in your travels through MATLAB. And I really do hope your teacher teaches you why Cramer's rule is a bad idea.

추가 답변 (1개)

KSSV
KSSV 2018년 9월 25일
A = [2 1 3; 0 -1 2; -2 3 1];
b = [1; 0; -3] ;
r = size(A, 1);
c = size(A, 2);
n = size(b, 1);
if r ~= c
disp('Oops! Please enter a square matrix');
end
if (r == c) && (c == n)
D = det(A);
if D == 0
disp('Oops! Either, there are a family of soultions or no unique solution')
end
if D ~= 0
result = zeros(n, 1);
for i = 1:n
A_x = A;
A_x(:, i) = b;
Dx = det(A_x);
result(i,1) = Dx/D;
end
x = result ;
end
end

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by