Let us solve a second order algebraic equation ar2 + bx+c = 0. The solution is g
ID: 2292756 • Letter: L
Question
Let us solve a second order algebraic equation ar2 + bx+c = 0. The solution is given in analytical form as x== - -h+ Vb2 - 4 * a*C 2 *a If the argument of the radical, i.e. b2 – 4 % a*c is negative the two roots are complex conjugates, if b) – 4 *a*c is zero the two roots are repeated, and if b2 – 4 **a*c is postive then the two roots are real. Write a function named (r1,r2,msg ] = sec_root(a,b,c) The argument = b2 - 4ac is used to determine the roots.... if the argument is negative return the roots n = -b+jv-argument) and re= (-b-jV-argument) and a message string 'The roots are complex D conjugates' if the argument equals zero return n1 = and a message string 'The roots are repeated' otherwise, r = Sar - (- + vargument) andre - (-b+ vargument) 2 and a message string The roots are real'Explanation / Answer
Matlab Code:
%Save this file in one matlab file ex: rootss.m
%Create a new matlab file with function name sec_root.m
clc
clear all
close all
%Quadratic equation is ax^2 + bx + c = 0
a = 1;
b = 1;
c = 1;
%Calling function to evaluate roots of quadratic equation
[R1, R2, M] = sec_root(a,b,c);
disp(M); %Displaying message
disp('Roots are:') %Displaying roots
disp([R1 R2])
%End of rootss.m file
%Start of function
%Create this function in seperate matlab file with name sec_root.m
function [R1, R2, M] = sec_root(a,b,c)
argmnt = b^2 - 4*a*c; %Calculating argument to decide whether it is real or complex
if(argmnt<0) %Complex conjugate case
M = 'Roots are complex conjugate';
R1 = -b/(2*a) + j*sqrt(-argmnt);
R2 = -b/(2*a) - j*sqrt(-argmnt);
else if (argmnt == 0) %Real roots and equal case
M = 'Roots are real and equal';
R1 = -b/(2*a);
R2 = R1;
else % Real roots and different
M = 'Roots are real and different';
R1 = -b/(2*a) + sqrt(argmnt);
R2 = -b/(2*a) - sqrt(-argmnt);
end
end
end
%End of function
Output:
Roots are complex conjugate
Roots are:
-0.5000 + 1.7321i -0.5000 - 1.7321i
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.