[ringland@orange 337]$ maple quiz3_solution.txt |\^/| Maple 11 (IBM INTEL LINUX) ._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2007 \ MAPLE / All rights reserved. Maple is a trademark of <____ ____> Waterloo Maple Inc. | Type ? for help. # Maple # quiz3_solution.txt > > print( "Problem 1" ); "Problem 1" # Largest number obtained with all mantissa bits 1, # and exponent code 111111111111110 > > max_expon_code := 2^15-2; max_expon_code := 32766 > max_expon := max_expon_code - 16383; max_expon := 16383 > largest := evalf(2*2^max_expon,2); 4933 largest := 0.12 10 > > machine_epsilon := evalf( 2^(-64), 2); -19 machine_epsilon := 0.54 10 > > print( "Problem 2" ); "Problem 2" > # Complete the function "solve_smart" # and show that it is more accurate than # "solve_naive" by using it on the example below # with the precision set to 2 decimal digits > > a:=21; a := 21 > b:=3100; b := 3100 > c:=-150; c := -150 > my_quadratic := a*x^2+b*x+c; 2 my_quadratic := 21 x + 3100 x - 150 > > a := evalf(a); a := 21. > b := evalf(b); b := 3100. > c := evalf(c); c := -150. > > solve_naive:=proc(a,b,c) > return [ (-b + sqrt(b^2-4*a*c))/(2*a) , > (-b - sqrt(b^2-4*a*c))/(2*a) ]; > end proc: > > solve_smart:=proc(a,b,c) > local xp,xm; > xp := (-b + sqrt(b^2-4*a*c))/(2*a); > xm := (-b - sqrt(b^2-4*a*c))/(2*a); > > ########################### part to be supplied > if b = 0 then > return [xm,xp]; > elif b < 0 then > return [xp,c/(a*xp)]; > else > return [xm,c/(a*xm)]; > end if; > ########################### > end proc: > > solve_exact:=solve(my_quadratic,x); 1/2 1/2 1550 5 96226 1550 5 96226 solve_exact := - ---- + ----------, - ---- - ---------- 21 21 21 21 > evalf(solve_exact,10); 0.04837124, -147.6674189 > > Digits := 2; ############## part to be supplied Digits := 2 > solve_naive(a,b,c); [0., -150.] > solve_smart(a,b,c); [-150., 0.047] > > quit bytes used=1053500, alloc=851812, time=0.04 [4]- Done gedit $1 [ringland@orange 337]$ We see that our "smart" algorithm gets 2 digits almost correct for the root closer to 0, while the naive algorithm gets no digits at all. (I am curious why we are off by 1 in the second digit, while Maple's algorithm "solve" manages to get both digits correct. I have no answer at present.)