THIS IS A MATLAB QUESTION. The statement given below calculates the roots of 4x^
ID: 3717985 • Letter: T
Question
THIS IS A MATLAB QUESTION.
The statement given below calculates the roots of 4x^3-12x^2+x-3.
Use the "if construct" to determine if each root is the real or complex. Use the MATLAB
command "isreal" to check each of the 3 roots, one root at a time. "isreal" returns
a 1 if real, a zero if not. Example: isreal(4)=1 isreal(4+2i)=0. [6]
As part of the if statement use fprintf to display a message like "Root number 2 is
complex". Note that "2" is a counter in your loop - not the root value. [2]
Put your "if" construct in a "for" loop so you can check all of the roots [2].
p=[4 -12 1 -3];
r=roots(p)
% start your "for" loop here
for i=1:length(r)
if isreal r(i)
fprintf(' The root number %g is real ',r(i))
else
fprintf(' The root number %g is complex ',r(i))
end
end
fprintf(' ');
I believe it's all correct , except I'm using isreal incorrect. Could someone help, so I can finish this code. Thanks
Explanation / Answer
all of your code is correct but use brackets() at isreal replace your code "if isreal r(i)" with "if isreal(r(i))"
the modified script is:--
p=[4 -12 1 -3];
r=roots(p);
% start your "for" loop here
for i=1:length(r)
if isreal(r(i)) %i have changed your code in this line
fprintf(' The root number %g is real ',r(i));
else
fprintf(' The root number %g is complex ',r(i));
end
end
fprintf(' ');
output is:--
>> roots_isreal
The root number 3 is real
The root number -6.93889e-18 is complex
The root number -6.93889e-18 is complex
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.