필터 지우기
필터 지우기

identity matrix what am i doing wrong

조회 수: 2 (최근 30일)
David Craven
David Craven 2017년 9월 10일
편집: Stephen23 2017년 9월 11일
The singularitytest function will test whether a matrix is singular. If it is, the output should be 1. Otherwise, the output should be 0. The input, A, can be any n × n matrix. Your code needs to take into consideration very small errors that could occur due to floating point arithmetic (apply a tolerance of 10-10).
function result = singularitytest(A)
n = some_number;
A = matrix (n, n)
d = det(A);
c = cond(d) <= 1e-10;
end
I get this Undefined function or variable 'some_number'.
  댓글 수: 1
Stephen23
Stephen23 2017년 9월 11일
편집: Stephen23 2017년 9월 11일
@David Craven: you recently asked a similar question:
And received an answer that you seem to have ignored completely. Was the advice not clear to you?
In any case the mistakes you are making show that you should do the Introductory Tutorials, which are highly recommended for all beginners:
and also read the MATLAB documentation for all operations and functions that you are using. Guessing how to write MATLAB code is going to be a total waste of your time. Read the documentation.

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

답변 (1개)

John Chilleri
John Chilleri 2017년 9월 10일
편집: John Chilleri 2017년 9월 10일
Hello,
I would recommend you watch a video or read an introduction to coding in MATLAB. They don't take too long and should give you enough knowledge to avoid errors such as these.
First, let's go through your code,
function result = singularitytest(A)
n = some_number;
A = matrix (n, n)
d = det(A);
c = cond(d) <= 1e-10;
end
So from the function definition line,
function result = singularitytest(A)
We know that the function is going to be given some value A (in this case an n x n matrix). Your function will then use this A to compute the variable result, which it returns from the function.
Since you are given A, you don't need the lines,
n = some_number;
A = matrix(n,n)
Also, n = some_number doesn't help MATLAB - it can only work with values. If you were trying to get how big your n x n matrix is, you can use,
n = size(A,1);
Furthermore, matrix is not a MATLAB function, so you'll also get an error on the line,
A = matrix(n,n)
Remember though, you were given matrix A as an input, so you don't need to try to make it.
Finally, you have the right idea in the next lines, but you need to assign a value to result, as that is the variable that is returned from your function.
In this case, I assume you're checking if the determinant of matrix A is zero, with tolerance 1e-10. Don't forget that since determinants can be negative, we must check if it's 1e-10 within zero on both sides of zero, or more simply,
result = abs(det(A)) <= 1e-10;
This line says, "is the absolute value of the determinant of A less than or equal to 1e-10?" If yes, it'll return a 1, if not, it'll return a 0, which is how comparisons naturally work in MATLAB, but it's also what was requested of you. And as a side note, I don't think you need to use the ( cond ) condition number here.
All in all, your function should look like this,
function result = singularitytest(A)
result = abs(det(A)) <= 1e-10;
end
Hope this helps, and please do your best to understand everything - understanding it now will save you countless hours in the future!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by