0% found this document useful (0 votes)
7 views3 pages

Redwon 201529

The document outlines a MATLAB assignment for solving quadratic equations. It includes a script that prompts the user to input coefficients for multiple equations and then solves a selected equation, displaying the roots based on the discriminant. An example is provided where five equations are inputted, and the roots for the third equation are calculated and displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Redwon 201529

The document outlines a MATLAB assignment for solving quadratic equations. It includes a script that prompts the user to input coefficients for multiple equations and then solves a selected equation, displaying the roots based on the discriminant. An example is provided where five equations are inputted, and the roots for the third equation are calculated and displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name:-Redwon Ahmed

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

How many quadratic equations do you want to input? 5

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

Enter the equation number (1 to 5) you want to solve: 3

Solving equation 1x^2 + 5x + 4 = 0


Real and distinct roots: -1.0000 and -4.0000

You might also like