In this problem, you will use MATLAB to compute the roots of a quadratic functio
ID: 3111540 • Letter: I
Question
In this problem, you will use MATLAB to compute the roots of a quadratic function as accurately as possible. a) Write a MATLAB function that computes the roots of f(x) = ax^2 + bx + c, where a, b, and c are real constants. Write the function call as [x_1, x_2] = quadroots(a, b, c) where x_1 and x_2 are the two roots. Make sure to minimize the effect of loss of significance, and reuse as few arithmetic operations as possible. You may need if statements to cover the cases when b is positive and when b is negative. Your function can also assume that b^2 > 4ac. b) Write a script file that uses quadroots to compute the roots for both the quadratic functions f(x) = x^2 + 10^5 x +1 and f(x) = x^2 + 10^5x + 1, and f(x) = x^2 + 10^5x + 1, and tests the function by computing both |f(x1)| and | f(x2) | for both quadratic functions, where x1 and x2 are the outputs of the function quadroots. Also, compute |f(x1)| and |f(x2)| for both quadratic functions after computing x1 and x2 from the quadratic formula, i.e., where x1, x2 = -b plusminus Squareroot b^2- 4ac/2a. Compare your results. Note that |f(x1)| and |f(x2| are called backward errors, since they show how well the solutions "solve" the original problem instead of giving the actual error, Make sure to display all computed roots and backward errors.Explanation / Answer
(a). clear, clc
for k=1:3
disp ('For the equation ax^2+ bx+c')
a=input (' Enter a: ') ;
b=input (' Enter b: ') ;
c=input (' Enter c: ') ;
D=b^2-4*a*c ;
if D<0
fprintf( ' The equation has no real roots. ')
elseif D==0
root=-b/(2*a) ;
fprintf( ' The equation has one real roots , ')
fprintf( '%. 3f ' ,root)
else
r1=(-b+sqrt (D) )/ (2*a) ;
r2=(-b-sqrt (D) )/ (2*a) ;
fprintf( ' The equation has two real roots , ')
fprintf( ' %.3f and %.3f n' ,r1 ,r2)
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.