% C. Talarico % file: improvedquadratic.m clear all; close all; clc; a = 1 ; b = 45000; c = 1; Q_square = a*c/b^2 format shortE; % set display format fprintf('--------------------------------\n'); fprintf('roots using standard format\n'); x1 = (- b + sqrt(b^2 - 4*a*c))/2 x2 = (- b - sqrt(b^2 - 4*a*c))/2 fprintf('--------------------------------\n'); fprintf('roots using improved format\n'); x1 = -b/2/a*(1 - sqrt(1-4*a*c/b^2)) x2 = -b/2/a*(1 + sqrt(1-4*a*c/b^2)) fprintf('--------------------------------\n'); fprintf('roots using approximation\n'); x2 = -b/a x1 = (c/a)/x2 fprintf('--------------------------------\n'); fprintf('roots using symbolic matlab\n'); syms x; y = a*x^2+b*x+c; x12 = solve(y==0, x); x1 = double(vpa(x12(1))) x2 = double(vpa(x12(2))) fprintf('--------------------------------\n'); % evaluating quality of approx Q = 0:0.01:0.5; F = 1 + (1 - 4*Q.^2).^0.5; figure(1); plot(Q,F,'linewidth',1) ax = gca; set(ax, 'Fontsize',14, 'FontName', 'Monospace', 'Fontweight', 'Normal'); xlabel('Q','fontsize',16,'fontname','Monospace','fontweight','Bold'); ylabel('F','fontsize',16,'fontname','Monospace','fontweight','Bold'); figure(2); E = 100*(2 - F) ./ F; plot(Q,E,'linewidth',2, 'color','r') ax = gca; set(ax, 'Fontsize',14, 'FontName', 'Monospace', 'Fontweight', 'Normal'); xlabel('Q','fontsize',16,'fontname','Monospace','fontweight','Bold'); title('% Error = 100 x (Approx Value - Exact Value)/Exact Value', ... 'Fontsize',18); Q1 = 0.3; F1 = 1 + (1 - 4*Q1^2)^0.5; E1 = 100*(2 - F1)/F1; e_str = sprintf(' %% Error for Q \\leq 0.3 is \\leq %0.2f %%', E1); leg = legend(e_str,'location','Best'); set(leg,'Box','off', 'fontsize',16);