MATLAB function to obtain the roots of the quadratic equation | applied electronics engineering

Latest

MATLAB function to obtain the roots of the quadratic equation

By Applied Electronics - Wednesday, March 15, 2017 No Comments
Continuing our example of functions in Matlab programming language we want to show here how one can use Matlab function to obtain the roors of a quadratic equations.

A simpler example of using function was illustrated in an example of function in Matlab tutorial and the syntax and more detailed explanation of Matlab function was explained in user defined and inbuilt function in Matlab programming language.

To find the roots of a quadratic equation using Matlab function we will use a matlab script as follows.

function rt = sqrtquad(coeff)

a = coeff(1);
b = coeff(2);
c = coeff(3);
num = b^2 - 4*a*c;
if int > 0
srint = sqrt(num);

x1= (-b + srint)/(2*a);
x2= (-b - srint)/(2*a);

elseif int == 0
x1= -b/(2*a);
x2= x1;

elseif num < 0
srint = sqrt(-num);
p1 = -b/(2*a);
p2 = srint/(2*a);
x1 = p1+p2*j;
x2 = p1-p2*j;
end
rt =[x1; x2];
end

In the above functions we have made use of if else statement in Matlab programming language. See comparison if statement in C, Matlab and Python programming languages.

No Comment to " MATLAB function to obtain the roots of the quadratic equation "