Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Engineering Computing
b b 2 4ac
Roots set by discriminant x
2a
Δ < 0 (no real roots)
Δ = 0 (one real root)
Δ > 0 (two real roots) b 4ac
2
clear all; % clear all variables
clc; % clear screen
Header
coeffts = input('Enter values for a,b,c (as a vector): '); % Read in equation coefficients
a = coeffts(1);
b = coeffts(2);
c = coeffts(3);
if delta < 0
Calculate Δ
fprintf('\nEquation has no real roots:\n\n')
disp(['discriminant = ', num2str(delta)])
elseif delta == 0
fprintf('\nEquation has one real root:\n')
xone = -b/(2*a)
else
Make decisions
fprintf('\nEquation has two real roots:\n')
based on value of Δ
x(1) = (-b + sqrt(delta))/(2*a);
x(2) = (-b – sqrt(delta))/(2*a);
fprintf('\n First root = %10.2e\n\t Second root = %10.2f', x(1),x(2))
end
Conclusions
MATLAB is more than a calculator
– its a powerful programming environment
Have reviewed:
– Concept of an M-file
– Decision making in MATLAB
– IF … END and IF … ELSEIF … ELSE … END
– Example of real roots for quadratic equation
– Same structure as Week 4 tutorial problem