Key for Exam 1, Mat360, Spring, 2015

Problem 1:

error[x_, xhat_]:=xhat-x
relativeError[x_, xhat_]:=error[x,xhat]/x
re=relativeError[x,xhat]
relativeError[Exp[x],Exp[xhat]]

RoundIt[x_,m_]:=Module[{n=Floor[Log[10,Abs[x]]]},
(* Print[N[10^(n-m+1)*Round[10^(m-1-n)*x]]]; *)
N[10^(n-m+1)*Round[10^(m-1-n)*x]]
]
RoundIt[94939,Table[i,{i,1,5,1}]]
RoundIt[.94939,Table[i,{i,1,5,1}]]

ChopIt[x_,m_]:=Module[{n=Floor[Log[10,Abs[x]]]},
N[10^(n-m+1)*Floor[10^(m-1-n)*x]]
]
ChopIt[94939,Table[i,{i,1,5,1}]]
ChopIt[.94939,Table[i,{i,1,5,1}]]

round=RoundIt[
RoundIt[RoundIt[34/21,3]-RoundIt[8/5,3],3]
/
RoundIt[RoundIt[Pi,3]-RoundIt[Exp[1],3],3]
,3]
chop=ChopIt[
ChopIt[ChopIt[34/21,3]-ChopIt[8/5,3],3]
/
ChopIt[ChopIt[Pi,3]-ChopIt[Exp[1],3],3]
,3]
true=N[(34/21-8/5)/(Pi-Exp[1])]
error[round,true]
error[chop,true]
Print["Rounded 34/21 = ",NumberForm[RoundIt[34/21,3],16] ]; 
Print["Rounded 8/5 = ",NumberForm[RoundIt[8/5,3],16] ]; 
Print["Rounded Pi = ",NumberForm[RoundIt[Pi,3],16] ]; 
Print["Rounded e = ",NumberForm[RoundIt[Exp[1],3],16] ]; 
Print["Rounded = ",round];
Print["RE Rounded = ",relativeError[true,round]];
Print["Chopped 34/21 = ",NumberForm[ChopIt[34/21,3],16] ]; 
Print["Chopped 8/5 = ",NumberForm[ChopIt[8/5,3],16] ]; 
Print["Chopped Pi = ",NumberForm[ChopIt[Pi,3],16] ]; 
Print["Chopped e = ",NumberForm[ChopIt[Exp[1],3],16] ]; 
Print["Chopped = ",chop];
Print["RE chopped = ",relativeError[true,chop]];
ChopIt[.01/.43,3]
(ChopIt[.01/.43,3]-true)/true
RoundIt[.02/.42,3]
(RoundIt[.02/.42,3]-true)/true

Problem 2:

f[x_]:=x*Exp[x]
x0=1
x1=x0-f[x0]/f'[x0]
Plot[{f[x],f[x0]+f'[x0](x-x0)},{x,-3,1.5},PlotRange->{{-3,1.5},{-2,5}}]
(* Two problem points: *)
Plot[{f[x],f[-2]+f'[-2](x-(-2)),-Exp[-1]},{x,-5,1.5},PlotRange->{{-5,1.5},{-2,2}}]

Problem 3:

f[x_]:=(x+2)(x+1)x(x-1)(x-2)
Plot[f[x],{x,-Pi,Pi}]

Bisection[f_,a0_,b0_]:=
Module[{i=1,its=100,eps=.000001},
If[Abs[f[a0]]≤eps,Return[a0]];
If[Abs[f[b0]]≤eps,Return[b0]];
a=N[a0]; (* Slight change -- use a local variable (so as not to alter passed arguments *)
b=N[b0];
fa = f[a];  
fb = f[b];  
If[fa*fb>=0,Return[False]];
width=b-a;
c = a+0.5*width; 
fc=f[c]; 
If[Abs[fc]≤eps, Return[c]];
Print[a," ",c," ",b];
While[a < c && c<b  && i<its,
If[ Sign[fa] ==  Sign[fc], 
a = c;fa=fc, 
b = c;fb=fc
];
width=b-a;
slope =(fb-fa)/width;
i=i+1;
c = a+0.5*width;
fc=f[c];
Print[a," ",c," ",b];
If[Abs[fc]≤eps, Return[c]];
]; 
Return[c];
]

Bisection[f,-4,2.6]

Problem 4:

BaseForm[1.2,2]
BaseForm[0.43,2]
N[(1+1/4)*(1/4+1/8+1/16)]
BaseForm[N[(1+1/4)*(1/4+1/8+1/16)],2]

Problem 5:

4*ArcTan[1]
N[Pi,50]

2048+512+32
BaseForm[2048+512+32,2]
BaseForm[155,2]

f[x_]:=Exp[1/x]
Cond[x_]:=x*f'[x]/f[x]
Cond[x]

f[x_]:=Exp[x]
Simplify[relativeError[f[x],f[x+h]]]

Problem 6:

BisectionDivision[d0_,a0_,b0_] :=
Module[{},
a=N[a0];
b=N[b0];
d=N[d0];
fa = d*a-1;  
fb =  d*b-1;  
c = a+0.5(b-a); 
fc= d*c-1; 
While[a < c && c<b,
If[ Sign[fa] ==  Sign[fc], 
a = c;fa=fc, 
b = c;fb=fc
];
c = a+0.5(b-a);
fc=d*c-1;
]; 
Return[c]
]

Bivision[a0_,b0_]:=
Module[{},
a=N[a0]; 
b=N[b0];
If[b<0,a=-a;b=-b];
If[b>1,Return[a*BisectionDivision[b,0,1]],Return[a*BisectionDivision[b,1,10^(-Floor[Log[10,b]])]]];
]

Bivision[1,3]

f[x_]:=Bivision[1,x]
f[2]
Plot[f[x],{x,.001,2}]
Plot[f[x]-1/x,{x,.001,2}]

f[x_]:=Bivision[Cos[x],x]
Plot[{f[x],Cos[x]/x},{x,0,2Pi}]
Plot[{f[x]-Cos[x]/x},{x,0,2Pi}]

Problem 7:

Clear[x,y,n,m]
diff[y_]:=ChopIt[y-3.142,4]
improvedDiff[x_]:=ChopIt[(x-3.142)-.0005927,4]
superDiff[x_]:=ChopIt[(x-3.141592653589793)-.0000000000000002384626433832795,16]
Plot[Cos[x-Pi]-Cos[superDiff[x]],{x,0,Pi}]
Plot[Cos[x-Pi]-Cos[improvedDiff[x]],{x,0,Pi}]
Plot[Cos[x-Pi]-Cos[diff[x]],{x,0,Pi}]