Question

For 01-3, use f (x)E-3.3x3 +2.5x -0.6, and be sure

% Bisection.m Lines of code 17-26 and 43-47 are bold

% This code finds the root of a function f(x) in the interval [a, b] using the Bisection method

%

% It uses f.m to define f(x), and assumes f(x) is continuous

% It requires specification of a, b and the maximum error

% It defines error using |f(xnew)|

% Define inputs for problem

a=0; %Defines lower limit of initial bracketing interval

b=1; %Defines upper limit of initial bracketing interval

errmax = 1E-4; %Defines maximum error

%Get function values associated with initial interval endpoints

fa = f(a); %get function value at a

fb = f(b); %get function value at b

% Let MATLAB figure out which end point is "plus" and which is "minus"

if fa>0 & fb<0

    xplus = a;

    xminus = b;

elseif fa <0 & fb>0

    xplus = b;

    xminus = a;

else

    'Not a valid bracketing interval'

    return

end

iter = 0; %Initialize iteration counter (not required, but for interest)

err = 1; %Initial value of err is set large to ensure enter loop.

while err > errmax %Iteration loop

   

    %Increase iteration counter

    iter=iter+1;

   

    %Get xnew using Bisection

    xnew = (xplus + xminus)/2;

    %Get f(xnew)

    fnew = f(xnew);

    %Adjust bracketing interval based on sign of f(xnew)

    if fnew > 0

        xplus = xnew;

    else

        xminus = xnew;

    end

    err = abs(fnew); %This is the error metric for this example.

   

end

'Root is'

xnew

iter

......................................................................................NEW CODE...............................................................

%Textbooks bisect.m lines of code 19 – 24 are Bold

function bisect(f,a,b,tol,n)

% Bisection method for solving the nonlinear

%equation f(x)=0.

a0=a;

b0=b;

iter=0;

u=feval(f,a);

v=feval(f,b);

c=(a+b)*0.5;

err=abs(b-a)*0.5;

disp('_____________________________________________________________________')

disp(' iter    a            b           c           f(c)      |b-a|/2 ')

disp('_____________________________________________________________________')

fprintf('\n')

if (u*v<=0)

   while (err>tol)&(iter<=n)

      w=feval(f,c);

      fprintf('%2.0f %10.4f %10.4f %12.6f %10.6f %10.6f\n',iter,a,b,c,w,err)

      if (w*u<0)

         b=c;v=w;

      end

      if (w*u>0)

         a=c;u=w;

      end

      iter=iter+1;

      c=(a+b)*0.5;

      err=abs(b-a)*0.5;

   end

   if (iter>n)

      disp(' Method failed to converge')

   end  

else

   disp('   The method cannot be applied f(a)f(b)>0')

end

% Plot f(x) in the interval [a,b].

fplot(f, [a0 b0])

xlabel('x');ylabel('f(x)');

grid

     

        

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PART A)

MATLAB CODE call bisect.m function

f = @(x) -3.3*x.^3+2.5*x-0.6;
a =0;
b = 0.5;
tol = 1E-4;
n=21;
bisect(f,b,a,tol,n);

OUTPUT

_____________________________________________________________________
iter    a            b           c           f(c)      |b-a|/2
_____________________________________________________________________

0      0.5000      0.0000      0.250000   -0.026562    0.250000
1      0.5000      0.2500      0.375000    0.163477    0.125000
2      0.3750      0.2500      0.312500    0.080542    0.062500
3      0.3125      0.2500      0.281250    0.029709    0.031250
4      0.2812      0.2500      0.265625    0.002215    0.015625
5      0.2656      0.2500      0.257812   -0.012018    0.007812
6      0.2656      0.2578      0.261719   -0.004862    0.003906
7      0.2656      0.2617      0.263672   -0.001313    0.001953
8      0.2656      0.2637      0.264648    0.000453    0.000977
9      0.2646      0.2637      0.264160   -0.000429    0.000488
10      0.2646      0.2642      0.264404    0.000012    0.000244
11      0.2644      0.2642      0.264282   -0.000209    0.000122

0.3 0.2 0.1 0 0.1 0.2 0.3 -0.4 0.5 0.4 0.5 -0.6 0.2 0.3 0.1

ANSWER

Initial xminus = 0 and initial xplus =0.5

f(x) is calculated 14 times

PART B)

Modified Bisection.m function

%Textbooks bisect.m lines of code 19 – 24 are Bold
function bisect(f,a,b,tol,n)
% Bisection method for solving the nonlinear
%equation f(x)=0.
fa = feval(f,a);
fb = feval(f,b);
if(fa>0 && fb <0)
    xplus = a;
    xminus = b;
