Professional Documents
Culture Documents
FOR Function 1 Question 1 Bisection Method: f1 @ (X) 2-X+log (X)
FOR Function 1 Question 1 Bisection Method: f1 @ (X) 2-X+log (X)
FOR Function 1 Question 1 Bisection Method: f1 @ (X) 2-X+log (X)
........................................................................................................................................ 1
FOR function 1> ................................................................................................................. 1
question 1> Bisection Method ............................................................................................... 1
question 1> fixed point iteration method ................................................................................. 2
question 1 >Newton Raphson method ..................................................................................... 4
For function 2> .................................................................................................................. 5
question 2 >bisection method ................................................................................................ 6
question 2> fixed point iteration method ................................................................................. 6
question 2> Netwon Raphson method ..................................................................................... 8
%%Lab 7
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Assisgnment - 7
%
% Name : Aniket Dixit Student ID : 201851021
% Section : 1 Lab Group : A
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f1=@(x) 2-x+log(x);
% setting the initial variables
a=1;
b=4;
% checking the condition for the opposite signs
if (f1(a)*f1(b)>0)
fprintf("must have oppsite signs")
end
% setting the tolerance for error
tol=1e-4;
% setting maximum iterations
max_iter=100;
x_low=a;
x_high=b;
x_old=0;
% algorithm
for k=1:max_iter
x_mid=(x_low+x_high)/2;
1
if (abs(f1(x_old)-f1(x_mid))<tol)
break;
end
if (f1(x_mid)==0)
break;
elseif (f1(x_mid)*f1(x_low)>0)
x_low=x_mid;
elseif(f1(x_high)*f1(x_mid)>0)
x_high=x_mid;
end
x_old=x_mid;
end
fprintf("the solution for non linear equation is > %f\n",x_mid);
fprintf("the solution through fzero function >%f\n",fzero(f1,b));
%
% maximum iterations
max_iter=100;
% this is for the g1 function
x_old=2;
xold_old=x_old;
x_new=0;
s=0;
error=[];
for i=1:max_iter
x_new=g1(x_old);
% error(s)=abs(g1(x_old)-g1(x_new));
if (abs(g1(x_old)-g1(x_new))<tol)
break;
end
s=s+1;
error(s)=abs(g1(x_old)-g1(x_new));
x_old=x_new;
end
2
fprintf("the solution of non linear equation from g1 function >
%f\n",x_new);
plot(error(1:s-1),error(2:s));
x_old=-1;
xold_old=x_old;
for i=1:max_iter
x_new=g2(x_old);
if (abs(x_old-x_new)<tol)
break;
end
x_old=x_new;
end
plot(error(1:s-1),error(2:s));
title("error(i+1) vs error(i)");
xlabel("error(i)");
ylabel("error(i+1)");
3
question 1 >Newton Raphson method
the function is in the variable x using the syms for finding the diff of the function
syms x;
f1=@(x) 2-x+log(x);
% to find the diff of the function
df1=matlabFunction(diff(f1(x)));
x_new=x_old-(f1(x_old)/df1(x_old));
if (abs((x_old)-(x_new))<tol)
break;
end
4
s=s+1;
err(s)=abs(x_new-x_old);
x_old=x_new;
end
disp(err);
fprintf("the solution of non linear equation from newton raphson
method > %f\n",x_new);
the solution of non linear equation from newton raphson method >
3.146193
the solution through fzero function >3.146193
5
question 2 >bisection method
% defining the function here
f1=@(x) (x^2)-(3*x)+1;
% setting the initial variables
a=1;
b=3;
% checking the condition for the opposite signs
if (f1(a)*f1(b)>0)
fprintf("must have oppsite signs")
end
% setting the tolerance for error
tol=1e-4;
% setting maximum iterations
max_iter=100;
x_low=a;
x_high=b;
x_old=0;
% algorithm
for k=1:max_iter
x_mid=(x_low+x_high)/2;
if (abs(f1(x_old)-f1(x_mid))<tol)
break;
end
if (f1(x_mid)==0)
break;
elseif (f1(x_mid)*f1(x_low)>0)
x_low=x_mid;
elseif(f1(x_high)*f1(x_mid)>0)
x_high=x_mid;
end
x_old=x_mid;
end
fprintf("the solution for non linear equation > %f \n",x_mid);
6
% g2 function here
g2=@(x) sqrt((3*x)-1);
% maximum iterations
max_iter=100;
% this is for the g1 function
x_old=1;
xold_old=x_old;
x_new=0;
s=0;
error=[];
for i=1:max_iter
x_new=g1(x_old);
% error(s)=abs(g1(x_old)-g1(x_new));
if (abs(g1(x_old)-g1(x_new))<tol)
break;
end
s=s+1;
error(s)=abs(g1(x_old)-g1(x_new));
x_old=x_new;
end
fprintf("the solution of non linear equation from g1 function >
%f\n",x_new);
plot(error(1:s-1),error(2:s));
title("error(i+1) vs error(i)");
xlabel("error(i)");
ylabel("error(i+1)");
x_old=-1;
xold_old=x_old;
for i=1:max_iter
x_new=g2(x_old);
if (abs(x_old-x_new)<tol)
break;
end
x_old=x_new;
end
fprintf("the solution of non linear function from g2 function >%f
\n",x_new);
7
the solution through fzero function >0.381966
the solution of non linear function from g2 function >2.618041
the solution through fzero function >2.618034
syms x;
f1=@(x) (x^2)-(3*x)+1;
% to find the diff of the function
df1=matlabFunction(diff(f1(x)));
x_new=x_old-(f1(x_old)/df1(x_old));
8
if (abs((x_old)-(x_new))<tol)
break;
end
s=s+1;
err(s)=abs(x_new-x_old);
x_old=x_new;
end
the solution of non linear equation from newton raphson method >
2.618034
the solution through fzero function >2.618034