Write a mATLnB vser-dehined funchion that selves for a ret or a nonlinear equate
ID: 3753972 • Letter: W
Question
Write a mATLnB vser-dehined funchion that selves for a ret or a nonlinear equaten f(x)- using He bisection methot Nam the finetm s Biseetion Rat ( FPan, a b The ar aryument iethesolution The input Paun, Is a name for the functon that calca lates ) for a given x (t is a dummy name for the Funehon that is imperted into Bibehon ort) and and a te are two points hat bracket the rot. The iteratims should Stop when the tolerance in fa), 5 is smaller than 0.soool. The prag ram shacld chec Ic if points a and b are on opposi te Sides of the sakutin If hot, the praagam Shexid Stop 1 display an error message Use Bisechian Toot to solre problem 32 Determine the root of fox) x- 2e by: ca) Using Bisection method Start with a-o aod be l and cary ast the fist three ileratims (b) isng the scant method Start with +he two oia,"X1:0 and Fe, 11 and cary out ust three ikratms (e) Usn Neutan s method Start at -I and any out the furt three ihreimsExplanation / Answer
Here is the code
syms x;
f(x) = x - 2*exp(-x); %%Defined function
sol = BisectionRoot(f,0,1); %%Calling BisectionRoot method
fprintf("Solution found at x = %.10f with an error of %.10f. ",double(sol),double(f(sol)));
function Xs = BisectionRoot(Fun,a,b)
%%This function finds the root of any function with a linear convergence,
%%only if the solution lies in [a,b], else it prompts an error to change
%%initial guess.
tol = 0.000001; %%Error Tolerance = 0.000001 as mentioned
%%Iterative loop for bisection method
while(1)
c=(a+b)/2; %%Midpoint
if(Fun(c)==0) %%Solution found as c is exact solution, hence return c.
Xs=c;
return;
elseif(Fun(c)*Fun(a)<0) %%Bisection method logic
b=c;
elseif(Fun(c)*Fun(b)<0) %%Bisection method logic
a=c;
else %%Solution doesn't lie between a and b, i.e. Fun(a).Fun(b)>0
error('No solution exist, change initial guess...');
end
if(abs(Fun(c))<tol) %%Exiting condition
Xs=c;
return;
end
end
-------------------------------------------oro-------------------
%function xs=bisecroot(f,a,b)
% clc;
% clear all
f=@(x)x-2*exp(-x);
a=0; %initial condition
b=1;
eps_abs =2* 1e-8; % tolerence
eps_step = 1e-8;
n=1;
if sign(f(a))*sign(f(b)) >= 0
error('f(a)f(b)<0 not satisfied!') %ceases execution
end
N_max=500;
while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )
xs = (a + b)/2; % midpoint
if ( f(xs) == 0 )
break;
elseif ( f(a)*f(xs) < 0 )
b = xs;
else
a = xs;
end
C(n)=xs;
err(n)=abs(b-a);
n=n+1;
if(n>N_max)
fprintf('Not converging ')
end
end
disp('num_iter x_value erorr')
disp('_______________________________________________________________________________')
for i=1:n-1
fprintf('%d %20f %20f ',i ,C(i),err(i))
end
disp('root is c')
xs
num_iter x_value erorr
_______________________________________________________________________________
1 0.500000 0.500000
2 0.750000 0.250000
3 0.875000 0.125000
4 0.812500 0.062500
5 0.843750 0.031250
6 0.859375 0.015625
7 0.851563 0.007813
8 0.855469 0.003906
9 0.853516 0.001953
10 0.852539 0.000977
11 0.853027 0.000488
12 0.852783 0.000244
13 0.852661 0.000122
14 0.852600 0.000061
15 0.852631 0.000031
16 0.852615 0.000015
17 0.852608 0.000008
18 0.852604 0.000004
19 0.852606 0.000002
20 0.852605 0.000001
21 0.852605 0.000000
22 0.852606 0.000000
23 0.852605 0.000000
24 0.852606 0.000000
25 0.852605 0.000000
26 0.852606 0.000000
27 0.852605 0.000000
root is c
xs =
0.852605499327183
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.