Let f(x) 2 - 5. Write a program (using Matlab/Octave or C/C++) that implements t
ID: 3589769 • Letter: L
Question
Let f(x) 2 - 5. Write a program (using Matlab/Octave or C/C++) that implements the method of false position as discussed in class (i.e., do not use any built-in false position functions, if they are available). Run your program using ai = 2, bi-3 and 10-6 How many iterations were required to achieve an absolute error less than what was the final pn value? Give your answer using 9 significant figures (8 digits after the decimal). Hint: Since the approximation of en uses pn, Pn-1 and p-2, you should save the iterates in a vector, for example, using p( n ) =bn-f( bn ) * (bn-an ) / (f( bn )-f( an ) ); You could save the an and bn in vectors as well, but it's not necessary. Hint: Since statement in your loop so that you only approximate the error when n >= 3 and otherwise just set the error approximate to, for example, 10e. e 3) is the first error approximate you can calculate, you can use an ifExplanation / Answer
MATLAB code
f = @(x) x.^2-5; % the function
tol = 10^-6; % Tolerence
a=2;% a and b are selected in such a way that f(a) and
b=3; % f(b) are of oposit sign
N = 0;% initial iteration number
while (abs(b-a)>tol) % loop til tolerence acheieves
% calculating the point p
r = (a*f(b)-b*f(a))/(f(b)-f(a));
% Checking the sign of f(r) and f(a)
if((f(a) > 0 && f(r) > 0) || (f(a) < 0 && f(r) < 0))
a = r;
else
b = r;
end
N = N + 1; % increase the iteration count
end
fprintf('Final Pn = %0.9f and number of iterations = %d ',r,N);
OUTPUT
Final Pn = 2.236067977 and number of iterations = 19
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.