[Link].
11 MEMOONA ARSHAD
PRACTICAL NO 2(B)
OBJECT:
Write a program to find the root of the given equation using secant
method(using estimated error).
Page | 21
PRACTICAL:2(B)
[Link].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=temp.
STEP NO.11: Repeat a loop while n<=5.
STEP NO. 11(a): r0=x2.
STEP NO. 11(b): x2=x1-(f(x1)*(x0-x1)/f(x0)-f(x1)))).
STEP NO. 11(c): f2=f(x2).
STEP NO. 11(d): e=abs(((x2-r0)/x2)*100)
STEP NO. 11(e): write n,x0,x1,x2,f2.
STEP NO. 11(f): x0=x1.
STEP NO. 11(g): X1=x2.
STEP NO. 11(h): 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 | 22
PRACTICAL:2(B)
[Link].11 MEMOONA ARSHAD
FLOW CHART
A
Start
r0=x1
Declare variables
x1,x2,temp,fo,f1,f2,e,r0,n
x2=x1-(f(x1)*(x0-x1)/f(x0)-f(x1)))).
Declare Function ‘f’
f2=f(x2) by calling function f(x).
N=1
e=abs(((x2-r0)/x2)*100)
Read x0,x1
Writen,x0,x1,x2,f2.
f0=f(x0) by calling function(x).
X0=x1
f1=f(x1) by calling function(x).
X1=x2
No
abs(f0)<abs(f1) B
f0=f(x0) by calling function(x).
Yes
f1=f(x1) by calling function(x).
Temp=x0
B
N=n+1
X0=x1
Writen,x2
X1=temp function
f(x:real):real
No Stop
B e<=1
f:= (3*x)+sin(x)-exp(x)
Yes
A Stop
Page | 23
PRACTICAL:2(B)
[Link].11 MEMOONA ARSHAD
SOURCE CODE
program secant_method;
uses crt;
var
x0,x1,x2,temp,f0,f1,f2,e,r0:real;
no:integer;
c:char;
function f(x:real):real;
begin
f:=(3*x)+sin(X)-exp(x);
end;
begin
clrscr();
no:=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:',x[Link]);
writeln('[Link] x0 x2 f2 estimated error');
writeln;
repeat
r0:=x2;
x2:=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))));
f2:=f(x2);
e:=abs(((x2-r0)/x2)*100);
writeln(no,' ', x[Link],' ',x[Link],' ' ,[Link],' ',[Link],'%');
x0:=x1;
x1:=x2;
f0:=f(x0);
f1:=f(x1);
no:=no+1;
until e<=1;
writeln('the required root is',x[Link]);
readln(c);
end.
Page | 24
PRACTICAL:2(B)
[Link].11 MEMOONA ARSHAD
OUTPUT
Page | 25
PRACTICAL:2(B)
[Link].11 MEMOONA ARSHAD
Page | 26
PRACTICAL:2(B)