You are on page 1of 2

1.

Create a function to find the roots of a quadratic equation


function r=myroots(p);
c=coeff(p,0);
b=coeff(p,1);
a=coeff(p,2);
r(1)=(-b+sqrt(b^2-4*a*c))/(2*a);
r(2)=(-b-sqrt(b^2-4*a*c))/(2*a);
endfunction

-->exec('C:\Users\L-7\Documents\act 2 1.sce', -1)


Warning : redefining function: myroots

2.

Determine the values of the roots as expected, using naive method & scilab method & find the error
a. p=poly([-0.0001 10000.0 0.0001],"x","coeff");
e1=1e-8;
roots1=myroots(p);
r1=roots1(1);
roots2=roots(p);
r2=roots2(1);
error1=abs(r1-e1)/e1;
error2=abs(r2-e1)/e1;
printf("Expected:%e\n",e1);
printf("Naive method:%e(error=%e)\n",r1,error1);
printf("Scilab method:%e(error=%e)\n",r2,error2);
-->exec('C:\Users\L-7\Documents\act2 2.sce', -1)
Expected:1.000000e-08
Naive method:9.094947e-09(error=9.050530e-02)
Scilab method:-1.000000e+08(error=1.000000e+16)
b. e=1.e-155;
a=1;
b=1/e;
c=1;
p=poly([c b a],"x","coeff");
expected=[-e;-1/e];
roots1=myroots(p);
roots2=roots(p);
error1=abs(roots1-expected)/norm(expected);
error2=abs(roots2-expected)/norm(expected);
printf("Expected:%e%e\n",expected(1),expected(2));
printf("Naive method:%e%e(error=%e%e)\n",roots1(1),roots1(2),error1(1),error1(2));
printf("Scilab method:%e%e(error=%e%e)\n",roots2(1),roots2(2),error2(1),error2(2));
-->exec('C:\Users\L-7\Documents\act 2 3.sce', -1)
Expected:-1.000000e-155-1.000000e+155
Naive method:Inf-Inf(error=InfInf)
Scilab method:-1.000000e+155-1.000000e-155(error=1.000000e+001.000000e+00)

3.

Why is 0.1 rounded


a. format(25)

x1=0.1

x1 =0.1000000000000000055511

x2=1.0-0.9

x2 = 0.0999999999999999777955

error=(x1-x2)

error = 0.0000000000000000277556

relativeerror=(x1-x2)/x1

relativeerror = 0.0000000000000002775558

x1==x2

ans = F

b. format(25)
x=0.1

x =0.1000000000000000055511

p=53

p =53.

e=floor(log2(x))

e =- 4.

M=x/2^(e-p+1)

M =7205759403792794.

x1=720579403792793*2^(-4-53+1)

x1 = 0.0100000480645178330130

x2=720579403792794*2^(-4-53+1)

x2 =0.0100000480645178468908

0.1-x1

ans = 0.0899999519354821725381

0.1-x2

ans =0.0899999519354821586603

You might also like