Question
In this problem, you will use MATLAB to compute the roots of a quadratic function as accurately as possible. a) As indicated at the bottom
In this problem, you will use MATLAB to compute the roots of a quadratic function as accurately as possible.
a) As indicated at the bottom of the editor, 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 [x1,x2] = quadroots(a,b,c) where x1 and x2 are the two roots. As discussed in class, 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) As indicated in the editor above your function, write script that calls quadroots to compute the roots for both the quadratic functions 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+- sqrt(b^2 -4ac)/2a. 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.
format long e
% define coefficients for the version of f(x) where b
a =
b =
c =
%call the function quadroots
%call the outputs, x1n and x2n, in that order.
%compute backward errors for the root approximations from quadroots
backx1n =
backx2n=
%compute root approximations directly from the quadratic formula
%call the larger one x1ne, and the smaller x2ne
x1ne =
x2ne =
%compute the backward errors for the root estimates from the quadratic formula
backx1ne =
backx2ne =
% define coefficients for the version of f(x) where b > 0
a =
b =
c =
%call the function quadroots
%call the outputs, x1p and x2p, in that order.
%compute backward errors for these root approximations from quadroots
backx1p =
backx2p =
%compute root approximations directly from the quadratic formula
%call the larger one x1pe, and the smaller x2pe
x1pe =
x2pe =
%compute the backward errors for the root esimates from the quadratic formula
backx1pe =
backx2pe =
%below, write the function, quadroots, called by the script above
% input: coefficients of ax^2+bx+c
% output: roots x1, x2
In this problem, you will use MATLAB to compute the roots of a quadratic function as accurately as possible. a) As indicated at the bottom of the editor, write a MATLAB function that computes the roots of f(x) = ar? + brto = ax' +bx+c, where a, b, and c are real constants. Write the function call as [x1,x2] = quadroots(a,b,c) Download Page 3 of 4 where xl and x2 are the two roots. As discussed in class, 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? > 4ac. b) As indicated in the editor above your function, write script that calls quadroots to compute the roots for both the quadratic functions f(x) = x 10 x +1 and f(x) = x +10x +1, and tests the function by computing both f (x1) and f (x2) for both quadratic functions, where xl and x2 are the outputs of the function quadroots. Also, compute f (x1) and If (x2) for both quadratic functions after computing xl and x2 from the quadratic formula, 2-b61b2 - 4ac ". Note that|f(xl) | and f (x2) | are called backward i.e., where xl, x2 = 2a errors, since they show how well the solutions "solve the original problem instead of giving the actual error. Script Reset MATLAB Documentatic 1 format long e % define coefficients for the version of f(x) where b 0 31 a = 32 b = %call the function quadroots %call the outputs, x1p and x2p, in that order. %compute backward errors for these root approximations from quadroots 41 backx1p = backx2p = %compute root approximations directly from the quadratic formula %call the larger one x1pe, and the smaller x2pe x1pe = x2pe = compute the backward errors for the root esimates from the quadratic formula backx1pe = backx2pe = %below, write the function, quadroots, called by the script above 57 % input: coefficients of ax^2+bx+c 58 % output: roots x1, x2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started