Mathmatica Bisection Root Finding

  • Thread starter teroenza
  • Start date
  • Tags
    Root
In summary: The while loop should have been:while[Abs[fg - r1]/r1*100 > 0.1, (*continue while deviation from Mathematica's root is > 0.1*) xn = xg,(*old guess is new upper 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*) This will make sure that the x point is being updated each time the loop runs.
  • #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.
 
Physics news on Phys.org
  • #2
Add a semicolon to the end of each of your separate statements. Yes Mathematica can sometimes let you skip putting in a semicolon, but too often that will get you into trouble. Mathematica also let's you omit the * to indicate multiplication and often just assumes two things next to each other are to be multiplied. That is probably the source of your "Tag Times" error, it thinks you are trying to multiply a couple of statements together.

When I add those that error goes away and your loop appears to converge, but it doesn't seem to stop appropriately.

See if you can get this far and then try to decide why it isn't stopping.
 
  • #3
Interesting, thank you. I was using semicolons to suppress output to the screen, and I wasn't sure if they would interfere with the syntax within the loop statements. I didn't bother to use it on (suppress) the ones in the loops before.

I also see why it was not stopping. It should have been the x point, not the f(x) point, that was being used in as the condition for the while loop.
 

Related to Mathmatica Bisection Root Finding

1. What is Mathmatica Bisection Root Finding?

Mathmatica Bisection Root Finding is a numerical method used to find the roots of a given function. It involves dividing the search interval in half and determining which half the root lies in. This process is repeated until the root is found within a desired level of precision.

2. How does Mathmatica Bisection Root Finding work?

The method starts with an interval that contains the root. It then evaluates the function at the midpoint of the interval. If the function value at the midpoint is equal to 0, then the midpoint is the root. If the function value at the midpoint has the same sign as the function value at the left endpoint, then the root must lie in the right half of the interval. If the function value at the midpoint has the opposite sign as the function value at the left endpoint, then the root must lie in the left half of the interval. This process is repeated until the root is found within the desired level of precision.

3. What are the advantages of using Mathmatica Bisection Root Finding?

Mathmatica Bisection Root Finding is a simple and reliable method for finding roots of a function. It does not require any initial guesses and is guaranteed to converge to a root if one exists within the given interval. It is also easy to implement and can easily handle multiple roots within the same interval.

4. What are the limitations of Mathmatica Bisection Root Finding?

This method can be slow and may require a large number of iterations to find the root with a desired level of precision, especially if the root is located near the endpoints of the interval. It also requires knowing an interval that contains the root, which may not always be known or easy to determine.

5. Are there other methods for finding roots besides Mathmatica Bisection Root Finding?

Yes, there are various other numerical methods for finding roots such as Newton's method, Secant method, and Brent's method. Each method has its own advantages and limitations, and the most appropriate method to use may depend on the specific problem at hand.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
183
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
8
Views
2K
  • Calculus and Beyond Homework Help
Replies
3
Views
485
  • Classical Physics
Replies
17
Views
1K
Back
Top