Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

False Position Algorithm Develop a function nanmed taisaPoeition.a which estimat

ID: 2268379 • Letter: F

Question

False Position Algorithm Develop a function nanmed taisaPoeition.a which estimates the root of a given funtion. Your function should have the following: Inputs: func- the function being evaluated · XI-the lower guess · Tu-the upper guess . es-the desired relative error (should default to 0.0001%) maxiter - the number of iterations desired (should default to 200) Outputs: . root - the estimated root location ·x-the function evaluated at the root location ea-the approximate relative error (%) · iter-how many iterations were performed

Explanation / Answer

function [ iter ] = myfalsep4(f, a,b, tol,n)
%UNTITLED3 Summary of this function goes here--please write
% Detailed explanation goes here -please write
%
%
%
%
format long
x1=a;x2=b;
iter=0;
f1=feval(f,x1);
f2=feval(f,x2);
%if u*v<0
% display(' false position')
%c=(v*a-u*b)/(v-u);
%w=feval(f,c);
err=1; % it just set it up a big number to go into the loops
disp('-------------------------------------------')
disp('iter a b c f(c) |b-a| ')
disp('-------------------------------------------')
fprintf(' ')
if (f1*f2<=0)
while (abs(x2-x1)>tol)&(iter<=n)&((f1-f2)~=0)
x=x2-f2*(x2-x1)/(f2-f1);
fx=feval(f,x);
if fx*f1>0
x1=x;f1=fx;
else
x2=x;f2=fx;
end
iter=iter+1;
fprintf('%2.0f %12.8f %12.8f %12.8f %10.8f ', iter, x1, x2,
x, fx)
end;
if(iter>n)
disp('Method failed to converge')
end;
if(f2-f1==0)
disp('division by zero')
end;
else
disp('The method cannot be applied f(a)f(b)>0')
end;
%not plotting the graph for now
%you may try to plot
%fplot(f,[a0 b0])
%xlabel('x'); ylabel('f(x)'); grid
end

function [x iter ea ] = falsepos(f,xl,xu, tol,itr)
x1=xl;x2=xu;
iter=0;
f1=feval(f,x1); %Evaluates the function with value x1
f2=feval(f,x2);
ea=1; % it just set it up a big number to go into the loops
disp('-------------------------------------------')
disp('iter xl xu x f(x)')
disp('-------------------------------------------')
fprintf(' ')
if (f1*f2<=0)
while ((ea>tol)&&(iter<=itr))
x=x2-f2*(x2-x1)/(f2-f1);
if(x~=0)
ea=abs((x-x1)/x)*100;
end
fx=feval(f,x);
if fx*f1>0
x1=x;f1=fx;
else
x2=x;f2=fx;
end
iter=iter+1;
fprintf('%d %3.10f %3.10f %3.10f %10.8f ', iter, x1, x2,x, fx)
end;
if(iter>itr)
disp('Method failed to converge')
end;
if(f2-f1==0)
disp('division by zero')
end;
else
disp('The method cannot be applied f(a)f(b)>0')
end;
end

Output:

Enter lower limit1
Enter upper limit3
-------------------------------------------
iter xl xu x f(x)
-------------------------------------------

1 1.5454545455 3.0000000000 1.5454545455 -4.39969947
2 1.8591632292 3.0000000000 1.8591632292 -2.29215123
3 2.0021190999 3.0000000000 2.0021190999 -0.97878205
4 2.0596443045 3.0000000000 2.0596443045 -0.38200012
5 2.0815717848 3.0000000000 2.0815717848 -0.14381564
6 2.0897535149 3.0000000000 2.0897535149 -0.05340766
7 2.0927817902 3.0000000000 2.0927817902 -0.01973263
8 2.0938992744 3.0000000000 2.0938992744 -0.00727690
9 2.0943111871 3.0000000000 2.0943111871 -0.00268167
10 2.0944629590 3.0000000000 2.0944629590 -0.00098799
11 2.0945188719 3.0000000000 2.0945188719 -0.00036396
12 2.0945394691 3.0000000000 2.0945394691 -0.00013408
13 2.0945470565 3.0000000000 2.0945470565 -0.00004939
14 2.0945498515 3.0000000000 2.0945498515 -0.00001819
15 2.0945508811 3.0000000000 2.0945508811 -0.00000670


The root for the given function is: 2.09455088108623
The function evaluated at the root location is: -0.00000670195102
Number of iterations it took: 15
The approximate relative error: 0.000049