Print the screen shot of your MATLAB code with output. Printout must be legible.
ID: 3879930 • Letter: P
Question
Print the screen shot of your MATLAB code with output. Printout must be legible. ·The first line of your code must be % your name. You must show your NAU user name in the "Editor" header. Use NAU computers (possibly the NAU Remote Desktop). Problem. Use the following pseudocode for the Bisection Method and write a MATLAB code to approximate the root of f(x) = e"-x-2 on 10.2] with accuracy within 10-8. Use maximum 100 iterations. Explain steps by commening on them. Algorithm Bisection-Method Input: f(x) = e"-x-2, interval [0.21, tolerance 10-8. maximum number of iterations 100 Output: an approximate root of f on [0, 2 within 10- or a message of failure set a = 0, b 2; rold = a: for i 1 to 100 x = (a + b)/2; if lr - rold 10- % checking required accuracy %done % leave for environment FoundSolution- true: break end if if f(a)f(x)Explanation / Answer
Given below is the code for the question. Just to see the value of root at each iteration, I have not used semi-colon on line 8 i.e x = (a+b)/2 You can suppress the printing of the value x by just putting a semi-colon at that statement end.
Please do rate the answer if it helped. Thank you.
format long
f = @(x) exp(x) - x - 2;
a = 0;
b = 2;
xold = a;
FoundSolution = false;
for i = 1:100
x = (a+b)/2
if abs(x - xold) < 10E-8
FoundSolution = true;
break;
end
if f(a)/f(x) < 0
a = a;
b = x;
else
a = x;
b = b;
end
xold = x;
end
if FoundSolution
disp(x)
disp(["The number of iterations = " , num2str(i)])
else
disp('The required accuracy is not reached in 100 iterations')
end
output
======
x = 1
x = 1.50000000000000
x = 1.25000000000000
x = 1.12500000000000
x = 1.18750000000000
x = 1.15625000000000
x = 1.14062500000000
x = 1.14843750000000
x = 1.14453125000000
x = 1.14648437500000
x = 1.14550781250000
x = 1.14599609375000
x = 1.14624023437500
x = 1.14611816406250
x = 1.14617919921875
x = 1.14620971679688
x = 1.14619445800781
x = 1.14618682861328
x = 1.14619064331055
x = 1.14619255065918
x = 1.14619350433350
x = 1.14619302749634
x = 1.14619326591492
x = 1.14619314670563
x = 1.14619320631027
1.14619320631027
The number of iterations = 25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.