need help with secant method

1 view (last 30 days)
Vick
Vick on 3 Apr 2012
Below is the script I m trying to run. Instead of getting all roots, I keep getting only one root. I want it to display all roots positive and negative. Can someone help me here?
clc
clear;
disp('Secant Method');
disp('—————-');
y1=input('Enter the depth 1:');
q=input('Enter the flowrate :');
z=input('Enter the size of the step :');
E1=y1+(q^2/(2*32.2*y1^2));
E2=E1-z;
x1=input('Initial value of x1=');
x2=input('Initial value of x2=');
tol=input('Tolerance for error=');
f1=x1-E2+q^2/(2*32.2*x1^2);
f2=x2-E2+q^2/(2*32.2*x2^2);
e=1;
ite=0;
while e>=tol;
x3=x2-(f2*(x2-x1)/(f2-f1));
f3=x3-E2+q^2/(2*32.2*x3^2);
e=abs(f3);
ite=ite+1;
if abs(f1)>=abs(f2);
x1=x2;
x2=x3;
f1=f2;
f2=f3;
else x2=x3;
f2=f3;
end
end
disp('The result is');
disp(['f(x)=0 at x3= ', num2str(x3)]);
disp(['f(x)=0 at x2= ', num2str(x2)]);
disp(['f(x)=0 at x1= ', num2str(x1)]);
disp(['Actual tolerance for error= ', num2str(e)]);
disp(['Iteration number= ', num2str(ite)]);

Answers (0)

Categories

Find more on External Language Interfaces in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!