Question

In: Advanced Math

Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all...

Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature

function output = solve(f,options)

% your code here

end

where

the input is

  • • f: the function in f(x) =0.
    • options: is a struct type with the following fields o method: bisection, newton or secant
    • tol: the tolerance for stopping the iterations.
    • maximum_iterations: the maximum number of iterations allowed.
    • initial_guess: that is P_0; if the method needs it
    • df: the derivative of f if the method needs it
    • initial_interval: if the method needs it

the output is also a struct type with the following fields

  • • message: either ‘success’ or an error message.
  • • root: the solution in case of success.
  • • iterations: an array that saves all iterations of the algorithm. Each row represents an iteration of the algorithm. Each row must contain P_n, f(P_n) and |P_n-P_n-1|.
  • Write a script file that tests your function using the following equations

                                  600x^4 – 550x^3 +200x^2 – 20x -1 = 0

Solutions

Expert Solution

function dummy=bisc_newton_secant()
clc;
clear all;


f=@(x)600*x^4 - 550*x^3 +200*x^2-20*x -1; %function
fp=@(x)600*4*x^3 - 550*3*x^2 +200*2*x-20; % derivative of unction


tol=1e-8;

a=0;
b=2; % interval
x0=0.5;
disp('Root by Bisection method')
y=bisecion(f,a,b,tol) % function calling

% a1=0;
% b1=1; % interval
% disp('Root by fixed point method')
% C1=fixedp(g,x0,tol) % function calling

disp('Root by Newton method')
y2=newt(f,x0,tol)% function calling

a2=0.5;
b2=2; % interval

disp('Root by Secant method')
y1=secn(f,a2,b2,tol)% function calling

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bisection method%%%%%%%%%%%
function c=bisecion(f,a,b,tol)
if(f(a)*f(b)>0)
fprintf('Root is not applicable in [%f, %f]\n',a,b)
else

disp('_____________________________________________________')
disp('n p_n ea')
disp('_____________________________________________________')
err=0.1;
k=1;
while ( err>tol )

c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
  
err=abs( (b-a)/2);

k=k+1;
fprintf('%f\t%10f \t %10f\n',k,c,err)
end

end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Secant method %%%%%%%%%%%%%%%%%%%%%%



function x1=secn(f,a,b,tol)
x(1)=a;
x(2)=b;
err=0.1;
%secont method
i=2;

disp('_____________________________________________________')
disp('x f(x) error')
disp('_____________________________________________________')
while (abs(err) > tol & i<=100)
x(i+1)=x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
err (i-1)= abs(x(i+1)-x(i));
  
i = i+1;

  
end
for j=1:i-2
fprintf('%f\t%10f \t %10f \n',x(j),f(x(j)),err(j))
end
x1=x(end);
err1=err(end);
  
  

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Newton method %%%%%%%%%%%%%%%%%%%%%%

function x=newt(f,x0,tol)
k=1;
err=0.1;
x=x0;
disp('_____________________________________________________')
disp('x f(x) error')
disp('_____________________________________________________')
while(err>tol)

x1=x-(f(x)/fp(x)); %newton method
  
err=abs(x-x1);

x=x1;

k=k+1;
fprintf('%f\t%10f \t %10f \n',x,f(x),err)
end

end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Result%%%%%%%%%%%%

Root by Bisection method
_____________________________________________________
n p_n ea
_____________________________________________________
2.000000   1.000000    0.500000
3.000000   0.500000    0.250000
4.000000   0.250000    0.125000
5.000000   0.125000    0.062500
6.000000   0.187500    0.031250
7.000000   0.218750    0.015625
8.000000   0.234375    0.007813
9.000000   0.226563    0.003906
10.000000   0.230469    0.001953
11.000000   0.232422    0.000977
12.000000   0.231445    0.000488
13.000000   0.231934    0.000244
14.000000   0.232178    0.000122
15.000000   0.232300    0.000061
16.000000   0.232361    0.000031
17.000000   0.232330    0.000015
18.000000   0.232346    0.000008
19.000000   0.232353    0.000004
20.000000   0.232349    0.000002
21.000000   0.232351    0.000001
22.000000   0.232352    0.000000
23.000000   0.232353    0.000000
24.000000   0.232353    0.000000
25.000000   0.232353    0.000000
26.000000   0.232353    0.000000
27.000000   0.232353    0.000000
28.000000   0.232353    0.000000

