0% found this document useful (0 votes)
73 views9 pages

FOR Function 1 Question 1 Bisection Method: f1 @ (X) 2-X+log (X)

Uploaded by

superman1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views9 pages

FOR Function 1 Question 1 Bisection Method: f1 @ (X) 2-X+log (X)

Uploaded by

superman1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Table of Contents

........................................................................................................................................ 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
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FOR function 1>


function > 2-x+log(x)

question 1> Bisection Method


defining the function here

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));
%

the solution for non linear equation is > 3.146271


the solution through fzero function >3.146193

question 1> fixed point iteration method


clear all;
clc;
tol=1e-4;
% original function
f1=@(x) 2-x+log(x);
% g1 function here
g1=@(x) 2+log(x);
% g2 function here
g2=@(x) exp(x-2);

% 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);

fprintf("the solution through fzero function >%f


\n",fzero(f1,xold_old));

plot(error(1:s-1),error(2:s));

% using the g2 form of the function

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 equation from g2 function > %f


\n",x_new);

fprintf("the solution though fzero function >%f


\n",fzero(f1,xold_old+2));

plot(error(1:s-1),error(2:s));

title("error(i+1) vs error(i)");
xlabel("error(i)");
ylabel("error(i+1)");

the solution of non linear equation from g1 function > 3.146140


the solution through fzero function >3.146193
the solution of non linear equation from g2 function > 0.158584
the solution though fzero function >0.158594

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)));

% finding the initial point here


x_old=3;
xold_old=x_old;
x_new=0;
tol=1e-7;
max_iter=100;
s=0;
err=[];
for i=1:max_iter

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);

fprintf("the solution through fzero function >%f


\n",fzero(f1,xold_old));
plot(err(1:s-1),err(2:s));
title("error(i+1) vs error(i)");
xlabel("error(i)");
ylabel("error(i+1)");

0.1479 0.0017 0.0000

the solution of non linear equation from newton raphson method >
3.146193
the solution through fzero function >3.146193

For function 2>


%function > (x^2)-(3*x)+1

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);

fprintf("the solution through fzero function >%f\n",fzero(f1,b));

the solution for non linear equation > 2.618011


the solution through fzero function >2.618034

question 2> fixed point iteration method


clear all;
clc;
tol=1e-4;
% original function
f1=@(x) (x^2)-(3*x)+1;
% g1 function here
g1=@(x) (1+(x^2))/3;

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);

fprintf("the solution through fzero function >%f


\n",fzero(f1,xold_old));

plot(error(1:s-1),error(2:s));

title("error(i+1) vs error(i)");
xlabel("error(i)");
ylabel("error(i+1)");

% using the g2 form of the function

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);

fprintf("the solution through fzero function >%f


\n",fzero(f1,xold_old+4));

the solution of non linear equation from g1 function > 0.382093

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

question 2> Netwon Raphson method


the function is in the variable x using the syms for finding the diff of the function

syms x;

f1=@(x) (x^2)-(3*x)+1;
% to find the diff of the function

df1=matlabFunction(diff(f1(x)));

% finding the initial point here


x_old=3;
xold_old=x_old;
x_new=0;
tol=1e-7;
max_iter=100;
s=0;
err=[];
for i=1:max_iter

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

fprintf("the solution of non linear equation from newton raphson


method > %f\n",x_new);

fprintf("the solution through fzero function >%f


\n",fzero(f1,xold_old));
plot(err(1:s-1),err(2:s));
title("error(i+1) vs error(i)");
xlabel("error(i)");
ylabel("error(i+1)");

the solution of non linear equation from newton raphson method >
2.618034
the solution through fzero function >2.618034

Published with MATLAB® R2019b

You might also like