MAT 360 Project 1
#1

NewtonsMethod[f_,a0_,eps_,nits_] :=
Module[
{a=N[a0], (* Use a local variable (so as not to alter passed arguments) *)
fa = f[N[a0]],
i=0,
output (* Let's collect the output, since we've gone to the trouble of computing it. *)
},
output={{i,a,fa}}; 
While[Abs[fa] > eps && i<nits,
a = a-(fa/(f'[a]));
fa=f[a];
i=i+1;
output=Append[output,{i,a,fa}];  (* once again, collect output. *)
]; 
(* Now let's augment the naive function to include printing some nice formatted output: *)
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"i","(Subscript[x, k])","f[(Subscript[x, k])]"}}],16]]; 
Print["  x  = ",NumberForm[a,16] ]; 
Print["f(x) = ",NumberForm[fa,16] ]; 
Return[Transpose[output]]; (* Now I'll actually return the output, so that it can be plotted, etc. *)
]

f[x_]:=x*(E^x)

Plot[f[x],{x,-3,1}]

eps=(10^(-6));
nits=100;

NewtonsMethod[f,1,eps,nits]

NewtonsMethod[f,-.5,eps,nits]

NewtonsMethod[f,-1,eps,nits]

NewtonsMethod[f,-2,eps,nits]

Solve[f'[x]==0,x]

“Basins”
a) For x0 > 0, Newton’s Method will converge monotonically to the root x=0. All roots of tangent lines drawn in the first quadrant will be posiitve. This means the method will continually converge to x=0. 

b) For -1 < x0 < 0, Newton’s Method will converge non-monotonically. Any tangent lines drawn from this area will cross the y-axis and intersect the x-axis positively. The method will then converge monotonically from there. 

c) For x0 = -1, Newton’s method will “blow up” because f’(-1) = 0, so the tangent line at that point will never cross the x-axis. 
d) For x0 < -1, Newton’s Method will monotonically flee the root. In that area, the roots of the tangent lines will approach -∞.

#2

Clear[f]

f[x_]:=(x+1)*x*(x-1)

Plot[f[x],{x,-Sqrt[2],π/2}]

Bisection[a0_,b0_,m_] :=
Module[{},
a = N[a0];  
b = N[b0];  
c = ((a + b)/2); 
k = 0; 
output={{k,a,c,b,f[c]}}; 
While[k < m,
If[ Sign[f[b]] ==  Sign[f[c]], 
b = c, a = c; ]; 
c = ((a + b)/2); 
k = k+1; 
output=Append[output,{k,a,c,b,f[c]}]; ]; 
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"k","(Subscript[a, k])","(Subscript[c, k])","(Subscript[b, k])","f[(Subscript[c, k])]"}}],16]]; 
Print["  c  = ",NumberForm[c,16] ]; 
Print[" Δc  = ±",((b-a)/2)]; 
Print["f[c] = ",NumberForm[f[c],16] ]; ]

Bisection[-Sqrt[2],Pi/2,10]

You will know the function is converging to 1 around the second or third iteration. It is most prominently known around the 7th and 8th iteration.

Bisection[-1.25,1.50,1]

Bisection[-1.15,1.15,1]

In order to converge to zero the midpoint of the chosen values must immediately converge to zero. For example the values -1.25 and 1.25 will converge to zero on the first iteration. Another example is -1.15 and 1.15.

#3

t[x_]:=(E^x)-(1/(x^2))
g1[x_]:=(x^3)(E^x)
g2[x_]:=-2*Log[Abs[x]]
g3[x_]:=x*((((x^2)(x-1)(E^x)+3)/((x^3)(E^x)+2)))
g4[x_]:=(E^((-x)/2))

Plot[{t[x],g1[x],g2[x],g3[x],g4[x]},{x,.25,1.5},PlotStyle->{Red,Blue,Green,Orange,Purple}]

G1

p0=1.1;
a=.25;
b=1.5;
precision=50;
Clear[p,k]
p[0]=N[p0,precision];
Do[p[k]=N[g1[p[k-1]],precision],{k,1,100}]
(* Let’s write out the values: *)
TableForm[Table[{k,NumberForm[p[k],precision]},{k,0,11}]]

Did not converge.

G2

p0=.7;
a=.25;
b=1.5;
precision=50;
Clear[p,k]
p[0]=N[p0,precision];
Do[p[k]=N[g2[p[k-1]],precision],{k,1,100}]
(* Let’s write out the values: *)
TableForm[Table[{k,NumberForm[p[k],precision]},{k,0,40}]]

Did not converge.

G3

p0=1.1;
a=.25;
b=1.5;
precision=50;
Clear[p,k]
p[0]=N[p0,precision];
Do[p[k]=N[g3[p[k-1]],precision],{k,1,100}]
(* Let’s write out the values: *)
TableForm[Table[{k,NumberForm[p[k],precision]},{k,0,11}]]

Converged!

G4

p0=1.1;
a=.25;
b=1.5;
precision=50;
Clear[p,k]
p[0]=N[p0,precision];
Do[p[k]=N[g4[p[k-1]],precision],{k,1,100}]
(* Let’s write out the values: *)
TableForm[Table[{k,NumberForm[p[k],precision]},{k,0,11}]]

Converged!

Abs[{g1'[.703467422],g2'[.703467422],g3'[.703467422],g4'[.703467422]}]

2.84306/.703467422

If the absolute value of |g’[r] |< 1, then the fixed point method will converge for all values in the neighborhood. For this reason, g1 and g2 are not valid fixed point methods for f[x]. The smaller the magnitude of slope the faster the fixed point method converges.

#4

Clear[f]

f[x_]:=(E^x)-(1/(x^2))