y =

0.2324

Root by Newton method
_____________________________________________________
x f(x) error
_____________________________________________________
0.385185   2.745637    0.114815
0.281283   0.714026    0.103903
0.234849   0.034931    0.046434
0.232358   0.000068    0.002491
0.232353   0.000000    0.000005
0.232353   0.000000    0.000000

y2 =

0.2324

Root by Secant method
_____________________________________________________
x f(x) error
_____________________________________________________
0.500000   7.750000    1.501953
2.000000   5959.000000    0.001923
0.498047   7.619192    0.113670
0.496124   7.492449    0.063088
0.382454   2.674183    0.063160
0.319366   1.337855    0.021495
0.256206   0.339699    0.002312
0.234710   0.032990    0.000045
0.232398   0.000636    0.000000
0.232353   0.000001    0.000000

y1 =

0.2324

>>


Related Solutions

7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for...
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for finding the roots of function. The signature of your function should look like def find_root(f,a,b,n): where n is the maximum number of iterations of to search for the root. The code should follow this algorithm: We are given a continuous function f and numbers a and b and with a<b with f(a)<0<f(b). From the intermediate value theorem we know that there exists a c...
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of...
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of scalar nonlinear equa-tions. Use these programs to compute approximations to real roots of the following equations: exp(x)−3x^2=0, (1) x^3=x^2+x+1, (2) exp(x) =1/(0.1 +x^2), (3) and x= 1 + 0.3 cos(x). (4) Use an error tolerance tol=10^(−12). Display the obtained approximations to the roots of these equations, and compare the number of iterations, starting with the same initial values x0 for bisection and Newton methods,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters) performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken. The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is...
Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for...
Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations.
To find a positive root for , write a MATLAB script file that uses Bisection method....
To find a positive root for , write a MATLAB script file that uses Bisection method. Choose any initial value that is needed. Use absolute relative approximate error to be less than 0.01. Your code should report the number of iteration and the value of x.
Bisection search 1. In MATLAB, implement a function that performs a bisection search. It should take...
Bisection search 1. In MATLAB, implement a function that performs a bisection search. It should take the following parameters: • F: A function (assumed to be continuous) whose roots you want to find, • a: A floating-point number giving the left endpoint of the initial interval in which you want to search for a root of F. • b: A floating-point number giving the right endpoint of the initial interval. • delta: A non-negative floating-point number giving the acceptable proximity...
Set up a spreadsheet that implements the secant method and then solves each of the problems....
Set up a spreadsheet that implements the secant method and then solves each of the problems. Use the graph of each function to select an initial guess. Recall the iteration formula for the secant method: X^k+1=x^k-[f(x^k)/f(x^k)-f(x^k-1)](x^k-x^k-1) Put the formula for the function under the heading f(xk-1) and f(xk). In the cell under xk+1, put the secant method iteration formula. In the second row, replace the previous xk-1 with xk and then xk with xk+1. Now copy the two formulas down...
Use matlab code for bisection method and regula falsi. Thank you!
Use matlab code for bisection method and regula falsi. Thank you!
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method...
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method to locate the solutions with tolerance 10^(−10).
script for Secant Method. Using Matlab or Octave. The data: y = x.^2 - 1.2 x_lower...
script for Secant Method. Using Matlab or Octave. The data: y = x.^2 - 1.2 x_lower = 0.4 x_upper = 0.6 Es = 0.5*10^(2-n) n = 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT