- #1
teroenza
- 195
- 5
Homework Statement
Write code in Mathematica to find the root of
[itex]f(x) = \int_0^x \frac{\sin (t)}{t} \, dt-\frac{\pi }{2}[/itex]
using the bisection method.
I have attempted to do this using:
Code:
f[x_] := N[-(Pi/2) + Integrate[Sin[t]/t, {t, 0, x}]]
(*Plot[f[x],{x,0,2*Pi}]*)
fg = f[1.5](*initial guess evaluation*)
r1 = FindRoot[f[x], {x, 1.5}];(*Mathmatica's guess for the root*)
r1 = r1[[1, 2]]
xg = 1.5; xn = 1; xp = 3; (*Initialize guess point, and bracket to \
search over*)
While[Abs[fg - r1]/r1*100 > 0.1,
(*continue while deviation from Mathematica's root is > 0.1*)
If[fg > 0,(*update bounds*)
xp = xg,(*old guess is new upper root bound*)
xn = xg(*old guess is new lower root bound*)
]
xg = (xn + xp)/2(*Bisect interval to generate new guess point*)
Print[fg]
fg = f[
xg](*update the function's value to feed back into the while \
condition*)
]
But I keep getting errors such as ""Tag Times in -0.246113\ 2\ Null is Protected."". I am trying to figure out if I defined the function incorrectly, or am missing something else.