Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using matlab to find roots.

  1. Aug 12, 2011 #1
    I am trying to use matlab to find the roots of a quadratic by the standard iterative techniques. I am totally on top of all this work in theory and in practice when it comes to doing it with a calculator or Excel, but I have never used matlab before and I have been given the code below to use.

    The trouble is I don't know how I am s'posed to enter the equation that I am using for the iteration.

    Suppose the equation was x^2 - 4x -11 = 0
    Then I obviously need to get the computer to recognise that I want to find the fixed point of

    x = (4x+11)^.5

    But where do I tell the computer that this is the equation I want to solve?

    Here is the code I was given. I suppose it must have something to do with "Gather input data"?

    % programme Bisection
    % Bisection method for finding root of equation
    % Algorithm from Burden & Faires page 47
    % Gather input data
    a = input('Input lower endpoint: ');
    b = input('Input upper endpoint: ');
    tol = input('Input tolerance: ');
    Nmax = input('Input maximum number of iterations: ');
    count = 1;
    fa = f(a);

    % Iterate
    while count <= Nmax
    p = a + (b-a)/2;
    fp = f(p);
    % Test for finish
    if or(fp == 0, (b-a)/2 < tol)
    disp('Calculated root is: ');
    disp('Number of iterations was: ');
    count = count + 1;
    % Compute next interval
    if fa*fp > 0
    a = p;
    fa = fp;

    % If we get here then no root was found
    disp('Method failed: Number of iterations: ')

    Thankyou very much in advance.
  2. jcsd
  3. Aug 12, 2011 #2


    User Avatar
    Gold Member

    It looks like you need to define a function file f.m for that code to use. If you run the code in your post as is, I suspect you will receive an error stating that the function f is not defined.

    Try something like:
    Code (Text):

    [COLOR="Blue"]function[/COLOR] [ y ] = f( x )
        y = a.*x.^2 + b.*x + c; [COLOR="SeaGreen"]% replace a, b, and c with the constants of your quadratic.[/COLOR]
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook