0% found this document useful (0 votes)
43 views34 pages

Nme Lab Assign4 Group 6

The document discusses using Newton-Raphson and secant methods to solve nonlinear equations numerically in MATLAB. It provides MATLAB code and output for four examples of applying each method to different functions. It also applies the Newton-Raphson method with a step size of 2 to the same function.

Uploaded by

Sameer Kumar
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)
43 views34 pages

Nme Lab Assign4 Group 6

The document discusses using Newton-Raphson and secant methods to solve nonlinear equations numerically in MATLAB. It provides MATLAB code and output for four examples of applying each method to different functions. It also applies the Newton-Raphson method with a step size of 2 to the same function.

Uploaded by

Sameer Kumar
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/ 34

NUMERICAL METHODS IN

ENGINEERING LABORATORY
GROUP 6
MEMBER 1 -SAMEER KUMAR
ENRL. ID – 2022MEB069
MEMBER 2- SHIVANSHU
ENRL. ID- 2022MEB099
MEMBER 3- DIETHOZO JABEZ VIZO
ENRL. ID – 2022MEB094

ASSIGNMENT 4
DATE -24th AUGUST ,2023
QUESTION 1.
a.
MATLAB CODE-
f=@(x) sin(x)-x*cos(x)+1;
df=@(x) cos(x)+x*sin(x)-cos(x);
e=10^-4;
x0=-2;
iv = [];
xv = [];
n=50;
if df(x0)~=0
for i =1:n
x1=x0-f(x0)/df(x0);
fprintf('x%d=%.4f\n',i,x1);
xv = [xv x1];
iv = [iv i];
if abs(f(x1))<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
plot(iv,xv,'g');
x0=x1;
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT
x1=-1.5922
x2=-1.5709
x3=-1.5708

iv =

1 2 3

xv =

-1.5922 -1.5709 -1.5708


b.
MATLAB CODE-
f=@(x) x^2+2-exp(-2*x)-x;
df=@(x) 2*x+2*exp(-2*x)-1;
e=10^-4;
x0=-2;
iv = [];
xv = [];
n=50;
if df(x0)~=0
for i =1:n
x1=x0-f(x0)/df(x0);
fprintf('x%d=%.4f\n',i,x1);
xv = [xv x1];
iv = [iv i];
if abs(f(x1))<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
plot(iv,xv,'g');
x0=x1;
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT
x1=-1.5528
x2=-1.1493
x3=-0.8190
x4=-0.6027
x5=-0.5195
x6=-0.5093
x7=-0.5091

iv =

1 2 3 4 5 6 7

xv =

Columns 1 through 5

-1.5528 -1.1493 -0.8190 -0.6027 -0.5195

Columns 6 through 7

-0.5093 -0.5091
c.
MATLAB CODE-
f=@(x) cos(x)+exp(sin(x))+x^3;
df=@(x) -sin(x)+exp(sin(x))*cos(x)+3*x^2-8*x;
e=10^-4;
x0=-2;
iv = [];
xv = [];
n=50;
if df(x0)~=0
for i =1:n
x1=x0-f(x0)/df(x0);
fprintf('x%d=%.4f\n',i,x1);
if abs(f(x1))<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
x0=x1;
plot(iv,xv,'r');
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT-
x1=-1.7212
x2=-1.5145
x3=-1.3621
x4=-1.2512
x5=-1.1715
x6=-1.1151
x7=-1.0758
x8=-1.0488
x9=-1.0304
x10=-1.0180
x11=-1.0096
x12=-1.0041
x13=-1.0003
x14=-0.9979
x15=-0.9962
x16=-0.9951
x17=-0.9944
x18=-0.9939
x19=-0.9936
x20=-0.9934
x21=-0.9932
x22=-0.9931
x23=-0.9931
x24=-0.9930
x25=-0.9930
x26=-0.9930
x27=-0.9930
iv =
Columns 1 through 8

1 2 3 4 5 6 7 8

Columns 9 through 16

9 10 11 12 13 14 15 16

Columns 17 through 24

17 18 19 20 21 22 23 24

Columns 25 through 27

25 26 27

xv =
Columns 1 through 5

-1.7212 -1.5145 -1.3621 -1.2512 -1.1715

Columns 6 through 10

-1.1151 -1.0758 -1.0488 -1.0304 -1.0180

Columns 11 through 15

-1.0096 -1.0041 -1.0003 -0.9979 -0.9962

