Use while loops for a function to keep asking for inputs, until an invalid input is entered
이전 댓글 표시
Ok i have 4 problems based off of this one topic. y(x)=ln(1/(1-x)), Evaluate the function for any user specified value of x. Use a while loop, so that the program repeats for each legal value of x entered. When an illegal value of x is entered, terminate the program, when x>=1.
I dont understand how to get it to repeat and then terminate. Heres a rough attempt, gives an error:
%Homework 6 Problem 4_18
x=input('Please input an x-value < 1:');
while x<1
y(x)=log(1/(1-x));
fprintf('When x is %g, y is %g',x,y(x))
x=input('Please input an x-value < 1');
end
if x>=1
fprint('Error-Program Terminated')
채택된 답변
추가 답변 (2개)
Paulo Silva
2011년 3월 6일
y=inline('log(1/(1-x))');
while 1
x=input('Please input an x-value < 1 ->');
if x>=1, break, end
fprintf('When x is %g, y is %g\n',x,y(x))
end
fprintf('Error-Program Terminated\n')
Walter Roberson
2011년 3월 7일
0 개 추천
Your program does not produce correct output for the case where the user enters a complex number with non-zero imaginary part. You might deem that such numbers are "illegal numbers", but they are never < 1 or >= 1, and therefore by the terms of the assignment, the program must not terminate in that case (it is only to terminate if the entered value >= 1). As the assignment further specifies that the program must "Evaluate the function for any user specified value of x" and it does not describe complex numbers as being illegal values, your program must produce correct answers for complex numbers, but in fact it drops the imaginary portion of the x and y values from the output.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!