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

2 次查看(过去 30 天)
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')

采纳的回答

Matt Tearle
Matt Tearle 2011-3-7
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...?
  3 个评论
Matt Tearle
Matt Tearle 2011-3-10
I don't like it *when you can use a function handle instead*. Function handles are more precise (you define exactly what the variables are), more flexible (you can embed parameters directly), more transportable (they contain their own path and workspace info), and more efficient. I find them more natural as well, but that's just personal style.
All minor stuff, I guess, but why not use the better option, when there's no real drawback or significant change to your code?
Paulo Silva
Paulo Silva 2011-3-10
Those are the small details that I'm here to learn and will do my best not to use inline function, thanks Matt.

请先登录,再进行评论。

更多回答(2 个)

Paulo Silva
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
Walter Roberson 2011-3-7
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.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by