Columns 16 through 20

-0.9951 -0.9944 -0.9939 -0.9936 -0.9934

Columns 21 through 25
-0.9932 -0.9931 -0.9931 -0.9930 -0.9930

Columns 26 through 27

-0.9930 -0.9930

d.
MATLAB CODE-
f=@(x) x^5-x^4+2*x-1;
df=@(x) 5*x^4-4*x^3+2;
e=10^-4;
x0=-2;
iv = [];
xv = [];
n=50;
if df(x0)~=0
for i =1:n
x1=x0-f(x0)/df(x0);
fprintf('x%d=%.4f\n',i,x1);
xv = [xv x1];
iv = [iv i];
if abs(f(x1))<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
plot(iv,xv,'g');
x0=x1;
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT-
x1=-1.5351
x2=-1.1248
x3=-0.7011
x4=-0.0878
x5=0.4992
x6=0.5172

iv =

1 2 3 4 5 6

xv =

Columns 1 through 5

-1.5351 -1.1248 -0.7011 -0.0878 0.4992

Column 6
0.5172

QUESTION 2.
a.
MATLAB CODE-
f=@(x) sin(x)-x*cos(x)+1;
x0=input('enter the value of x0:');
x1=input('enter the value of x1:');
e=input('enter tolerance:');
n=input('enter the number of iteration:');
gpx=0;
if f(x1)~=f(x0)
for i=1:n
x2=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));
fprintf('x%d=%.4f\n',i,x2)
if abs(x2-x1)<e
break;
end
x0=x1;
x1=x2;
gpx=gpx+1;
end
else
disp('secant method failed');
end
plot(0:gpx,x2,'.')

OUTPUT-
enter the value of x0:-2
enter the value of x1:0
enter tolerance:10^-4
enter the number of iteration:50
x1=-1.1484
x2=-2.6023
x3=-1.5008
x4=-1.5646
x5=-1.5709
x6=-1.5708
x7=-1.5708
b.
MATLAB CODE-
f=@(x) x^2+2-e^((-2)*x)-x;
x0=input('enter the value of x0:');
x1=input('enter the value of x1:');
e=input('enter tolerance:');
n=input('enter the number of iteration:');
gpx=0;
if f(x1)~=f(x0)
for i=1:n
x2=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));
fprintf('x%d=%.4f\n',i,x2)
if abs(x2-x1)<e
break;
end
x0=x1;
x1=x2;
gpx=gpx+1;
end
else
disp('secant method failed');
end
plot(0:gpx,x2,'.')

OUTPUT-
enter the value of x0:-2
enter the value of x1:0
enter tolerance:10^-4
enter the number of iteration:50
x1=0.2857
x2=0.0015
x3=0.0029
x4=0.0496
x5=0.0326
x6=0.0362
x7=0.0367
x8=0.0367
C.
MATLAB CODE-
f=@(x) cos(x)+e^(sin(x))+x^3-4*x^2;
x0=input('enter the value of x0:');
x1=input('enter the value of x1:');
e=input('enter tolerance:');
n=input('enter the number of iteration:');
gpx=0;
if f(x1)~=f(x0)
for i=1:n
x2=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));
fprintf('x%d=%.4f\n',i,x2)
if abs(x2-x1)<e
break;
end
x0=x1;
x1=x2;
gpx=gpx+1;
end
else
disp('secant method failed');
end
plot(0:gpx,x2,'.')

OUTPUT-
enter the value of x0:-2
enter the value of x1:0
enter tolerance:10^-4
enter the number of iteration:50
x1=0.0009
x2=0.2180
x3=0.4093
x4=0.5197
x5=0.5028
x6=0.5038
x7=0.5038
d.
MATLAB CODE-
f=@(x) x^5-x^4+2*x-1;
x0=input('enter the value of x0:');
x1=input('enter the value of x1:');
e=input('enter tolerance:');
n=input('enter the number of iteration:');
gpx=0;
if f(x1)~=f(x0)
for i=1:n
x2=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));
fprintf('x%d=%.4f\n',i,x2)
if abs(x2-x1)<e
break;
end
x0=x1;
x1=x2;
gpx=gpx+1;
end
else
disp('secant method failed');
end
plot(x1,n,'.')

