NOT: please i need only the programming solution, screenshot only. Problem 2: De
ID: 2248996 • Letter: N
Question
NOT: please i need only the programming solution, screenshot only.
Problem 2: Determine the root of f(x)-x - 2e-* by (a) Using the bisection method. Start with a=0 and b=1, and carry out the first three iterations. (b) Using the secant method. Start with the two points, xi=0 and X2=1, and carry out the first three iterations. (c) Using the Newton's method. Start at xi-1 and carry out the first three iterations. For each of the parts, provide solution using a programming language you are free to use any language including MATLABExplanation / Answer
Solution :
%*********************************************************************************************%
%%*************Bisection code for "limit" number of iteration****************%%
function p = bisection(f,a,b,limit)
iter_limit = 0;
if f(a)*f(b)>0
disp('Wrong choice')
else
p = (a + b)/2;
err = abs(f(p));
while iter_limit<limit
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
iter_limit=iter_limit+1;
end
end
%********************************************
The argument of above functions is : function f , left limit a , right limit b , and interartion count limit.
so we run it as :
>> f = @(x) (x-2*exp(-x))
f =
@(x) (x - 2 * exp (-x))
>> bisection(f,0,1,3)
ans = 0.81250
%*********************************************************************************************%
%*********************************************************************************************%
%%*************secant method****************%%
function y = secant(f,a,b,limit)
c = (a*f(b) - b*f(a))/(f(b) - f(a));
iteration=0;
while iteration<limit
a = b;
b = c;
c = (a*f(b) - b*f(a))/(f(b) - f(a));
iteration=iteration+1;
endwhile
y=c;
end
%********************************************
The argument of above functions is : function f , left limit a , right limit b , and interartion count limit.
so we run it as :
>> f = @(x) (x-2*exp(-x))
f =
@(x) (x - 2 * exp (-x))
>> secant(f,0,1,3)
ans = 0.85261
%*********************************************************************************************%
%*********************************************************************************************%
%%*************Newtons method****************%%
function x = newton( f, df, x0, tol, nmax )
%f = inline(f);
%df = inline(df);
x(1) = x0 - (f(x0)/df(x0));
ex(1) = abs(x(1)-x0);
k = 2;
while (ex(k-1) >= tol) && (k <= nmax)
x(k) = x(k-1) - (f(x(k-1))/df(x(k-1)));
ex(k) = abs(x(k)-x(k-1));
k = k+1;
end
end
%********************************************
The argument of above functions is : function f , derivative df , initial guess x0 , tolerance and interartion count limit.
so we run it as :
>> f1 = @(x) (x - 2 * exp (-x))
f1 =
@(x) (x - 2 * exp (-x))
>> f2 = @(x) (1+2*exp(-x))
f2 =
@(x) (1 + 2 * exp (-x))
>> newton(f1,f2,1,1e-3,3)
ans =
0.84777 0.85260 0.85261
%*********************************************************************************************%
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.