Collinearity code not working?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
2015년 2월 10일
Sorry did not see that button but I edited it.
채택된 답변
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
Bob
2015년 2월 10일
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];
Bob
2015년 2월 10일
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
Star Strider
2015년 2월 10일
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.
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.
Star Strider
2015년 2월 10일
What variable do you have your function return?
What variable do you want it to return?
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.
Star Strider
2015년 2월 10일
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!
Bob
2015년 2월 10일
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
Star Strider
2015년 2월 11일
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.
Bob
2015년 2월 11일
I am sorry but I still am not sure how to do that? I know what I need to do.
Star Strider
2015년 2월 11일
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.)
Bob
2015년 2월 12일
I figured it out. Thank you for your help!
Star Strider
2015년 2월 12일
My pleasure!
I knew you could.
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개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
