1 dop = ###; % Specify the number of digits of precision. a = 2; b = ###; 5 C-5; 6 7 % Compute the quadratic solutions x1 and x2 using double precision. 8 x1 (b+ sqrt(b^2-4*a*c))/###; 9 x2 (-b sqrt(b^2-4*a*c))/###; 10 fprintf('Using double precision, x1 = %f and x2 = %f\n',x1,###) 11 12 % Test if parts of the quadratic solutions might be sensitive to 13 % catastrophic cancellations. 14 if abs(-b+sqrt(b^2-4*a*c)) < 0.1*b 15 trust flag = 1; %% possible issue with x1 % b>0 16 elseif abs(-b-sqrt(b^2-4*a*c)) < abs(0.1*###) % b<0 17 18 else 19 20 end 21 trustflag = 2; %% possible issue with x2 trustflag = 0; %% no issue 22 % Redo the calculations using the specified digits if precision. 23 % The numdop function is defined at the bottom. 24 a numdop(a, dop); 25 bnumdop (b,dop); 26 c = numdop(###); 27 % Every operation must be done with the specified digits of precision. 28 bsq numdop(b^2, dop); 29 fac numdop (numdop (4*a, dop) *c, dop); 30 bsqmfac = numdop (bsq-fac, dop); 31 sqrtterm = numdop(sqrt(###), dop); 32 numer1 = numdop(-b+sqrtterm, dop); 33 numer2numdop(-b-sqrtterm, dop); 34 denom numdop (2*a, dop); 35 36 x1dop numdop (###, dop); 37 x2dop numdop (###, dop); 38 fprintf('Using %d digits of precision, x1 = %f and x2 = %f\n', dop, x1dop,x2dop) 39 40 if trustflag == 1 41 42 43 x1m numdop (numdop (c/a, dop)/x2dop, dop); fprintf(['However, x1 is susceptible to catastrophic cancellations' ... and the modified solution x1 = %f should be taken.\n'],x1m) 44 elseif trustflag == 2 45 46 47 48 else 49 50 end x2m = numdop (numdop (c/a, dop) ###, dop); fprintf(['However, x2 is susceptible to catastrophic cancellations' ... and the modified solution x2 = %f should be taken.\n'], ###) disp('No modification of x1 or x2 is required') 51 52 53 % The numdop function rewrites num as 0.#####e# which is equivalent to 54 % dec x 10^pow. With dop holding the specified number of digits of 55 % precision, dec is multiplied by 10^dop, rounded, then mutiplied by 56 % 10^-dop to return it its original format. This new number is expressed 57 % with the appropriate digits of precision. 58 % To find the correctly formatted version of num, it is multiplied by a 59 % vector of powers of ten. The resultant vector of products is then 60 % searched to find the last entry less than 1. This gives the decimal part 61 % and its location in the vector gives the power. 62 function newnum = numdop (num, dop) 63 p = -8:8; 64 tempnum num * 10.^p; 65 ind find (abs (tempnum)<1); 66 dec = tempnum(ind (end)); 67 pow = -p(ind(end)); 68 newnum = round(dec*10^(dop))/10^(dop-pow); 69 end 70 ► Run Script ?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

--- MATLAB work for catastrophic cancellations ---

--- Please answer the question in the attached image by replacing the ### with matlab script. The equation to be solved is 2x2 - 487x - 5 and 3 digits of precision is required.  ---

--- Thank you in adavance ---

