You are on page 1of 29

Q no.

BISECTION METHOD

MATLAB Code: -

%%Discription
%filename:-Untitled.m
%Author: zahid khan
%Registration_no: -19pwmec4747
% Date 6-may-2021
%Number of revisions= non
%Input: - non
%Output: -non
%% problem
%Find the approximate root and corresponding error using the error bounds
%Of the corresponding method for the equation f(x)=x^3+4x^2-10=0 in the
%Interval [1 2] up to the 4th iteration
%%
%Solution
% Bisection method
format short

clear all,closeall

f = @(x)x^3+4*x^2-10
fplot(f,[-10,10])
grid on

a=1; b=2;

max_I=4; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;
C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'
t=fzero(f,[1,2])
error=C(4)-t
fprintf('the error bound by this is %d \n',error)
fplot(f,[-10,10])
grid on
hold on
plot(t,'d')
Result: -

Error:-
the error bound by bisection method is 0.0527

Newtonmethod:-
MATLAB code:-
format long

clear all,closeall

symsx
f1=x^3+4*x^2-10
f = @(x)x^3+4*x^2-10
df= diff(f1,x,1)
df=matlabFunction(df)
N=4;% maximum number of iterations
tol=10^(-10);
x0=1; % Initial guess
x(1)=x0;
for k=2:N
x(k) = x(k-1)-feval(f,x(k-1))/feval(df,x(k-1));
error=abs(x(k)-x(k-1));
if (error<tol), break;
end
end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'
t=fzero(f,[1,2])
error1=abs(x(4)-t)
disp('the error is \n')
disp(error1)
fplot(f,[-10,10])
grid on
hold on
plot(t,'d')
Result: -

Error: -
The error bounded by newtons method is 0.000006586788
secant method:-
Matlabcodes:-
format short

clear all,closeall
f=@(x)x^3+4*x^2-10
N=4;% maximum number of iterations
tol=10^(-4);
% Initial guess
x(1)=1.0;x(2)=2;
for k=3:N
x(k) = x(k-1)-feval(f,x(k-1))*(x(k-1)-x(k-2))/(feval(f,x(k-1))-feval(f,x(k-
2)));
error=abs(x(k)-x(k-1));
if (error<tol), break;
end
end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'

t=fzero(f,[1,2])
error1=abs(x(4)-t)
disp('the error is ')
disp(error1)
fplot(f,[-10,10])
grid on
hold on
plot(t,'d')
Result:-

Error: -
The error bounded by newtons method is 0.026402174586258

Regula falsimethod: -
MATLABcode:-
format short

clear all,closeall
f = @(x) x.^3+4*x^2-10;
a=1; b=2;
N=4;% maximum number of iterations
tol=10^(-10);
fa=feval(f,a); % Initial guess
fb=feval(f,b);

for k=1:N
c=b-(b-a)*fb/(fb-fa);
C(k)=c;
fc=feval(f,c);
Fc(k)=fc;
if fc==0,break
end
if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end
if abs(fc)<tol,break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

t=fzero(f,[1,2])
error1=abs(C(4)-t)
disp('the error is \n')
disp(error1)
fplot(f,[-10,10])
grid on
hold on
plot(t,'d')
Result: -

Error: -
The error bounded by newtons method is 0.0016825733

fix point iteration method: -


MATLABcode: -
format short

g = @(x) sqrt(10/(4+x)); %% x=g(x)

N=4;% maximum number of iterations


tol=10^(-5);

x(1)=1; % Initial guess


for k=2:N
x(k) = feval(g,x(k-1));

error=abs(x(k)-x(k-1));

if (error<tol), break;
end

end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'
f=@(x)x.^3+4*x^2-10;
t=fzero(f,[1,2])
error1=abs(x(4)-t)
disp('the error is \n')
disp(error1)
fplot(f,[-10,10])
grid on
hold on
plot(t,'d')
Result: -

Error: -
The error bounded by newtons method is 0.000076621

Conclusion: -
As we seen from results of all methods, we observed that newton method is fastest and most
accurate method. It is easy and fastest approach to find root with high accuracy.
Graph of function: -

1.3652
QNO2
%%Discription
%filename:-Untitled.m
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=(x)^1/2-cos(x) on [0,1]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)(x)^1/2+cos(x)

a=0; b=1;

max_I=3; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'
QNO2 PART B
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=3*(x+1)*(x-1/2)*(x-1) on [-2,1.5]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)3*(x+1)*(x-1/2)*(x-1)

a=-2; b=1.5;

max_I=3; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

qno2 part b(ii)


%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=3*(x+1)*(x-1/2)*(x-1) on [-2,1.5]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)3*(x+1)*(x-1/2)*(x-1)

a=-1.23; b=2.5;

max_I=3; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess


fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

qno2 part c

%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [0,1]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^3-7*x^2+14*x-6

a=0; b=1;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

part (ii)
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [1,3.2]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^3-7*x^2+14*x-6

a=1; b=3.2;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end
for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

part(iii)

%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [3.2,4]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^3-7*x^2+14*x-6

a=3.2; b=4;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end
fprintf('no of iteration(k), root sequence(x_k), function values fc\n')
[(1:k);C(1:k);Fc(1:k)]'

qno2 part(c)
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^4+2*x^2-x-3

a=-2; b=-1;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I
c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

part(ii)
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [0,2]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^4+2*x^2-x-3

a=0; b=2;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

part(iii)
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^4+2*x^2-x-3

a=2; b=3;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I
c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'
fplot(f,[-2,2])
hold on
plot(C(7),'d')
grid on

part (iv)
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format short

clear all,closeall

f = @(x)x^4+2*x^2-x-3
a=-1; b=0;

max_I=10; % maximum number of iterations

tol=10^(-2);

fa=feval(f,a); % Initial guess

fb=feval(f,b);

if fa*fb>0
end

for k=1:max_I

c=(a+b)/2;

C(k)=c;

fc=feval(f,c);

Fc(k)=fc;

if fc==0
break
end

if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end

if abs(fc)<tol
break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'
fplot(f,[-2,2])
hold on
plot(C(7),'d')
grid on
Qno4

Part 1
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format short

clear all,closeall

symsx
f1=x^2-6
f = @(x)x^2-6
df= diff(f1,x,1)
df=matlabFunction(df)
N=2;% maximum number of iterations
tol=10^(-5);
x0=1; % Initial guess
x(1)=x0;
for k=2:N
x(k) = x(k-1)-feval(f,x(k-1))/feval(df,x(k-1));
error=abs(x(k)-x(k-1));
if (error<tol), break;
end
end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'

Part 2
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format short

clear all,closeall

symsx
f1=-x^3-cos(x)
f = @(x)x^2-6
df= diff(f1,x,1)
df=matlabFunction(df)
N=2;% maximum number of iterations
tol=10^(-5);
x0=-1; % Initial guess
x(1)=x0;
for k=2:N
x(k) = x(k-1)-feval(f,x(k-1))/feval(df,x(k-1));
error=abs(x(k)-x(k-1));
if (error<tol), break;
end
end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'
We can’t use p0=0 because if we take intial value 0 then we can’t get root we get infity upon second
itration
soointial p0=0 is not a good choice to find root of equation

Part no 3
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format long

clear all,closeall
f=@(x)(x^2)-6
N=3;% maximum number of iterations
tol=10^(-2);
% Initial guess
x(1)=3;x(2)=2;
for k=3:N
x(k) = x(k-1)-feval(f,x(k-1))*(x(k-1)-x(k-2))/(feval(f,x(k-1))-feval(f,x(k-
2)));
error=abs(x(k)-x(k-1));
if (error<tol), break;
end
end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'
Secant method give 2.400000
now false position
%%Discription
%filename:-Untitled.m
%Author: zahid khan
%Registration_no: -19pwmec4747
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format long

clear all,closeall
f = @(x) x^(2)-6
a=3; b=2;
N=3;% maximum number of iterations
tol=10^(-4);
fa=feval(f,a); % Initial guess
fb=feval(f,b);

for k=1:N
c=b-(b-a)*fb/(fb-fa);
C(k)=c;
fc=feval(f,c);
Fc(k)=fc;
if fc==0,break
end
if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end
if abs(fc)<tol,break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

As we see false position method give us 2.448979591836735 which is more closer to (6)^1/2 or 2.44

Part 4
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format long

clear all,closeall
f=@(x)-x^(3)-cos(x)
N=3;% maximum number of iterations
tol=10^(-2);
% Initial guess
x(1)=-1;x(2)=0;
for k=3:N
x(k) = x(k-1)-feval(f,x(k-1))*(x(k-1)-x(k-2))/(feval(f,x(k-1))-feval(f,x(k-
2)));
error=abs(x(k)-x(k-1));
if (error<tol), break;
end
end
fprintf('no of iteration(k), root sequence(x_k) \n')
[(1:k);x(1:k)]'
Now false positon method
%%Discription
%filename:-Untitled.m
% date 6-may-2021
%number of revision= non
%input:- non
%output:-non
%% problem
%use bisection method to find p3 for f(x)=x^3-7*x^2+14*x-6 on [-2,-1]
%%
%solution
% Bisection method
format long

clear all,closeall
f = @(x) )-x^(3)-cos(x)
a=-1; b=0;
N=3;% maximum number of iterations
tol=10^(-4);
fa=feval(f,a); % Initial guess
fb=feval(f,b);

for k=1:N
c=b-(b-a)*fb/(fb-fa);
C(k)=c;
fc=feval(f,c);
Fc(k)=fc;
if fc==0,break
end
if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end
if abs(fc)<tol,break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);C(1:k);Fc(1:k)]'

Therom 2.1 use bound number of itrations


format long

clear all,closeall
f = @(x) x^(3)-x-1
a=1; b=2;
symsn
N=(b-a)/(2^n)==10^-4;% maximum number of iterations
N=solve(N);
N=floor(N)
tol=10^(-4);
fa=feval(f,a); % Initial guess
fb=feval(f,b);

for k=1:N
c=b-(b-a)*fb/(fb-fa);
C(k)=c;
fc=feval(f,c);
Fc(k)=fc;
if fc==0,break
end
if fb*fc>0;
b=c;
fb=fc;
else
a=c;
fa=fc;
end
if abs(fc)<tol,break
end
end

fprintf('no of iteration(k), root sequence(x_k), function values fc\n')


[(1:k);vpa(C(1:k));vpa(Fc(1:k))]'
Bound number of itration are 13 and 12

Question
Question 3:

Answer:
%%
%solution
g=@(x)2^(-x)
symsx
t=x==2^(-x)
u=solve(t)
u=vpa(u)
disp(u)
y=@(x)x
fplot(y,[1/3,1])
hold on
grid on
fplot(f,[1/3,1])
symsn
N=10^(-4)==(log(2)*2^(-1/3))^n
N=solve(N)
N=vpa(N)
N=ceil(N)
tol=10^(-4);

x(1)=1/3; % Initial guess

for k=2:N
x(k) = feval(g,x(k-1));

error=abs(x(k)-x(k-1));

if (error<tol), break;
end

end
fprintf('no of iteration(k), root sequence(x_k) \n')
[vpa((1:k));vpa(x(1:k))]'

Theritically number of itration need to get 10^-4 result is 16 but in actual practice 12 itration is needed
to get desire result

Qno
use algebraic maniplution to show
for b
%fix point iteration method
g = @(x)((x+3-x^(4))/2)^(1/2) %% x=g(x)

N=10;% maximum number of iterations


tol=10^(-5);

x(1)=1; % Initial guess

for k=2:N
x(k) = feval(g,x(k-1));

error=abs(x(k)-x(k-1));

if (error<tol), break;
end

end
fprintf('no of iteration(k), root sequence(x_k) , function value \n')
[(1:k);x(1:k)]'
answ =g(x(10))
for a
%fix point iteration method
g = @(x)((x+3-x^(4))/2)^(1/2) %% x=g(x)

N=10;% maximum number of iterations


tol=10^(-5);

x(1)=1; % Initial guess

for k=2:N
x(k) = feval(g,x(k-1));

error=abs(x(k)-x(k-1));

if (error<tol), break;
end

end
fprintf('no of iteration(k), root sequence(x_k) , function value \n')
[(1:k);x(1:k)]'
answ =g(x(10))

for part A
%fix point iteration method
g = @(x) (3+x-2x^(2))^(1/4)%% x=g(x)

N=10;% maximum number of iterations


tol=10^(-5);

x(1)=1; % Initial guess

for k=2:N
x(k) = feval(g,x(k-1));
error=abs(x(k)-x(k-1));

if (error<tol), break;
end

end
fprintf('no of iteration(k), root sequence(x_k) , function value \n')
[(1:k);x(1:k)]'
answ =g(x(10))

from the result we see that part b (3+x-2x^(2))^(1/4) is more closer to


f(p)=0.

You might also like