MATLAB: Use while loops for a function to keep asking for inputs, until an invalid input is entered

loopsMATLAB

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')

Best Answer

Your logic is fine. The problem is with your use of y(x). MATLAB is a computational, not symbolic, language - y is a variable calculated from x, there is no functional relationship between them. So just change y(x) to y and you should be fine.
If you want to use a functional type of relationship you could do something similar to what Paulo suggested, however you should use a function handle, not inline (sorry Paulo!)
y = @(x) log(1/(1-x));
x = input(...
I actually prefer your logic, than using while 1 and break, but that's just personal style.
BTW, I don't suppose you could ask your teacher why they think anyone should do something like this in MATLAB...?
Related Question