Write two functions that return the two solutions to the quadratic equation (ax^2) + bx + c
Question:
Write two functions that return the two solutions to the
quadratic equation (ax^2) + bx + c = 0. You can write something like
function [p,q] = quadratic Formula (a,b, c)
For the first function, assign
p = [-b+ sqrt((b^2) - 4ac)]/2a , q = [-b-sqrt((b^2) -4ac)]/2a
Make sure the code works for some trial values of a, b, and c. At least expect
the solutions of a quadratic equation (x^2) - 3x + 2 = 0 are p = 2 and q = 1.
Then try your code for a = 1 b = (-10^12), c = 1. Note that q = 0, which is obviously not a correct root. This is an example of round-off error.
For the second function, assign
p = [-b + sqrt((b^2) -4ac]/2a , q = 2c/ -b+sqrt((b^2)-4ac) , (b < 0) , and
p = 2c/( -b-sqrt((b^2)-4ac)) , q = (-b - sqrt((b^2) -4ac)/2a , (b >= 0)
Again, make sure the code works for some trial values of a, b, and c. Now
try this code for a = 1, b = -10^12 , c = 1 to obtain more reasonable results.
Microeconomics An Intuitive Approach with Calculus
ISBN: 978-0538453257
1st edition
Authors: Thomas Nechyba