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 件のコメント
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.
Star Strider
2015 年 2 月 10 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