Bisection Method

Bisection[.25,1.5,20]

Bisection method takes 17 iterations to converge with an error of (10^(-6))

Newton’s Method

NewtonsMethod[f_,a0_,eps_,nits_] :=
Module[
{a=N[a0], (* Use a local variable (so as not to alter passed arguments) *)
fa = f[N[a0]],
i=0,
output (* Let's collect the output, since we've gone to the trouble of computing it. *)
},
output={{i,a,fa}}; 
While[Abs[fa] > eps && i<nits,
a = a-(fa/(f'[a]));
fa=f[a];
i=i+1;
output=Append[output,{i,a,fa}];  (* once again, collect output. *)
]; 
(* Now let's augment the naive function to include printing some nice formatted output: *)
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"i","(Subscript[x, k])","f[(Subscript[x, k])]"}}],16]]; 
Print["  x  = ",NumberForm[a,16] ]; 
Print["f(x) = ",NumberForm[fa,16] ]; 
Return[Transpose[output]]; (* Now I'll actually return the output, so that it can be plotted, etc. *)
]

NewtonsMethod[f,1,(10^(-6)),50]

Newton’s Method take 4 iterations to converge with an error of (10^(-6)).

Muller’s Method

MullersMethod[f_,x0_,x1_,x2_,ϵ_,nits_]:=Module[{x00=N[x0],x11=N[x1],x22=N[x2],fx0,fx1,c,a,b,i=0,output},
fx0=f[x00];
fx1=f[x11];
c=f[x22];
output={i,x22,,c};
a=((((x11-x22)*(fx0-c))-((x00-x22)*(fx1-c)))/((x00-x22)*(x11-x22)*(x00-x11)));
b=((((((x00-x22))^2)*(fx1-c))-((((x11-x22))^2)*(fx0-c)))/((x00-x22)*(x11-x22)*(x00-x11)));

While[Abs[c]>ϵ&&i<nits,
x00=x11;
x11=x22;
fx0=f[x00];
fx1=f[x11];
x22=x22+((-2c)/(b+Sign[b]*Sqrt[(b^2)-4a*c]));
c=f[x22];

a=((((x11-x22)*(fx0-c))-((x00-x22)*(fx1-c)))/((x00-x22)*(x11-x22)*(x00-x11)));
b=((((((x00-x22)))^2)*(fx1-c)-((((x11-x22))^2)*(fx0-c)))/((x00-x22)*(x11-x22)*(x00-x11)));
i=i+1;
output=Append[output,{i,x22,c}];];
Print[NumberForm[TableForm[output,TableHeadings->{None,{"i","(Subscript[x, k])","f[(Subscript[x, k])]"}}],16]];
Print[" x = ",NumberForm[x22,16]];
Print["f(x)=",NumberForm[c,16]];
]

MullersMethod[f,.5,.75,1,(10^(-6)),50]

Muller’s Method takes 4 iterations with an error of (10^(-6)).

Newton’s Quadratic

NewtonsQuadratic[f_,a0_,eps_,nits_] :=
Module[
{a=N[a0], (* Use a local variable (so as not to alter passed arguments) *)
fa = f[N[a0]],
fp = f'[N[a0]],
fpp = f''[N[a0]],
i=0,
output (* Let's collect the output, since we've gone to the trouble of computing it. *)
},
output={{i,a,fa}}; 
While[Abs[fa] > eps && i<nits,
a = a-((fp-Sign[fp]*Sqrt[fp*fp-2*fa*fpp])/fpp); (* a variation on Joey's idea...*)
fa=f[a];
fp = f'[a];
fpp = f''[a];
i=i+1;
output=Append[output,{i,a,fa}];  (* once again, collect output. *)
]; 
(* Now let's augment the naive function to include printing some nice formatted output: *)
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"i","(Subscript[x, k])","f[(Subscript[x, k])]"}}],16]]; 
Print["  x  = ",NumberForm[a,16] ]; 
Print["f(x) = ",NumberForm[fa,16] ]; 
Return[Transpose[output]]; (* Now I'll actually return the output, so that it can be plotted, etc. *)
]

NewtonsQuadratic[f,1,(10^(-6)),50]

Newton’s Quadratic takes 3 iterations to converge with an error of (10^(-6)).

Secant Method

SecantMethod[f_,a0_,b0_,eps_,nits_] :=
Module[
{a=N[a0],b=N[b0], (* Use a local variable (so as not to alter passed arguments) *)
fa = f[N[a0]],fb=f[N[b0]],
i=0,
output (* Let's collect the output, since we've gone to the trouble of computing it. *)
},
output={{i,a,fa,fb}}; 
While[Abs[fa] > eps && i<nits,
a = a-((fa*(b-a))/(fb-fa));
fa=f[a];
fb=f[b];
i=i+1;
output=Append[output,{i,a,fa}];  (* once again, collect output. *)
]; 
(* Now let's augment the naive function to include printing some nice formatted output: *)
Print[NumberForm[TableForm[output,
TableHeadings->{None,{"i","(Subscript[x, k])","f[(Subscript[x, k])]"}}],16]]; 
Print["  x  = ",NumberForm[a,16] ]; 
Print["f(x) = ",NumberForm[fa,16] ]; 
]

SecantMethod[f,.5,1,10^-6,30]

Secant method takes 14 iterations to converge with an error of (10^(-6 )).

Method Comparison Fastest to Slowest: Chosen error (10^(-6)).

1. Newton’s Quadratic was the fastest and converged in only 3 iterations. 
2. Muller’s and Newton’s method both converged in 4 iterations.
3. The Secant method converged in 14 iterations.
4. The Bisection method was the slowest and converged in 17 iterations.