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 で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!