elseif(fa<0&&fb>0)
    xplus = b;
    xminus = a;
else
    fprintf('Not a valid bracketing interval\n');
    return
end
iter=0;
xnew=(xplus+xminus)*0.5;
err=abs(xplus-xminus)*0.5;
disp('_____________________________________________________________________')
disp(' iter xminus          xplus        xnew         f(xnew)     |xplus-xminus|/2 ')
disp('_____________________________________________________________________')
fprintf('\n')
while ((err>tol)&&(iter<=n))
   fnew=feval(f,xnew);
   fprintf('%2.0f %10.4f %10.4f %12.6f %10.6f %10.6f\n',iter,xminus,xplus,xnew,fnew,err)
    if(fnew>0)
        xplus=xnew;
    else
        xminus=xnew;
    end
   iter=iter+1;
   xnew=(xplus+xminus)*0.5;
   err=abs(xplus-xminus)*0.5;
end
if (iter>n)
   disp(' Method failed to converge')
end
end

FUNCTION CALL

f = @(x) -3.3*x.^3+2.5*x-0.6;
a =0;
b = 0.5;
tol = 1E-4;
n=21;
bisect(f,b,a,tol,n);

OUTPUT

_____________________________________________________________________
iter xminus          xplus        xnew         f(xnew)     |xplus-xminus|/2
_____________________________________________________________________

0      0.0000      0.5000      0.250000   -0.026562    0.250000
1      0.2500      0.5000      0.375000    0.163477    0.125000
2      0.2500      0.3750      0.312500    0.080542    0.062500
3      0.2500      0.3125      0.281250    0.029709    0.031250
4      0.2500      0.2812      0.265625    0.002215    0.015625
5      0.2500      0.2656      0.257812   -0.012018    0.007812
6      0.2578      0.2656      0.261719   -0.004862    0.003906
7      0.2617      0.2656      0.263672   -0.001313    0.001953
8      0.2637      0.2656      0.264648    0.000453    0.000977
9      0.2637      0.2646      0.264160   -0.000429    0.000488
10      0.2642      0.2646      0.264404    0.000012    0.000244
11      0.2642      0.2644      0.264282   -0.000209    0.000122

Part C)

function regularfalsi(f,a,b,tol,n)
% The secant method for solving the nonlinear
% equation f(x)=0.
iter=0;
u=feval(f,a);
v=feval(f,b);
err=abs(b-a);
disp('______________________________________________________________')
disp('iter     xn          f(xn)      f(xn+1)-f(xn)    |xn+1-xn|')
disp('______________________________________________________________')
fprintf('%2.0f %12.6f %12.6f\n',iter,a,u)
fprintf('%2.0f %12.6f %12.6f %12.6f %12.6f\n',iter,b,v,v-u,err)
while (err>tol)&(iter<=n)&((v-u)~=0)
      x=(v*a-u*b)/(v-u);
      a=b;
      u=v;
      b=x;
      v=feval(f,b);
      err=abs(b-a);
      iter=iter+1;
      fprintf('%2.0f %12.6f %12.6f %12.6f %12.6f\n',iter,b,v,v-u,err)
end
if ((v-u)==0)
      disp(' Division by zero')
end
if (iter>n)
      disp(' Method failed to converge')
end

Part D)

Bisection.m modified to regulafalsi.m

f = @(x) -3.3*x.^3+2.5*x-0.6;
a =0;
b = 0.5;
tol = 1E-4;
n=21;
regulafalsi(f,b,a,tol,n);

OUTPUT

______________________________________________________________
iter     xn          f(xn)      f(xn+1)-f(xn)    |xn+1-xn|
______________________________________________________________
0      0.500000      0.237500
0      0.000000     -0.600000     -0.837500      0.500000
1      0.358209      0.143844      0.743844      0.358209
2      0.288939      0.042743     -0.101101      0.069270
3      0.259653     -0.008637     -0.051381      0.029286
4      0.264576      0.000322      0.008959      0.004923
5      0.264399      0.000002     -0.000320      0.000177
6      0.264398     -0.000000     -0.000002      0.000001

ANSWER

f(x) is calculated 8 times

In general Bisection method take more time to converge to the root, but it have less number of poerations in the loop as compared to ragula falsi method

Add a comment
Know the answer?
Add Answer to:
% Bisection.m Lines of code 17-26 and 43-47 are bold % This code finds the root...
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
  • I need help creating program for the Test_Bisection pseudocode. I need to find a root for...

    I need help creating program for the Test_Bisection pseudocode. I need to find a root for each function f and g and get the output below. Pseudocode Now let's construct pseudocode to carry out this procedure. We shall not try to create a piece of high-quality software with many "bells and whistles,” but we write the pseudocode in the form of a procedure for general use. This allows the reader an opportunity to review how a main program and one...

  • 45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using...

    45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using Newton-Rephson Method without showing any iteration. Also find the root of equation, f(x) = x 9-3x -10, take initial guess, Xo=2 العقدة College of 9:05 mybb.qu.edu.ca Numerical Methods (Lab.) GENG 300 Summer 2020 5.1.2 Open Methods - Newton-Raphson Method f(x) *1+1 = x; - Matlab Code Example:4 function mynewtraph.t1.x0,-) XXO for ilin x - x - x)/1 x) disp 1 x) <0.01 break end...

  • Looking for Muller Method MatLab coding for these following equations: This is the code I currently...

    Looking for Muller Method MatLab coding for these following equations: This is the code I currently have but is not working fun = @(x) x.^2-2; x1 = 0; x2 = 1; tol = 0.0001; kmax = 100; function [x, y] = Muller(fun, x1, x2, tol, kmax) x(1) = x1; x(2) = x2; x(3) = (x(2)+x(1))/2; y(1) = feval(fun, x1); y(2) = feval(fun, x2); y(3) = feval(fun, x(3)); c(1) = (y(2)-y(1))/(x(2)-x(1)); for k = 3 : kmax c(k-1) = (y(k)-y(k-1))/(x(k)-x(k-1)); d(k-2)...

  • Matlab problem using newton raphson to find square root of number

    Need help modifying my Matlab script below (myscript calculates the square root of a number. using a Newton-Raphson method with 1 as the initial guess, calculates true and estimated error, and shows each iteration).-I need to create three new functions each of which should be called in the main script. These functions are needed to replace code that is currently in my script shown below.-I need to create these functions:A function to find f(x)A function to find f '(x) ?A...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • 5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f,...

    5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f, f1,x0,n) Xx0; for ilin x = x - f(x)/f1(x); disp (li if f(x) <0.01 f(x))) break end end end Matlab Code from Chapra function [root, ea, iter)=newtraph (func,dfunc, xr, es,maxit,varargin) newtraph: Newton-Raphson root location zeroes 8 [root, ea, iter)-newtraph (func, dfunc, xr, es,maxit,pl,p2, ...): $uses Newton-Raphson method to find the root of fune input: func- name of function 8dfunc = name of derivative of...

  • I'm working on the newton's method on matlab, could someone help me and show what two...

    I'm working on the newton's method on matlab, could someone help me and show what two lines are needed to be added in the codes in order to make this function work? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x)...

  • need the MATLAB code to solve for percentage error at the 5th iteration given the following...

    need the MATLAB code to solve for percentage error at the 5th iteration given the following Newton-Raphson code Using the MATLAB code finline(X13-x^2-18); dfeinline(3*x"2-2#x'); e?100 count=0; xi=0.5; while (ea>01); count = count 1 ; xil - xi - f(xi)/df(xi); ea = 100 * abs((xi 1-xi)/xi 1 ); disp([count xil.xi, ea]); xi=xi 1 ; If ea is the error defined in percentage, the value of the error (ea) at 5th iteration (i.e., count-5) is closest or equal to hoices 51% 54%...

  • The function should starts as: function [x,fs,k]=Newton1(x0) % enter your code here end >> ...

    The function should starts as: function [x,fs,k]=Newton1(x0) % enter your code here end >> [x,f,k]=Newton1(0)  x = 1.1641, f = -1.6653e-16, k = 7 >> [x,f,k]=Newton1(0.1)  x = 0.1972, f = 1.1102e-16, k = 5 >> [x,f,k]=Newton1(1.5)  x = 1.3111, f = -1.1102e-16, k = 6 >> [x,f,k]=Newton1(2)  Warning iteration diverged, x = 2.8808, f = -0.5624, k = 1000 Let the mathematical function f(x) be defined as: f(x) = exp(-0.5x) cos(5x)-0.5 , x 〉 0...

  • THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that...

    THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that calculates exp(2) by a Maclaurin series using a while loop exp x )=-+-+-+-+ The while loop should add each of the series terms. The program error as defined below is smaller than 0.01. Error is the exact value (i.e. exp(2)), minus the approximate value (i.e., the current series sum) should exit the loop when the absolute absolute error-lexact-approx Use fprintf within the loop such...

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