Consider the function f(x) = e^x x 2. We already proved that this function had e
ID: 2257920 • Letter: C
Question
Consider the function f(x) = e^x x 2. We already proved that this function had exactly one solution ( ¯x such that f(¯x) = 0) in [1, 2].
Please answer this question using Matlab and make sure each step is clear, thank you.
a) Apply the Bisection Methods starting at a 1 and b 2 to approximate to 4 decimal places. (L.e., iterate until MATLAB reports the first 4 decimals remain unchanged.) b) Apply the Method of False Position starting at ao-1 and bo-2 to approximate x to 4 decimal places. (L.e., iterate until MATLAB reports the first 4 decimals remain unchanged.Explanation / Answer
clc;
clear all;
f=@(x) exp(x)-x-2;
%initial interval
a=1;
b=2;
% eps_abs = 1e-12;
eps_step = 1e-5;
n=1;
Nmax=500;
N(1)=1
while ((b -a)/2 >= eps_step && n<=Nmax )
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);
c1(n)=c;
err1(n)=err;
N(n)=n;
n=n+1;
end
disp(' N (x) error')
v=[N' c1' err1']
disp('root of function is')
c1(end)
N (x) error
v =
1.0000 1.5000 0.2500
2.0000 1.2500 0.1250
3.0000 1.1250 0.0625
4.0000 1.1875 0.0313
5.0000 1.1563 0.0156
6.0000 1.1406 0.0078
7.0000 1.1484 0.0039
8.0000 1.1445 0.0020
9.0000 1.1465 0.0010
10.0000 1.1455 0.0005
11.0000 1.1460 0.0002
12.0000 1.1462 0.0001
13.0000 1.1461 0.0001
14.0000 1.1462 0.0000
15.0000 1.1462 0.0000
16.0000 1.1462 0.0000
zero of function is
ans =
1.1462
>>
clc;
clear all;
f=@(x) exp(x)-x-2; ; %Given function
% a=input('Starting point a=');
% b=input('End point b=');
% N=input('Number of iteration N=');
a=1; %starting point
b=2; % end point
if(f(b)<f(a))
m=a;
a=b;
b=m;
end
n=1;
N=15;
err=0.1;
while((err>1e-3)&&n<N)
c=(a*f(b)-b*f(a))/(f(b)-f(a));% false method formula
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
err=abs(a-b);
C1(n)=c;
n=n+1;
end
C1'
ans =
1.0767
1.1138
1.1312
1.1393
1.1430
1.1447
1.1455
1.1459
1.1461
1.1461
1.1462
1.1462
1.1462
1.1462
>>