Collinearity code not working?

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 Comments

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.
Sorry did not see that button but I edited it.

Sign in to comment.

 Accepted Answer

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 Comments

I did that and now I am receiving this error: Error in mylinecheck (line 2) p1(a,b);
I tried adding an = sign after p1 but then that came up with an unbalanced error.
It seems ‘p1’ is not defined. If you want to assign ‘a’ and ‘b’ to ‘p1’, use square brackets to indicate an array:
p1 = [a,b];
Yes I did want to do that but now I am getting another error: Output argument "g" (and maybe others) not assigned during call to
You have to change the ‘g’ output argument to whatever variable you want your function to return to your script workspace. I have no idea what that variable is, so that is your decision.
Bob
Bob on 10 Feb 2015
Edited: Bob on 10 Feb 2015
Thank you the code now runs but I am not getting the right answers. I have attached a test file that has the code to test and what the answer should be. For example: Test#1: a = mylinecheck(0,0,2,2,5,5) a = 1 (answer)
Instead I am getting 0 instead of 1? It seems to just printing out what ever the first number is in the line, so 0 for this example.
What variable do you have your function return?
What variable do you want it to return?
Bob
Bob on 10 Feb 2015
Edited: Bob on 10 Feb 2015
Right now it is returning a = whatever the first number is being tested. It should return either a =1 or a =0 depending on the points that is used in the test file. 1 means the points are in the same line and 0 means they are not.
If I remember correctly, ‘a’ was also your first input argument.
Surprise! It assigned that value to the output, likely because you did not have your function calculate any value of ‘a’ to replace it.
I told you earlier not to name your output arguments the same as any of your input arguments, for that reason. See Function Basics for details.
What do you want your function to return to your script code workspace? Make that variable (or those variables) your output argument list.
Please read the documentation. It is clearly written, and extremely informative. If there are parts of it you don’t understand, we will help you. But please make an effort to understand it!
Basic MATLAB programming is not rocket surgery!
I read the article and I am still completely confused. I know that I need an output function to print 1 if they are on the same line and 0 if not, but I am still not sure how I would do that. Would I use and if loop, slopes are the same then =1 and if not =0? So something like:
function g = mylinecheck(a,b,c,d,e,f)
p1 = [a,b];
p2 = [c,d];
p3 = [e,f];
end
function tf = collinear(p1,p2,p3)
m = slope(p1,p2);
g = 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 g = intercept(p1,p2)
m = slope(p1,p2);
g = -p1(2)+m*p1(1);
end
function g = output(a);
m = slope(p1,p2);
if g == m;
g = 1;
else
g = 0;
end
end
This is what I have now but I am still getting: Output argument "g" (and maybe others) not assigned during call to
Variable ‘g’ is not assigned because you never call your ‘intercept’ function, perhaps because you never call your ‘collinear’ function.
I leave that for you to sort out.
I am sorry but I still am not sure how to do that? I know what I need to do.
Since you know what you need to do, experiment until you succeed in doing it. That is how we all learned to program. I guarantee you the world will not come to an end if your program throws an error or gives you the wrong result the first (or the first several) times you run it.
Go back and read the documentation on functions. It will tell you how to call them. It seems your ‘mylinecheck’ function needs at some point to call all of your subfunctions from within it, so all you have to do is to code the calls correctly and in the correct order. Then call ‘mylinecheck’ from your main script, and make appropriate use of whatever you want it to return.
Be sure it returns the variables you want it to as its output. (It doesn’t have to be ‘g’ — I just used that as an example — and you can return more than one output.)
I figured it out. Thank you for your help!
My pleasure!
I knew you could.
could you post your corrected code so i can see what changes you made to get it to work properly?

Sign in to comment.

More Answers (0)

Asked:

Bob
on 10 Feb 2015

Commented:

on 20 Feb 2015

Community Treasure Hunt

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

Start Hunting!