How would I use a while loop to get a certain amount of inputs from a described interval?

6 Ansichten (letzte 30 Tage)
The problem asks for inputs for 3 constants (a,b,c) and an x value to output y depending on the value of x.
bascially there's 3 cases and this is the code I have so far.
a=input('Please enter value a: ');
b=input('Please enter value b: ');
c=input('Please enter value c: ');
x=input('Enter argument for x: ');
if x<-1 %case 1
y=a*exp(x^3)-b*x^2+x-c
elseif -1<=x<5 %case 2
y=2*log(x)-(a*x-1)^3+c^2
elseif x>=5 %case 3
y=a*exp(-x^2)/log(x)+b/x^2-c
end
This is part of the problem description which I'm having trouble with and would appreciate any help:
Your program should use a while loop to evaluate y for scanned x in each of the above three cases. If you input x from the same interval more than twice, your program should ask you to input x from another interval, until you input x twice from each interval.

Akzeptierte Antwort

KSSV
KSSV am 14 Nov. 2018
c1 = 0 ;
c2 = 0 ;
c3 = 0 ;
while true
a=input('Please enter value a: ');
b=input('Please enter value b: ');
c=input('Please enter value c: ');
x=input('Enter argument for x: ');
if x<-1 %case 1
c1 = c1+1 ;
y=a*exp(x^3)-b*x^2+x-c
if c1 > 2
c1 = 0 ;
fprintf('Please input x from other intervel: you entered x<-1, twice\n')
end
elseif -1<=x<5 %case 2
c2 = c2+1 ;
y=2*log(x)-(a*x-1)^3+c^2
if c2 > 2
c2 = 0 ;
fprintf('Please input x from other intervel: you entered -1<x<5, twice\n')
end
elseif x>=5 %case 3
c3 = c3+1 ;
y=a*exp(-x^2)/log(x)+b/x^2-c
if c3 > 2
c3 = 0 ;
fprintf('Please input x from other intervel: you entered x>=5, twice\n')
end
end
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by