Collinearity code not working?

조회 수: 7 (최근 30일)
Bob
Bob 2015년 2월 10일
댓글: David Rogers 2015년 2월 20일
Question: mylinecheck(a,b,c,d,e,f) which takes six inputs: [30 pts] a,b,c,d,e,f: Real numbers. You may assume that a,c,e are all nonequal. Does: Checks if the three points (a, b), (c, d) and (e, f) all lie on the same line. How you do this is up to you but I suggest trying to find a really quick way. Quick ways exist! You’ll almost certainly need an if statement. Returns: 1 if they do and 0 if they don’t
Code:
if true
% code
end
function mylinecheck(a,b,c,d,e,f)
p1 (a,b);
p2 (c,d);
p3 (e,f);
end
function tf = collinear2(p1,p2,p3)
m = slope(p1,p2);
b = intercept(p1,p2);
if ~isfinite(m)
if (p3(1)==p1(1))
tf = true;
else
tf = false;
end
else
tf = (m*p3(1)+b) == p3(2);
end
end
function m = slope(p1,p2)
m = (p2(2)-p1(2))/(p2(1)-p1(1));
end
function b = intercept(p1,p2)
m = slope(p1,p2);
b = -p1(2)+m*p1(1);
end
Test file:
format long;
a=mylinecheck(0,0,2,2,5,5)
Error: Error using mylinecheck Too many output arguments.
Error in Test (line 2) a=mylinecheck(0,0,2,2,5,5)
  댓글 수: 2
Star Strider
Star Strider 2015년 2월 10일
It would definitely help if you used the [ {} Code ] button to format your code. It makes it easier to read, and if it’s easier to read, it’s more likely your Question will get an Answer.
Bob
Bob 2015년 2월 10일
Sorry did not see that button but I edited it.

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

채택된 답변

Star Strider
Star Strider 2015년 2월 10일
See if changing the first line of your function to:
function g = mylinecheck(a,b,c,d,e,f)
and then assign ‘g’ (or whatever variable you want it to return) somewhere in your code as well. (The output argument and the variable you want the function to return must have the same name.) It is not obvious what you want your function to return, but it should also not be the same name as one of your input arguments (for example, (a,b,c,d,e,f)). That causes confusion at the very least, and could throw an error.
  댓글 수: 15
Star Strider
Star Strider 2015년 2월 12일
My pleasure!
I knew you could.
David Rogers
David Rogers 2015년 2월 20일
could you post your corrected code so i can see what changes you made to get it to work properly?

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by