1 dop = ###; % Specify the number of digits of precision.
a = 2;
b = ###;
5 C-5;
6
7 % Compute the quadratic solutions x1 and x2 using double precision.
8 x1
(b+ sqrt(b^2-4*a*c))/###;
9 x2 (-b sqrt(b^2-4*a*c))/###;
10 fprintf('Using double precision, x1 = %f and x2 = %f\n',x1,###)
11
12 % Test if parts of the quadratic solutions might be sensitive to
13 % catastrophic cancellations.
14 if abs(-b+sqrt(b^2-4*a*c)) < 0.1*b
15
trust flag = 1; %% possible issue with x1
% b>0
16 elseif abs(-b-sqrt(b^2-4*a*c)) < abs(0.1*###) % b<0
17
18 else
19
20 end
21
trustflag = 2; %% possible issue with x2
trustflag = 0; %% no issue
22 % Redo the calculations using the specified digits if precision.
23 % The numdop function is defined at the bottom.
24 a
numdop(a, dop);
25 bnumdop (b,dop);
26 c = numdop(###);
27 % Every operation must be done with the specified digits of precision.
28 bsq numdop(b^2, dop);
29 fac numdop (numdop (4*a, dop) *c, dop);
30 bsqmfac = numdop (bsq-fac, dop);
31 sqrtterm = numdop(sqrt(###), dop);
32 numer1 = numdop(-b+sqrtterm, dop);
33 numer2numdop(-b-sqrtterm, dop);
34 denom numdop (2*a, dop);
35
36 x1dop numdop (###, dop);
37 x2dop
numdop (###, dop);
38 fprintf('Using %d digits of precision, x1 = %f and x2 = %f\n', dop, x1dop,x2dop)
39
40 if trustflag == 1
41
42
43
x1m numdop (numdop (c/a, dop)/x2dop, dop);
fprintf(['However, x1 is susceptible to catastrophic cancellations' ...
and the modified solution x1 = %f should be taken.\n'],x1m)
44 elseif trustflag == 2
45
46
47
48
else
49
50 end
x2m = numdop (numdop (c/a, dop) ###, dop);
fprintf(['However, x2 is susceptible to catastrophic cancellations' ...
and the modified solution x2 = %f should be taken.\n'], ###)
disp('No modification of x1 or x2 is required')
51
52
53 % The numdop function rewrites num as 0.#####e# which is equivalent to
54 % dec x 10^pow. With dop holding the specified number of digits of
55 % precision, dec is multiplied by 10^dop, rounded, then mutiplied by
56 % 10^-dop to return it its original format. This new number is expressed
57 % with the appropriate digits of precision.
58 % To find the correctly formatted version of num, it is multiplied by a
59 % vector of powers of ten. The resultant vector of products is then
60 % searched to find the last entry less than 1. This gives the decimal part
61 % and its location in the vector gives the power.
62 function newnum = numdop (num, dop)
63
p = -8:8;
64
tempnum num * 10.^p;
65
ind find (abs (tempnum)<1);
66
dec = tempnum(ind (end));
67
pow = -p(ind(end));
68
newnum = round(dec*10^(dop))/10^(dop-pow);
69 end
70
► Run Script ?
Transcribed Image Text:1 dop = ###; % Specify the number of digits of precision. a = 2; b = ###; 5 C-5; 6 7 % Compute the quadratic solutions x1 and x2 using double precision. 8 x1 (b+ sqrt(b^2-4*a*c))/###; 9 x2 (-b sqrt(b^2-4*a*c))/###; 10 fprintf('Using double precision, x1 = %f and x2 = %f\n',x1,###) 11 12 % Test if parts of the quadratic solutions might be sensitive to 13 % catastrophic cancellations. 14 if abs(-b+sqrt(b^2-4*a*c)) < 0.1*b 15 trust flag = 1; %% possible issue with x1 % b>0 16 elseif abs(-b-sqrt(b^2-4*a*c)) < abs(0.1*###) % b<0 17 18 else 19 20 end 21 trustflag = 2; %% possible issue with x2 trustflag = 0; %% no issue 22 % Redo the calculations using the specified digits if precision. 23 % The numdop function is defined at the bottom. 24 a numdop(a, dop); 25 bnumdop (b,dop); 26 c = numdop(###); 27 % Every operation must be done with the specified digits of precision. 28 bsq numdop(b^2, dop); 29 fac numdop (numdop (4*a, dop) *c, dop); 30 bsqmfac = numdop (bsq-fac, dop); 31 sqrtterm = numdop(sqrt(###), dop); 32 numer1 = numdop(-b+sqrtterm, dop); 33 numer2numdop(-b-sqrtterm, dop); 34 denom numdop (2*a, dop); 35 36 x1dop numdop (###, dop); 37 x2dop numdop (###, dop); 38 fprintf('Using %d digits of precision, x1 = %f and x2 = %f\n', dop, x1dop,x2dop) 39 40 if trustflag == 1 41 42 43 x1m numdop (numdop (c/a, dop)/x2dop, dop); fprintf(['However, x1 is susceptible to catastrophic cancellations' ... and the modified solution x1 = %f should be taken.\n'],x1m) 44 elseif trustflag == 2 45 46 47 48 else 49 50 end x2m = numdop (numdop (c/a, dop) ###, dop); fprintf(['However, x2 is susceptible to catastrophic cancellations' ... and the modified solution x2 = %f should be taken.\n'], ###) disp('No modification of x1 or x2 is required') 51 52 53 % The numdop function rewrites num as 0.#####e# which is equivalent to 54 % dec x 10^pow. With dop holding the specified number of digits of 55 % precision, dec is multiplied by 10^dop, rounded, then mutiplied by 56 % 10^-dop to return it its original format. This new number is expressed 57 % with the appropriate digits of precision. 58 % To find the correctly formatted version of num, it is multiplied by a 59 % vector of powers of ten. The resultant vector of products is then 60 % searched to find the last entry less than 1. This gives the decimal part 61 % and its location in the vector gives the power. 62 function newnum = numdop (num, dop) 63 p = -8:8; 64 tempnum num * 10.^p; 65 ind find (abs (tempnum)<1); 66 dec = tempnum(ind (end)); 67 pow = -p(ind(end)); 68 newnum = round(dec*10^(dop))/10^(dop-pow); 69 end 70 ► Run Script ?
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education