Info
This question is closed. Reopen it to edit or answer.
How do I use while loop to cycle through 3 cases and stop after evaluating the three cases?
1 view (last 30 days)
Show older comments
My homework problem reads:
"A mathematical relationship between x and y is described by the following expressions:
y=A*exp(x^2)-B*x^3+x-1 if x<=0 (Case 1)
y=A*log10(x)-B*x^4+1.5 if 0<x<=10 (Case 2)
y=A/log(x)-B/x+C if x>10 (Case 3)
where A, B, and C are constants. Write a program that inputs the constants A,B,C, and the argument x, and computes the corresponding value of y. Print x by using 4 decimal places, and y by using 5 decimal places.
Use if/else statements to choose the proper expression for y, corresponding to selected x.
After reading A,B,C, 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 again, your program should ask you to input x from another interval, until you input x once from each interval.
Your output should look like:
Enter A, B, C:
-1.5 2.5 0.125
Enter x: -3.5
Case 1
x value is = -3.5000 and y value is = -313369.24580
Enter x: -5
Enter x from another interval!
Enter x: 1.85
Case 2
x value is = 1.8500 and y value is = -28.18452
Enter x: 12
Case 3
x value is = 12.0000 and y value is = -0.68698"
So far i have the if/else conditions down but i'm not sure how to proceed with the while loop. any guidance will be appreciated!
1 Comment
James Tursa
on 17 Nov 2015
Please post the code you have so far so we can comment on the next steps.
Answers (1)
Stalin Samuel
on 18 Nov 2015
A = -1.5;B = 2.5; C= 0.125;
count=[1 1 1]%three one for three cases
while(sum(count)>0)
x = input('enter the value of x:');
if x<=0
if count(1)>0
count(1) = 0;%fisrt case tested
y=A*exp(x^2)-B*x^3+x-1
else
disp('enter another no')
end
elseif x>0&x<=10
if count(2)>0
count(2) = 0; %second case tested
y=A*log10(x)-B*x^4+1.5
else
disp('enter another no')
end
elseif x>10
if count(3)>0
count(3) = 0;%third case tested
y=A/log(x)-B/x+C
else
disp('enter another no')
end
end
end
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!