Secant Method

Author

Eric W. Weisstein
March 24, 2006

This notebook downloaded from http://mathworld.wolfram.com/notebooks/NumericalMath/SecantMethod.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/SecantMethod.html.

©2006 Wolfram Research, Inc. except for portions noted otherwise

Code

SecantMethod2[f_,{x_,x1_,x2_},n_:1]:=Module[{l={x1,x2}},
	 Do[
			l=Append[l,l[[-1]]-(l[[-1]]-l[[-2]])/(f[l[[-1]]]-f[l[[-2]]])f[l[[-1]]]],
			{i,n}
		];
		l
]

SecantMethodFigure[f_,{x_,x1_,x2_},n_:1,opts___]:=Module[
		{xi=SecantMethod2[f,{x,x1,x2},n],solid,dotted,pts,arrows},
		dotted=Append[Table[
					Line[{{xi[[i]],f[xi[[i]]]},{xi[[i+1]],f[xi[[i+1]]]}}],{i,2}],
				Table[Line[{{xi[[i]],f[xi[[i]]]},{xi[[i+1]],0}}],{i,4,n+1}]];
		arrows=Table[Arrow[{xi[[i]],0},{xi[[i]],f[xi[[i]]]},HeadScaling->Relative],{i,3,n+2}];
	 pts=Table[Point[{xi[[i]],f[xi[[i]]]}],{i,n+2}];
		Show[Plot[f[x],{x,x1,x2},PlotRange->All,DisplayFunction->Identity],
			Graphics[{arrows,{PointSize[.02],pts},{Dashing[{.02,.02}],dotted}}],
		opts,DisplayFunction->$DisplayFunction]
	]

SecantMethodList[f_,{x_,x0_,x1_},n_]:=NestList[Last[#]-{0,((Function[x,f][Last[#]]Subtract@@#)/(Subtract@@(Function[x,f]/@#)))}&,{x0,x1},n]

SecantMethodAnimation[f_,{x_,x0_,x1_},n_]:=Module[{l=SecantMethodList[f,{x,x0,x1},n]},
{l,Function[x,f]/@l}
]

Examples

Plot[Tan[x]-x,{x,0,5},PlotStyle->Red]

FindRoot[Tan[x]==x,{x,2}]

SecantMethodList[Tan[x]-x,{x,2.,4.5},10]

Figure

SecantMethodList[x^3-x,{x,-.75,.7},10]

<<Graphics`Arrow`

SecantMethodFigure[3#^3-#&,{x,-.75,.7},3,Ticks->None]