Write a function file that accepts the coefficients a b c fo
Write a function file that accepts the coefficients a, b, c (for the quadratic equation). Set up a code that evaluates b^2 - 4ac to determine if it will return a real value. If b^2 - 4ac is negative, display an error that tells the user that the root is imaginary and abort the rest of the program. If b^2 - 4ac is zero or positive, calculate the roots and display them. Test your code on the following quadratic equations: x^2 - x + 12 4x^2 + 13x - 17 2x^2 - 4x + 2 Write a function file that determines the following sum (for all odd n from 1 to 11) using a for loop. The starting point, ending point, and increment should be input to the function file. y = sigma 2^n+2/n - 2 Repeat number 3 using a while loop.
Solution
(2)
MATLAB code:
function [r1,r2] = roots( a,b,c )
x=power(b,2)-4*a*c;
if x<=0
r1=-b+sqrt(x/2)
r2=-b-sqrt(x/2)
end
end
sample outputs
>> roots(1,-1,12)
r1 =
1.0000 + 4.8477i
r2 =
1.0000 - 4.8477i
>> roots(4,13,-17)
result is zero or negative
>> roots(2,-4,2)
r1 =
4
r2 =
4
