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!