Write a MATLAB function that can be used to solve a quadratic equation if answer
ID: 3141907 • Letter: W
Question
Write a MATLAB function that can be used to solve a quadratic equation if answers are real
numbers (you have to do following steps):
ax^2 - 3bx + 4c = 0
. The inputs to the function are the three coefficients a, b, and c.
. Check if the equation has real solutions. If there are no real solution the program should display
‘THERE ARE NO REAL SOLUTIONS.
. Otherwise, calculate the solutions and display them as shown in the following example ( use the
fprintf command showing each solution with 4 decimal points). As example: ‘The solutions are -3.4135 and 9.7435’
.Try to solve two quadratic equations with coefficients: [a, b, c] = [ 1 , 1, -4 ] and [a,b, c] = [ 5 -1 3]
Submit the m file for your function, and the results of both test runs done in MATLAB (again copy and paste them in the word file)
Explanation / Answer
% This m-file will solve the quadratic equation :ax^2 - 3bx + 4c = 0
function Quad= Quadratic(A,B,C)
% A=1;
% B=1;
% C=-4;
%If-Else statement for if A=0
if A==0,
X= -C/B;
else
X(1) = (-B+sqrt(B^2-4*A*C))/(2*A);
X(2) = (-B-sqrt(B^2-4*A*C))/(2*A);
disp(X(1))
disp(X(2))
fprintf('X(1) = %f ',X(1));
fprintf('X(2) = %f ',X(2));
% This m-file will solve the quadratic equation :ax^2 - 3bx + 4c = 0
function Quad= Quadratic(A,B,C)
% A=5;
% B=-1;
% C=3;
%If-Else statement for if A=0
if A==0,
X= -C/B;
else
X(1) = (-B+sqrt(B^2-4*A*C))/(2*A);
X(2) = (-B-sqrt(B^2-4*A*C))/(2*A);
disp(X(1))
disp(X(2))
fprintf('X(1) = %f ',X(1));
fprintf('X(2) = %f ',X(2));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.