Use MATLAB codes to answer the following questions B to compute the roots of a q
ID: 3111567 • Letter: U
Question
Use MATLAB codes to answer the following questions B to compute the roots of a quadratic function as In this problem, you will use MATLA accurately as possible. 1) a) Write a MATLAB function that computes the roots of f(x)-a+h+c, where a, b, and c are real constants. Write the function call as xlx2-quadroots(a,b.c) to minimize the effect of loss of significance, where xl and x2 are the two roots. Make sure 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 b2 4ac function can also assume that Page 2 of 3 b) Write a script file that uses quadroots to compute the roots for both the quadratic functions f(x)--10x +1 and (r)-x10'x+1, and tests the function by computing both fCx) and |f(x2) for both quadratic functions, where xI and x2 are the outputs of function quadroots. Also, compute L(x) andC2) for both quadratic functions after computing x/ and x2 from the quadratic formula, i.e., where xl,x2 2a Compare your results. Note that |f (x2)) 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 Write a script m-file that calls the function nest) to evaluate P(-2), where P(x)is defined in Problem 1 of the Theoretical Problems. (the base nestO is provided in the MATLAB Code folder in FL17 or CS 4334.701-FL17. Note that using nesto may require a different n from your result in Problem 1, but use as few flops as possible when applying nest 2) You do not have to pass the 4 argument to the function points)-just the first 3 arguments. Run your script and give the output. The function course Math 4334.701-Explanation / Answer
A.
MATLAB function that computes the roots of f(x) = ax2 + bx + c, where a, b, and c are real constants
PROGRAM:
function [r1r,r1i,r2r,r2i] = quadric ( a,b,c,opt );
% -------------------- implementation ----------------------
small = 0.00000001;
r1r = 0.0;
r1i = 0.0;
r2r = 0.0;
r2i = 0.0;
discrim = b*b - 4.0 *a*c;
%a
%b
%c
%discrim
% --------------------- real roots --------------------------
if ( abs(discrim) < small )
r1r = -b / ( 2.0 *a );
r2r = r1r;
% if (opt=='U')
% r2r = 99999.9;
% end;
else
if abs(a) < small
r1r = -c/b;
else
if ( discrim > 0.0 )
r1r = ( -b + sqrt(discrim) ) / ( 2.0 *a );
r2r = ( -b - sqrt(discrim) ) / ( 2.0 *a );
else
% ------------------ complex roots --------------------
if (opt=='I')
r1r = -b / ( 2.0 *a );
r2r = r1r;
r1i = sqrt(-discrim) / ( 2.0 *a );
r2i = -sqrt(-discrim) / ( 2.0 *a );
else
r1r = 99999.9;
r2r = 99999.9;
end;
end;
end;
end;
b.
function r = quadroot(p)
% Check array class.
if ~isnumeric(p)
error('P must be a numeric array.');
end
% Check array size.
if (ndims(p) ~= 2) | (size(p, 1) ~= 1)
error('P must be row vector.');
end
a = p(1);
b = p(2);
c = p(3);
if a == 0
if b == 0
if c == 0
%
% a, b, c are zero: 0 = 0
%
error('All coefficients are zero');
else
%
% a, b are zero: c = 0;
%
error('Constant equation');
end
else
if c == 0
%
% a, c are zero: b*x = 0
%
r = 0;
return;
else
%
% a is zero: b*x + c = 0
%
r = -c/b;
return;
end
end
else
if b == 0
if c == 0
%
% b, c are zero: a*x^2 = 0
%
r = [0 0];
return;
else
%
% b is zero: a*x^2 + c = 0
%
t = sqrt(c/a);
r = [t -t];
return;
end
else
if c == 0
%
% c is zero: a*x^2 + b*x = 0;
%
r = [0 -b/a];
return;
else
%
% none of a, b, c are zero: a*x^2 + b*x + c = 0
%
d = b*b - 4*a*c; % discriminant
if d == 0
% two identical roots
t = -b/(2*a);
r = [t t];
return;
else
if sign(real(sqrt(d))) == sign(real(b))
t = -b - sqrt(d);
else
t = -b + sqrt(d);
end
r = [ t/(2*a) (2*c)/t ];
return;
end
end
end
end
% we should never get here
error('Internal error');
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.