Collinearity code not working?

7 次查看(过去 30 天)
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 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by