Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an MATLAB function (M-file) to solve a second-order polynomial: Ax2 + Bx +

ID: 3784004 • Letter: W

Question

Write an MATLAB function (M-file) to solve a second-order polynomial:

Ax2 + Bx + C = 0

Request input from the user (three numbers: A, B, and C).

Check for real/imaginary roots

Solve mathematically and store the solutions in variables x1 and x2

Return the solutions to the user (x1, x2)

Remember: annotation and comments are very important in the understanding of your code by another (a grader or faculty member) and you will be graded by the functionality of your code AND the quality of your code, including annotation.

Please don't forget to add comment, so any one read the program should understand your thought.

Explanation / Answer

%File: Quadratic_roots.m

%Purpose:
%This program finds the roots of the quadratic equation. It determines
%wether the roots are real or imaginery. It outputs regardless of the type
%of roots that the equation posses.
%
%Variables:
%A - Coefficient of x^2 term.
%B - Coefficient of x term.
%C - Constant term
%Discriminant - Discriminant of equation.
%x1 - First Solution.
%x2 - Second Solution

%Determining Input from the user
disp('This program determines the roots of equation');
disp('Equation is of type A*x^2+ B*x+C=0.');
A=input('Enter the coefficient A:');
B=input('Enter the coefficient B:');
C=input('Enter the constant coeffecient C:');

%Discriminant Equation.
Discriminant=B^2-4*A*C;

%Check if it is real or imaginary roots.
if(Discriminant>0)
disp('Roots are Real');
elseif(Discriminant==0)
disp('Roots are Real and Equal');
else
disp('Roots are Imaginery');
end;

%Roots of the equation are:
x1= (-B + sqrt(Discriminant))/(2*A);
x2= (-B - sqrt(Discriminant))/(2*A);

%Result
disp('The roots of the equation are:');
fprintf('x1= (%f) +i (%f) ', real(x1), imag(x1));
fprintf('x1= (%f) +i (%f) ', real(x2), imag(x2));

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote