Sunteți pe pagina 1din 12

COMPUTATIONAL METHODS ASSIGNMENT

1.Bisection Method

clc
clear all
close all
f=@(x)(x^6)-x-1
xl=input('enter first initial guess')
xu=input('enter second initial guess')
error_t=input('enter the allowed error')
iteration=0
if f(xu)*f(xl)<0
xr=(xu+xl)/2
else
fprintf(‘the guess is incorrect enter the new guesses’\n)
xl=input(‘enter the first initial guess’)
xu=input(‘enter the second initial guess’)
end
fprintf(‘\t\t\t iteration \t\t\t xl \t\t\t xu \t\t\t xr \t\t\t error)
for i=2:1000
xr=(xu+xl)/2
if f(xr)<0
xl=xr
else
xu=xr
end
iteration=iteration+1
xn(1)=0
xn(i)=xr
error(i)=(xn(i)-xn(i-1))/xn(i)
fprintf('\n %15d %15f %15f %15f %15f',iteration,xl,xu,xr,abs(error(i)))
if abs(error(i))<error_t
break
end
end
fprintf('\n approximate root %.10f',xr)
plot(abs(error))
grid on
title(‘error vs no of iterations)
xlabel(‘iterations’)
ylabel(‘error’)
GRAPH:
2.Regula Falsi Method
clc
clear all
close all
f=@(x)(x^6)-x-1
xl=input('enter first initial guess')
xu=input('enter second initial guess')
error_t=input('enter the allowed error')
iteration=0
if f(xu)*f(xl)<0
xr=xu-(((f(xu)*(xl-xu))/f(xl)-f(xu))
else
fprintf(‘the guess is incorrect enter the new guesses’\n)
xl=input(‘enter the first initial guess’)
xu=input(‘enter the second initial guess’)
end
fprintf(‘\t\t\t iteration \t\t\t xl \t\t\t xu \t\t\t xr \t\t\t error)
for i=2:1000
xr=xu-(((f(xu)*(xl-xu))/f(xl)-f(xu))
if f(xr)<0
xl=xr
else
xu=xr
end
iteration=iteration+1
xn(1)=0
xn(i)=xr
error(i)=(xn(i)-xn(i-1))/xn(i)
fprintf('\n %15d %15f %15f %15f %15f',iteration,xl,xu,xr,abs(error(i)))
if abs(error(i))<error_t
break
end
end
fprintf('\n approximate root %.10f',xr)
plot(abs(error))
grid on
title(‘error vs no of iterations)
xlabel(‘iterations’)
ylabel(‘error’)

GRAPH:

3.Newton Raphson Method


Clc
Clear all
Close all
f=@(x)(x^6)-x-1
df=@(x)(6*(x^5))-1
x0=input(‘enter the initial guess’)
error_t=input(‘enter the allowed error’)
iteration=-1
x(1)=x0
fprintf(‘\t\t iteration \t\t xr \t\t\t error’)
for i=1:1000
x(i+1)=x(i)-(f(x(i))/df(x(i))
iteration=iteration+1
error(i+1)=(x(i+1)-x(i))/x(i+1)
fprintf(‘\n %15d %15f %15f’,iteration,x(i+1),abs(error(i)))
if abs(error(i+1))<error_t
xr=x(i+1)
break
end
end
fprintf(‘\n approximate root is %.10f’,xr)
plot(abs(error))
xlabel(‘iterations’)
ylabel(‘error’)
title(‘error vs no of iterations’)

GRAPH:

4.SECANT METHOD
Clc
Clear all
Close all
f=@(x)(x^6)-x-1
error_t=input(‘enter the allowed error’)
iteration=-1
x(1)=input(‘enter the first initial guess’)
x(2)=input(‘enter the second initial guess’)
fprintf(‘\t\t iteration \t\t xr \t\t error’)
for i=3:1000
x(i)=x(i-1)-(f(x(i-1)))*((x(i-1)-x(i-2))/(f(x(i-1))-f(x(i-2))))
iteration=iteration+1
error(i)=(x(i)-x(i-1))/x(i)
fprintf(‘\n %15d %15f %15f’,iteration,x(i),abs(error(i)))
if abs(error(i))<error_t
xr=x(i)
break
end
end
fprintf(‘\n approximate root is %.10f’,xr)
plot(abs(error))
xlabel(‘iterations’)
ylabel(‘error’)
title(‘error vs no of iterations’)

GRAPH:
5.MODIFIED SECANT METHOD
Clc
Clear all
Close all
f=@(x)(x^6)-x-1
a=input(‘enter the del value’)
error_t=input(‘enter the allowed error’)
iteration=0
xr=input(‘enter the initial value’)
fprintf(‘\t\t iteration \t\t xr \t\t error’)
for i=2:1000
xr(i)=xr(i-1)-a*xr(i-1)*f(xr(i-1))/((f(xr(i-1)+a*xr(i-1))-f(xr(i-1))))
iteration=iteration+1
error(i)=((xr(i)-xr(i-1))/xr(i)
fprintf(‘\n %15d %15f %15f’,iteration,xr,abs(error(i)))
if abs(error(i))<error_t
xr=xr(i)
break
end
end
fprintf(‘\n approximate root is %.10f’,xr)
plot(abs(error))
grid on
title(‘error vs no of iterations’)
xlabel(‘iterations’)
ylabel(‘error’)

GRAPH:
6.COMBINED CODE
Clc
Clear all
Close all
f=@(x)(x^6)-x-1
df=@(x)(6*(x^5))-1
%bisection method
xl=input(‘enter the first initial guess’)
xu=input(‘enter the second initial guess’)
error_t=input(‘enter the allowed error’)
iteration=0
if f(xu)*f(xl)<0
xr=(xu+xl)/2
else
fprintf(‘the guesses are incorrect so enter the new guesses’)
xl=input(‘enter the first initial guess’)
xu=input(‘enter the second initial guess’)
end
fprintf(‘\t\t\t iteration \t\t\t xl \t\t\t xu \t\t\t xr \t\t\t error)
for i=2:1000
xr=(xu+xl)/2
if f(xr)<0
xl=xr
else
xu=xr
end
iteration=iteration+1
xn(1)=0
xn(i)=xr
error(i)=(xn(i)-xn(i-1))/xn(i)
fprintf('\n %15d %15f %15f %15f %15f',iteration,xl,xu,xr,abs(error(i)))
if abs(error(i))<error_t
break
end
end
fprintf('\n approximate root %.10f',xr)
%regula falsi method
xl=input('enter first initial guess')
xu=input('enter second initial guess')
error_t=input('enter the allowed error')
iteration=0
if f(xu)*f(xl)<0
xr=xu-(((f(xu)*(xl-xu))/f(xl)-f(xu))
else
fprintf(‘the guess is incorrect enter the new guesses’\n)
xl=input(‘enter the first initial guess’)
xu=input(‘enter the second initial guess’)
end
fprintf(‘\t\t\t iteration \t\t\t xl \t\t\t xu \t\t\t xr \t\t\t error)
for i=2:1000
xr=xu-(((f(xu)*(xl-xu))/f(xl)-f(xu))
if f(xr)<0
xl=xr
else
xu=xr
end
iteration=iteration+1
xn(1)=0
xn(i)=xr
error_a(i)=(xn(i)-xn(i-1))/xn(i)
fprintf('\n %15d %15f %15f %15f %15f',iteration,xl,xu,xr,abs(error_a(i)))
if abs(error_a(i))<error_t
break
end
end
fprintf('\n approximate root %.10f',xr)
%newton Raphson method
x0=input(‘enter the initial guess’)
error_t=input(‘enter the allowed error’)
iteration=-1
x(1)=x0
fprintf(‘\t\t iteration \t\t xr \t\t\t error’)
for i=1:1000
x(i+1)=x(i)-(f(x(i))/df(x(i))
iteration=iteration+1
error_b(i+1)=(x(i+1)-x(i))/x(i+1)
fprintf(‘\n %15d %15f %15f’,iteration,x(i+1),abs(error_b(i)))
if abs(error_b(i+1))<error_t
xr=x(i+1)
break
end
end
fprintf(‘\n approximate root is %.10f’,xr)
%secant method
error_t=input(‘enter the allowed error’)
iteration=-1
x(1)=input(‘enter the first initial guess’)
x(2)=input(‘enter the second initial guess’)
fprintf(‘\t\t iteration \t\t xr \t\t error’)
for i=3:1000
x(i)=x(i-1)-(f(x(i-1)))*((x(i-1)-x(i-2))/(f(x(i-1))-f(x(i-2))))
iteration=iteration+1
error_c(i)=(x(i)-x(i-1))/x(i)
fprintf(‘\n %15d %15f %15f’,iteration,x(i),abs(error_c(i)))
if abs(error_c(i))<error_t
xr=x(i)
break
end
end
fprintf(‘\n approximate root is %.10f’,xr)
%modified secant method
a=input(‘enter the del value’)
error_t=input(‘enter the allowed error’)
iteration=0
xr=input(‘enter the initial value’)
fprintf(‘\t\t iteration \t\t xr \t\t error’)
for i=2:1000
xr(i)=xr(i-1)-a*xr(i-1)*f(xr(i-1))/((f(xr(i-1)+a*xr(i-1))-f(xr(i-1))))
iteration=iteration+1
error_d(i)=((xr(i)-xr(i-1))/xr(i)
fprintf(‘\n %15d %15f %15f’,iteration,xr,abs(error_d(i)))
if abs(error_d(i))<error_t
xr=xr(i)
break
end
end
fprintf(‘\n approximate root is %.10f’,xr)
plot(abs(error))
hold on
plot(abs(error_a))
hold on
plot(abs(error_b))
hold on
plot(abs(error_c))
hold on
plot(abs(error_d))
hold on
title(‘error vs no of iterations’)
xlabel(‘no of iterations’)
ylabel(‘error’)

Graph:

S-ar putea să vă placă și