Practical 5
1)Input
clc;clear;
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
function y=f(x)
y=x^3-2*x-5
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=(a+b)/2
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(a-b)
if error<0.0001 then
break
end
//printf('i')
//printf('c')
disp(i)
disp(c)
end
Output
Enter the 1st guess:2
Enter the 2nd guess:3
Enter the number of iteration100
13.
2.0946045
1)Input
//Regula falsi method
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
d=0
function y=f(x)
y=x^3-2*x-5
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=(b*f(a)-a*f(b))/(f(a)-f(b))
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(d-c)
if error<0.0001 then
break
end
d=c
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:2
Enter the 2nd guess:3
Enter the number of iteration100
8.
2.0945181
1)Input
//Secant method
clc;clear;
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
d=0
function y=f(x)
y=x^3-2*x-5
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=b-(f(b)*(b-a))/(f(b)-f(a))
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(d-c)
if error<0.0001 then
break
end
d=c
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:2
Enter the 2nd guess:3
Enter the number of iteration100
8.
2.0945181
2)Input
//Regula falsi method
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
d=0
function y=f(x)
y=x^3-5*x+1
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=(b*f(a)-a*f(b))/(f(a)-f(b))
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(d-c)
if error<0.0001 then
break
end
d=c
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:1
Enter the 2nd guess:5
Enter the number of iteration100
34.
2.1281697
1)Input
//Newton
clc;clear;
a=input('Enter the fiest guess:')
b=input('Enter the second guess:')
n=input('Enter the number of iterations')
function y=f(x)
y=x^3-2*x-5
endfunction
fa=f(a)
fb=f(b)
c=0
if (fa*fb>0) then
error('Change the interval value')
end
if (fa+fb) >0 then
x=a
else
x=b
end
for i=1:n
x=x-((f(x))/(numderivative(f,x)))
if abs(c-x)<0.0001
break
end
c=x
end
disp(i,x)
Output
Enter the fiest guess:1
Enter the second guess:6
Enter the number of iterations100
8.
2.0945515
2)Input
clc;clear;
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
function y=f(x)
y=x^3-5*x+1
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=(a+b)/2
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(a-b)
if error<0.0001 then
break
end
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:1
Enter the 2nd guess:7
Enter the number of iteration100
16.
2.1283875
2)Input
//Regula falsi method
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
d=0
function y=f(x)
y=x^3-3*x-5
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=(b*f(a)-a*f(b))/(f(a)-f(b))
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(d-c)
if error<0.0001 then
break
end
d=c
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:1
Enter the 2nd guess:5
Enter the number of iteration100
24.
2.2788138
2)Input
//Newton
clc;clear;
a=input('Enter the fiest guess:')
b=input('Enter the second guess:')
n=input('Enter the number of iterations')
function y=f(x)
y=x^3-5*x+1
endfunction
fa=f(a)
fb=f(b)
c=0
if (fa*fb>0) then
error('Change the interval value')
end
if (fa+fb) >0 then
x=a
else
x=b
end
for i=1:n
x=x-((f(x))/(numderivative(f,x)))
if abs(c-x)<0.0001
break
end
c=x
end
disp(i,x)
Output
Enter the fiest guess:1
Enter the second guess:6
Enter the number of iterations100
5.
0.2016397
3)Input
//Newton
clc;clear;
a=input('Enter the fiest guess:')
b=input('Enter the second guess:')
n=input('Enter the number of iterations')
function y=f(x)
y=x^3-3*x-5
endfunction
fa=f(a)
fb=f(b)
c=0
if (fa*fb>0) then
error('Change the interval value')
end
if (fa+fb) >0 then
x=a
else
x=b
end
for i=1:n
x=x-((f(x))/(numderivative(f,x)))
if abs(c-x)<0.0001
break
end
c=x
end
disp(i,x)
Output
Enter the fiest guess:1
Enter the second guess:6
Enter the number of iterations100
66.
2.2790188
3)Input
//Secant method
clc;clear;
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
d=0
function y=f(x)
y=x^3-5*x+1
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=b-(f(b)*(b-a))/(f(b)-f(a))
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(d-c)
if error<0.0001 then
break
end
d=c
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:1
Enter the 2nd guess:5
Enter the number of iteration100
34.
2.1281697
2)Input
3)Input
clc;clear;
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
function y=f(x)
y=x^3-3*x-5
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=(a+b)/2
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(a-b)
if error<0.0001 then
break
end
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:1
Enter the 2nd guess:5
Enter the number of iteration100
16.
2.2789917
3)Input
//Secant method
clc;clear;
a=input('Enter the 1st guess:')
b=input('Enter the 2nd guess:')
n=input('Enter the number of iteration')
d=0
function y=f(x)
y=x^3-3*x-5
endfunction
fa=f(a)
fb=f(b)
if (fa*fb>0) then
error('Initial guess have different value')
end
for i=1:n
c=b-(f(b)*(b-a))/(f(b)-f(a))
fc=f(c)
if (fc<0)
a=c
fa=fc
else
b=c
fb=fc
end
error=abs(d-c)
if error<0.0001 then
break
end
d=c
end
//printf('i')
//printf('c')
disp(i)
disp(c)
Output
Enter the 1st guess:1
Enter the 2nd guess:5
Enter the number of iteration100
24.
2.2788138