I need an example code to make the bisection method in Matlab.
Yes I have an example m function below. I run it in freemat, since I don't have license of Matlab
function [k,c] = bisection(f, a, b, Tol)
%
% bisection(f, a, b, Tol)
% Input:
% f - function given as a string
% a - lower bound
% b - upper bound
% Output:
% k - number of iterations performed
% c - root of the function
% Example:
% [k,c] = bisection( 'x^6-x-1', 1, 2, 0.00001 )
format long;
N= 1e4;
if nargin == 3
Tol = 1e-4;
end
f = inline(f);
if (f(a) * f(b) > 0) || (a >= b)
error('INCORRECT INPUT f(a)*f(b)>0 or a>=b');
k = 1;
x(k) = (a + b) / 2;
while ((k = Tol)
if f(x(k)) == 0
error([ 'bisection: condition f(' num2str(x(k)) ...
')~=0 didn''t apply' ]);
if (f(x(k)) * f(a)) < 0
b = x(k);
else
a = x(k);
k = k + 1;
c=x(k);
Oh sorry, it is better use the attachment.
On MATLAB File Exchange you can find few implementations:
http://www.mathworks.com/matlabcentral/fileexchange/index?utf8=%E2%9C%93&term=bisection
اه
you may use the example in applied numerical method with matlab (Steven Chapra)
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% fx = function value at root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin0,error('no sign change'),end
if nargin
% Simple and Easy way to under stand of Bisection Method in MATLAB
a=input('Enter function with right hand side zero:','s');
f=inline(a);
xl=input('Enter the first value of guess interval:') ;
xu=input('Enter the end value of guess interval:');
tol=input('Enter the allowed error:');
if f(xu)*f(xl)
Seeking advise on java language
07 August 2013 8,641 1 View
Dear Researchers I am trying to perform a PIL simulation using STM32F4 Discovery board and comunication serial USB TO TTL. During simulation I receive the following timeout error: An error...
01 March 2021 2,327 1 View
Hi dear researchers I connected a rigid body into the part of my structure by tie constraint and then I added displacement to rigid body, in static general step every thing is fine and these two...
28 February 2021 3,442 2 View
The following code (see 1st 2 images attached) is used to produce PID controller values that are designed to control the system (G). The code finds the PID controller values (noted as k) by using...
28 February 2021 6,560 14 View
I have input and output data set for "ANFIS modeling in MATLAB", and I am getting some negative predicted values of output in testing. However, the predicted values of output in training are...
28 February 2021 3,459 3 View
I am familiar with finite element simulations and numerical schemes like the Gauss Legendre quadrature. I also know that the Gauss integration yields exact values for polynomials up to degree 2n-1...
27 February 2021 6,818 3 View
If I want to compare characteristics of 2 groups of patients, one group is recruited from a random sample of the general public while another group is from a sample with a fixed gender (male to...
26 February 2021 3,526 3 View
I am required to learn about Flyback converters and I got stuck not knowing to full design of the power supply flyback converter Based USB Charger Model using Simulink, especially the design model...
25 February 2021 5,435 2 View
I do need the Matlab code of Fractal Discrete Cosine Transform (FDCT). Can anyone who has already implement this code, help me with the implementation of this transform?
24 February 2021 5,602 2 View
Hi Hope you are well. Can you please share your code for D2D implementation in Matlab. I want to implement D2D in Matlab based Vienna simulator and struggling to deploy D2D. Thanks
24 February 2021 9,378 3 View
Dear colleagues If anyone has a model of multi-effect distillation in EES or MATLAB software, please send it to me.
24 February 2021 7,282 1 View