simple False Position code
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to do a simple False Position code following my teachers model but can't get it to actually work. Not sure if my if, else is wrong or what as it only says Method failed but I know there should be and answer of P=3.0571 and i=16. Totally new to programming and matlab so any help would be greatly valued. Thanks
%False Position %(x^2-4x+4-lnx=0 for 2<=x<=4)
p0=2; p1=4; TOL=10^-6; N=100;
q0=p0^2-4*p0+4-log(p0); q1=p1^2-4*p1+4-log(p1);
i=2; while i<=N p=p1-q1*(p1-p0)/(q1-q0);
if abs(p-p1)<TOL
disp('False Position')
p
i
break
end
i=i+1;
q=p^2-4*p+4-log(p);
if q*q1<0
p0=p1;q0=q1;
else
p1=p;q1=q;
end
end
if i>=N disp('Method Failed')
end
0 Kommentare
Antworten (2)
Roger Stafford
am 16 Sep. 2013
I believe your error lies in the lines
if q*q1<0
p0=p1;
q0=q1;
They ought to read
if q*q1<0
p0=p;
q0=q;
The idea is to keep the signs of q0 and q1 opposite, so you set either (p1,q1) or (p0,q0) to (p,q) whichever will retain that as true while continually shrinking the interval length. You were setting (p0,q0) to (p1,q1) which would suddenly shrink the interval length to zero with the true solution off to one side.
0 Kommentare
Ayana
am 30 Nov. 2025 um 12:31
function [root, iter] = false_position(f, a, b, tol, max_iter) % f: function handle for which we want to find the root % a: lower bound of the interval % b: upper bound of the interval % tol: tolerance for convergence % max_iter: maximum number of iterations
if f(a) * f(b) >= 0
error('f(a) and f(b) must have different signs');
end iter = 0;
root = a; % Initialize root while iter < max_iter
% Calculate the false position
root = (a * f(b) - b * f(a)) / (f(b) - f(a)); % Check if the root is close enough to zero
if abs(f(root)) < tol
break; % Found the root within tolerance
end % Update the interval
if f(root) * f(a) < 0
b = root; % The root is in the left subinterval
else
a = root; % The root is in the right subinterval
end iter = iter + 1; % Increment iteration count
end if iter == max_iter
warning('Maximum number of iterations reached without convergence.');
end
end▎How to Use the Function
1. Define your function as a function handle. For example, if you want to find the root of f(x) = x² - 4 :
f = @(x) x^2 - 4;
2. Call the false_position function with appropriate arguments:
a = 0; % Lower bound of the interval b = 5; % Upper bound of the interval tol = 1e-6; % Tolerance for convergence max_iter = 100; % Maximum number of iterations
[root, iter] = false_position(f, a, b, tol, max_iter);
fprintf('Root: %.6f found in %d iterations\n', root, iter);f(x) = x² - 4 within the interval [0, 5]. Adjust the function, interval, tolerance, and maximum iterations as needed for your specific problem.
root=a; % initialize root
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!