Program Secant1
Program Secant1
11 MEMOONA ARSHAD
OBJECT:
Write a program to find the root of the given equation using secant
method(using fixed iteration).
Page | 16
PRACTICAL # 2(A)
GR.NO.11 MEMOONA ARSHAD
ALGORITHM
STEP NO. 01: Start the program.
STEP NO. 02: Declare variables x1,x2,temp,fo,f1,f2,e,r0,n.
DEFINE FUNCTION f(x:real):real;
STEP NO. 03: f:= (3*x)+sin(x)-exp(x)
STEP NO. 04: Return value of the function.
STEP NO. 05: End of the function.
DEFINE MAIN PROGRAM
STEP NO. 06: n:=1.
STEP NO. 07: Read x0 and x1.
STEP NO. 08: f0=f(x0) by calling function(x).
STEP NO. 09: f1=f(x1) by calling function(x).
STEP NO. 10:if abs(f0)<abs(f1)
STEP NO. 10(a): temp=x0.
STEP NO. 10(b):x0=x1.
STEP NO. 10(c): x1=x2.
STEP NO.11: Repeat a loop while n<=5.
STEP NO. 11(a): x2=x1-(f(x1)*(x0-x1)/f(x0)-f(x1)))).
STEP NO. 11(b): f2=f(x2).
STEP NO. 11(c): Writen,x0,x1,x2,f2.
STEP NO. 11(d): X0=x1.
STEP NO. 11(e): X1=x2.
STEP NO. 11(f): f0=f(x0) by calling function(x).
STEP NO. 11(i): f1=f(x1) by calling function(x).
STEP NO. 11(j): N=n+1.
STEP NO. 12: Write x2.
STEP NO. 13: Stop the program.
Page | 17
PRACTICAL # 2(A)
GR.NO.11 MEMOONA ARSHAD
FLOW CHART
A
Start
Writen,x0,x1,x2,f2.
N=1
X0=x1
Read x0,x1
X1=x2
f0=f(x0) by calling function(x).
abs(f0)<abs(f1) B B
N=n+1
Yes
Writen,x2
Temp=x0
Stop
X0=x1
X1=temp function
f(x:real):real
No
B N<=5
f:= (3*x)+sin(x)-exp(x)
Yes
A Stop
Page | 18
PRACTICAL # 2(A)
GR.NO.11 MEMOONA ARSHAD
SOURCE CODE
program secant_method;
uses crt;
var
x0,x1,x2,temp,f0,f1,f2,e,r0:real;
n:integer;
c:char;
function f(x:real):real;
begin
f:=(3*x)+sin(X)-exp(x);
end;
begin
clrscr();
n:=1;
write('x0:');
readln(x0);
write('x1:');
readln(x1);
f0:=f(x0);
f1:=f(X1);
if abs(f0)<abs(f1) then
begin
temp:=x0;
x0:=x1;
x1:=temp;
end;
writeln('to find the root of the equation using secant method');
writeln;
writeln('x2',x2:3:2);
writeln('s.no x0 x2 f2 ');
writeln;
while n<=5 do
begin
x2:=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))));
f2:=f(x2);
writeln(n,' ', x0:8:7,' ',x2:8:7,' ' ,f2:8:7);
x0:=x1;
x1:=x2;
f0:=f(x0);
f1:=f(x1);
n:=n+1;
end;
writeln('the required root is',x2:8:7);
readln(c);
end.
Page | 19
PRACTICAL # 2(A)
GR.NO.11 MEMOONA ARSHAD
OUTPUT
Page | 20
PRACTICAL # 2(A)