Redwon 201529
Redwon 201529
Id:-201529
Assignment on quadratic equation using MATLAB
clc;
clear all;
numEquations = input('How many quadratic equations do you want to
input? ');
coefficients = zeros(numEquations, 3);
for i = 1:numEquations
fprintf('\nEquation %d:\n', i);
a = input('Enter coefficient a: ');
b = input('Enter coefficient b: ');
c = input('Enter coefficient c: ');
coefficients(i, :) = [a, b, c];
end
eqNum = input(['\nEnter the equation number (1 to ',
num2str(numEquations), ') you want to solve: ']);
if eqNum < 1 || eqNum > numEquations
disp('Invalid equation number.');
else
% Get the coefficients
a = coefficients(eqNum, 1);
b = coefficients(eqNum, 2);
c = coefficients(eqNum, 3);
if a == 0
disp('This is not a quadratic equation (a = 0).');
else
D = b^2 - 4*a*c;
fprintf('\nSolving equation %dx^2 + %dx + %d = 0\n', a,
b, c);
if D > 0
root1 = (-b + sqrt(D)) / (2*a);
root2 = (-b - sqrt(D)) / (2*a);
fprintf('Real and distinct roots: %.4f and %.4f\n',
root1, root2);
elseif D == 0
root = -b / (2*a);
fprintf('Real and equal roots: %.4f\n', root);
else
realPart = -b / (2*a);
imagPart = sqrt(-D) / (2*a);
fprintf('Complex roots: %.4f + %.4fi and %.4f -
%.4fi\n', realPart, imagPart, realPart, imagPart);
end
end
end
Equation 1:
Enter coefficient a: 1
Enter coefficient b: 5
Enter coefficient c: 6
Equation 2:
Enter coefficient a: 4
Enter coefficient b: 5
Enter coefficient c: 6
Equation 3:
Enter coefficient a: 1
Enter coefficient b: 5
Enter coefficient c: 4
Equation 4:
Enter coefficient a: 5
Enter coefficient b: 5
Enter coefficient c: 21
Equation 5:
Enter coefficient a: 4
Enter coefficient b: 1
Enter coefficient c: 5