Write a short piece of MATLAB code that will: Ask a user for a value, R. Generat
ID: 3729044 • Letter: W
Question
Write a short piece of MATLAB code that will:
Ask a user for a value, R.
Generate the message: The square root of X.XXX is Y.YYY, if R is non-negative.
If R is negative, the code should generate the message: R is negative. The square root of X.XXX is Y.YYYi [imaginary number]
The value of R should be substituted for X.XXX, and the value of the square root of the magnitude of R should be substituted for Y.YYY. Both numbers should be displayed with three decimal places. For this question, use values where -10 < R < 10.
HINT: Need to use input statement and if / else statement to make sure values are within range. If not in range, print out error message using error (‘error message’) statement. Use fprintf for output.
Explanation / Answer
if (R<=-10 || R>=10)
else
if (R>=0)
sq = sqrt(R);
fprintf('The square root of %.3f is %.3f ',R,sq);
else
sq = sqrt((-1*R));
fprintf('%.3f is negative. The square root of %.3f is %.3f i ',R,R,sq);
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.