Question

Using matlab and if/else statement please!

Write a function that determines the real roots of a quadratic equation ax2 + bx + c = 0. To calculate the roots of the equation, the function calculates the discriminant D, given by: D = b2-4ac If D> 0, the code should display The equation has two roots and print the values on a new line. If D 0, the code should display The equation has one root., and print the value on a new line. If D <0, the code should display The equation has no real roots. The function must take three inputs, a, b, and c (in that order), and provide two outputs (the two roots), with NaN as the default value if less than two roots exist. For example if there is one real root of 4, the function returns 4 and NaN. If there are no real roots, it returns NaN and NaN. f there are two real roots, the function must return them in ascending order. For example, if the roots are 4 and 2, it must return them in the order: 2, 4. As with the previous problem, it is up to your discretion whether the code to display the root(s) is included in the function or in the script. In the script, run the following test cases: a. 2x2+8x+8 0 b. -5x2 3x-4 0 c. -2x2+ 7x +4 -0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

%%%%%%%%%%%%%save this as cal_root.m

function [root1,root2]=cal_root(a,b,c)
d=b^2-4*a*c;
if(d>0)
root1=(-b-d^0.5)/(2*a);
root2=(-b+d^0.5)/(2*a);
if(root1>root2)
temp=root1;
root1=root2;
root2=temp;
end
elseif(d==0)
root1=-b/(2*a);
root2=NaN;
else
root1=NaN;
root2=NaN;
end
end

%%%%%%%%%%%%%%%end of file1

%%%%%start of file 2

[root1,root2]=cal_root(2,8,8);
fprintf('roots for 2x^2+8x+8=0\n');
disp(root1)
disp(root2)
[root1,root2]=cal_root(-5,3,-4);
fprintf('roots for -5x^2+3x-4=0\n');
disp(root1)
disp(root2)
[root1,root2]=cal_root(-2,7,4);
fprintf('roots for -2x^2+7x+4=0\n');
disp(root1)
disp(root2)

MATLAB R2017a HOME Search Documentation Log In ▼ Go To▼ Comment ge-ese Breakpoints Run Run and ES Compare New Open Save Advance Run and Indent E Print Find ▼ Advance Current Folder Editor - GAMATLABR2017a bin Untitled57.m Name ▲ 田山m3iregistry A [rooti,root2]-cal_root [2, 8,8) registry util 2- fprintf( roots for 2x^2+8x+8=0\n) ; 3disp (root1) 4 -d1sp (root2) 5 -[rooti, root2]-cal root (-5, 3,-4); 6- fprintf( roots for -5x^2+3x-4=0\n ); 7-disp (rooti) Bdisp (root2) 9[rooti, root2]-cal_root (-2,7,4): 田」 win64 cal root.asv cal root.m fractalTreeBasic.m fractalTriangle.m imageProcessing.m Command Window >Untitled57 IterMeth.m Icdata.ml Details Works Name- NaN root3 for -5x^2+3x-4=0 Value NaN 0.2500 1,0000e-06 9.8000 13 68.1000 0.5000 NaN -0.5000 rooti 20 2 script Ln 10 Col 34

Add a comment
Know the answer?
Add Answer to:
Using matlab and if/else statement please! Write a function that determines the real roots of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT