Use while loops for a function to keep asking for inputs, until an invalid input is entered
Show older comments
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')
Accepted Answer
More Answers (2)
Paulo Silva
on 6 Mar 2011
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
on 7 Mar 2011
0 votes
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.
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!