OUTPUT
enter the value of x0:-2
enter the value of x1:0
enter tolerance:10^-4
enter the number of iteration:50
x1=0.0385
x2=0.5000
x3=0.5162
x4=0.5173
x5=0.5173
QUESTION 3.
i.

MATLAB CODE-
f=@(x) x^5+0.25*x^3-3*x^4+0.5*x^2-15*x+50;
df=@(x) 5*x^4+0.75*x^2-12*x^3+x-15;
e=10^-4;
x0=100;
iv = [];
xv = [];
n=25;
if df(x0)~=0
for i =1:n
x1=x0-(f(x0)/df(x0));
fprintf('x%d=%.4f\n',i,x1);
xv = [xv x1];
iv = [iv i];
if abs(x1-x0)<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
plot(iv,xv,'g');
x0=x1;
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT-
x1=80.1227
x2=64.2216
x3=51.5016
x4=41.3267
x5=33.1883
x6=26.6793
x7=21.4745
x8=17.3137
x9=13.9890
x10=11.3346
x11=9.2182
x12=7.5348
x13=6.2016
x14=5.1538
x15=4.3412
x16=3.7255
x17=3.2757
x18=2.9644
x19=2.7633
x20=2.6425
x21=2.5746
x22=2.5383
x23=2.5194
x24=2.5098
x25=2.5049

iv =

Columns 1 through 8

1 2 3 4 5 6
7 8

Columns 9 through 16

9 10 11 12 13 14
15 16

Columns 17 through 24
17 18 19 20 21 22
23 24

Column 25

25

xv =

Columns 1 through 5

80.1227 64.2216 51.5016 41.3267


33.1883

Columns 6 through 10

26.6793 21.4745 17.3137 13.9890


11.3346

Columns 11 through 15

9.2182 7.5348 6.2016 5.1538


4.3412

Columns 16 through 20

3.7255 3.2757 2.9644 2.7633


2.6425
Columns 21 through 25

2.5746 2.5383 2.5194 2.5098


2.5049

ii.
MATLAB CODE-
f=@(x) x^5+0.25*x^3-3*x^4+0.5*x^2-15*x+50;
df=@(x) 5*x^4+0.75*x^2-12*x^3+x-15;
e=10^-4;
x0=100;
iv = [];
xv = [];
n=25;
if df(x0)~=0
for i =1:n
x1=x0-2*(f(x0)/df(x0));
fprintf('x%d=%.4f\n',i,x1);
xv = [xv x1];
iv = [iv i];
if abs(x1-x0)<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
plot(iv,xv,'g');
x0=x1;
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT-
x1=60.2455
x2=36.3965
x3=22.0937
x4=13.5234
x5=8.4028
x6=5.3734
x7=3.6472
x8=2.7932
x9=2.5267
x10=2.5002
x11=2.5000
x12=2.5000

iv =

Columns 1 through 8

1 2 3 4 5 6 7 8

Columns 9 through 12

9 10 11 12

xv =

Columns 1 through 5

60.2455 36.3965 22.0937 13.5234 8.4028

Columns 6 through 10

5.3734 3.6472 2.7932 2.5267 2.5002

Columns 11 through 12

2.5000 2.5000
iii.
MATLAB CODE-
f=@(x) x^5+0.25*x^3-3*x^4+0.5*x^2-15*x+50;
df=@(x) 5*x^4+0.75*x^2-12*x^3+x-15;
ddf=@(x) 20*x^3+1.5*x-36*x^2+1;
e=10^-4;
x0=100;
iv = [];
xv = [];
n=25;
if df(x0)~=0
for i =1:n
x1=x0-(f(x0)*df(x0))/((df(x0))^2-
(f(x0)*ddf(x0)))
fprintf('x%d=%.4f\n',i,x1);
xv = [xv x1];
iv = [iv i];
if abs(x1-x0)<e
break;
end
if df(x1)==0
disp('Newton raphson failed');
end
plot(iv,xv,'g');
x0=x1;
end
else
disp('Newton raphson failed')
end

iv
xv

OUTPUT-
x1 =

0.6276

x1=0.6276
x1 =

1.8036

x2=1.8036

x1 =

2.3107

x3=2.3107

x1 =

2.4867
x4=2.4867

x1 =

2.4999

x5=2.4999

x1 =

2.5000

x6=2.5000

iv =

1 2 3 4 5 6
xv =

Columns 1 through 5

0.6276 1.8036 2.3107 2.4867 2.4999

Column 6
2.5000

You might also like