Question

Program for the bisection method for finding the root of the nonlinear equation with a programming...

Program for the bisection method for finding the root of the nonlinear equation with a programming language(Matlab)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

function  m = bisection(f, low, high, tol)
disp('Bisection Method');

% Evaluate both ends of the interval
y1 = feval(f, low);
y2 = feval(f, high);
i = 0;

% Display error and finish if signs are not different
if y1 * y2 > 0
   disp('Have not found a change in sign. Will not continue...');
   m = 'Error'
   return
end

% Work with the limits modifying them until you find
% a function close enough to zero.
disp('Iter    low        high          x0');
while (abs(high - low) >= tol)
    i = i + 1;
    % Find a new value to be tested as a root
    m = (high + low)/2;
    y3 = feval(f, m);
    if y3 == 0
        fprintf('Root at x = %f \n\n', m);
        return
    end
    fprintf('%2i \t %f \t %f \t %f \n', i-1, low, high, m);   

    % Update the limits
    if y1 * y3 > 0
        low = m;
        y1 = y3;
    else
        high = m;
    end
end

% Show the last approximation considering the tolerance
w = feval(f, m);
fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', m, y3, i-1);
fprintf(' Approximation with tolerance = %f \n', tol);

how to run:

my_fun = @(x) 5*x^4 - 2.7*x^2 - 2*x + .5;
low = .1;
high = 0.5;
tolerance = .00001;
x = bisection(my_fun, low, high, tolerance);

result:

Bisection Method
Iter    low        high          x0
0    0.100000 0.500000 0.300000
Root at x = 0.200000

Add a comment
Know the answer?
Add Answer to:
Program for the bisection method for finding the root of the nonlinear equation with a programming...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT