Can someone help me with my if statements in my function?

조회 수: 1 (최근 30일)
Scott
Scott 2016년 5월 1일
편집: Scott 2016년 5월 2일
I have written this function for a homework assignment, Basically finding heronian isosceles triangles for a given Perimeter. My if statements
if ((b>= 2*a)) && ((b<=0));
continue
end
if IntTestA == 0
a=0; b=0; A=0;
continue
end
are not working properly I was wondering if someone could explain to me why.
  댓글 수: 1
Jan
Jan 2016년 5월 1일
편집: Jan 2016년 5월 1일
This is not twitter - tags do not need hash characters.
Before we can explain, why the code is not working "properly", we have to know, what you call "poperly". Note that all information we have is the "failing" code. And this code does exactly what its lines commands it to do. Due to the absence of comments, guessing your intention is risky.

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

채택된 답변

Jos (10584)
Jos (10584) 2016년 5월 1일
I quickly see three issues:
  1. a is always positive so b can never be both positive (>=2*a) and negative (<= 0).
  2. for A = sqrt(...) is a loop over a single value
  3. setting the iterator in a loop (a=0) does not effect the loop. See this example
for k=1:5
disp(k)
k = 5 ;
disp(k)
end
  댓글 수: 1
Scott
Scott 2016년 5월 2일
Thanks, I used elseif instead of && for P and b conditions and dropped the second point the question asked that the function output [0,0,0] if It does not satisfy Area as an integer. Maybe I just do not understand function output and how to alter it... Overall it's working now :) Just need to work out how to frintf 4 values P,a,b,A

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 5월 1일
Your test “IntTestP==0” does not accomplish what you think it does. The only way “fix((fix(P)-P)+1)” can be zero is for P to be a positive non-integer, and the further requirement that P<=0 is then impossible which means that the error message can never be issued.
  댓글 수: 1
Scott
Scott 2016년 5월 1일
편집: Scott 2016년 5월 2일
Thanks, you are right, I hadn't tested the integer test yet. :) Made the change below and it is working. if (IntTestP == 0); disp('error, input is not a non negative integer') elseif (P <= 0); disp('error, input is not a non negative integer') and it works as intended.

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

카테고리

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