Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:54:07+00:00 2026-06-09T03:54:07+00:00

I have written a code for the bisection algorithm in MatLab. I have based

  • 0

I have written a code for the bisection algorithm in MatLab. I have based this on the pseudocode given in my textbook. The algorithm has worked just fine on all my problems so far, but when I’m asked to find a root of f(x) = x – tan(x) on the interval [1,2] I have some troubles. My code is as follows:

function x = bisection(a,b,M)
f = @(x) x - tan(x);
u = f(a);
v = f(b);
e = b-a;
x = [a, b, u, v]
if (u > 0 && v > 0) || (u < 0 && v < 0)
    return;
end;
for k = 1:M
    e = e/2;
    c = a + e;
    w = f(c);
    x = [k, c, w, e]
    if (abs(e) < 10^(-5) || abs(w) < eps)
        return;
    end
    if (w < 0 && u > 0) || (w > 0 && u < 0)
        b = c;
        v = w;
    else
        a = c;
        u = w;
    end
end

If I run this algorithm on the interval [1,2] with, say, 15 iterations, my final answer is:

x =

  1.0e+004 *

    0.0015    0.0002   -3.8367    0.0000

which is obviously way off as I wish to get f(c) = 0 (the third entry in the vector above).

If someone can give me any help/tips on how to improve my result, I would greatly appreciate it. I am very new to MatLab, so treat me as a novice :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:54:09+00:00Added an answer on June 9, 2026 at 3:54 am

    Let’s have a look at the sequence of c which is generated by the bisection method:

    c =  1.5000
    c =  1.7500
    c =  1.6250
    c =  1.5625
    c =  1.5938
    c =  1.5781
    c =  1.5703
    c =  1.5742
    c =  1.5723
    c =  1.5713
    c =  1.5708
    c =  1.5706
    c =  1.5707
    c =  1.5707
    c =  1.5708
    

    You can see that it converges to pi/2. The tan() has a singularity at this point and so does x - tan(x). The bisection method converges to this singularity as is also stated here, for example. This is why the function value at f(c) is not close to zero. In fact it should go to (plus/minus) infinity.

    Other suggestions:

    I like your bisection method. To make it usable in a more general fashion, you could incorporate these changes:

    • Make the variable f a function parameter. Then you can use your method with different
      functions without having to re-write it.
    • The very first time you assign x = [a, b, u, v] but later on x(1) is the number of
      iterations. I would make this more consistent.
    • You can easily test if f(a) and f(b) have different signs by looking at the sign of
      the product p = f(a)*f(b). For p > 0 the signs are equal and there is no root,
      for p < 0 the signs differ and for p == 0 either f(a) or f(b) is zero and you
      already have found the root.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code in JavaScript and works perfectly fine when I include
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written this code inside a servlet to delete certain records from three
I have written code like this to throw a pop up when my website
I have written code for an application. It has some motion events. The motion
I have written code for a custom ListView. It is just a view with
I have written code below.but it will print this exception and i really don't
I have written code for many patterns but unable to write for this..... not
I have written code that automatically creates CSS sprites based on the IMG tags
I have written code like this. <img id='test_img' src='../../..' /> I want to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.