You are on page 1of 12

%Bisection method

clear all
syms y;
func=input('Function : ');
F = inline(func);
xl=input('Lower Limit, xl : ');
xu=input('Upper Limit, xu : ');
es=input('Stopping Criterion, es : ');
disp(' ');
flag=0; i=1;
xr = 0; % Dummy value for xr.
Fl=F(xl); Fu=F(xu);
if Fl*Fu > 0
disp('Error: The function has the same sign at points xl and xu.')
else
disp('iteration
xl
xu
xr
f(xr)
ea')
while flag == 0
xrold = xr;
xr = (xl + xu)/2;
Fr=F(xr);
ea=abs((xr-xrold)/xr)*100;
fprintf('%5i %11.4f %11.4f %11.4f %11.4f %11.4f\n',i, xl, xu, xr, Fr,
ea)
i = i+1;
if Fr == 0
fprintf('\n An exact solution y =%11.6f was found \n',xr)
flag=1;
break
end
if ea < es
fprintf('\n The approximate solution is y = %11.6f \n ',xr)
flag=1;
break
end
if Fl*Fr < 0
xu = xr;
else
xl = xr;
end
end
end
disp(' ');

%False Position Method


clear all
syms y;
func=input('Function : ');
F = inline(func);
xl=input('Lower Limit, xl : ');
xu=input('Upper Limit, xu : ');
es=input('Stopping Criterion, es : ');
disp(' ');
flag=0; i=1;

box 1

xr = 0; % Dummy value for xr.


Fl=F(xl); Fu=F(xu);
if Fl*Fu > 0
disp('Error: The function has the same sign at points xl and xu.')
else
disp('iteration
xl
xu
xr
f(xr)
ea')
while flag == 0
xrold = xr;
xr = xu-(Fu*(xl-xu))/(Fl-Fu);
Fr=F(xr);
ea=abs((xr-xrold)/xr)*100;
fprintf('%5i %11.4f %11.4f %11.4f %11.4f %11.4f\n',i, xl, xu, xr, Fr,
ea)
i = i+1;
if Fr == 0
fprintf('\n An exact solution y =%11.6f was found \n',xr)
flag=1;
break
end
if ea < es
fprintf('\n The approximate solution is y = %11.6f \n ',xr)
flag=1;
break
end
if Fl*Fr
xu =
Fu =
else
xl =
Fl =
end
end
end
disp(' ');

< 0
xr;
F(xu);
xr;
F(xl);
box 2

Modify by add this code into box 1


il = 0; iu = 0;
and replace this code into box 2
if Fl*Fr < 0
xu
Fu
iu
il
if

= xr;
= F(xu);
= 0;
= il +1;
il >= 2
Fl = Fl/2;

end
else
xl
Fl
il
iu
if
end
end

= xr;
= F(xl);
= 0;
= iu +1;
iu >= 2;
Fu = Fu/2;

%Newton Raphson Method


clear all
syms y;
func=input('Function : ');
F = inline(func);
DiffF=inline (diff(func,y));
x0=input('Initial Guess, x0 : ');
es=input('Stopping Criterion, es : ');
imax=input('iteration limit, imax : ');
disp(' ');
xi=x0;
disp('

xi

x[i+1]

F(xi)

DiffF(xi)

ea')

for i = 0:imax
if F(xi) == 0
fprintf('An exact solution y =%11.6f was found',xi)
break
end
if DiffF(xi) == 0
fprintf('Error: Zero gradient')
break
end
xinew=xi-F(xi)/DiffF(xi);
ea=abs((xinew-xi)/xinew)*100;
fprintf('%5i %11.4f %11.4f %11.4f %11.4f %11.4f\n',i, xi, xinew, F(xi),
DiffF(xi), ea)
xi=xinew;
if ea < es
if F(xi) >= -0.05 && F(xi) <= 0.05 && xi > 0;
fprintf('\n The approximate solution is y = %11.6f \n ',xi)
break
else
fprintf('\n Solution was not obtained ,try use other initial
guess \n ')
break
end
end
if i == imax
fprintf('\n Solution was not obtained in %i iterations \n',imax)
break
end
end
disp('

');

%Secant Method
clear all
syms y;
func=input('Function : ');
F = inline(func);
x0=input('Initial Guess 1, x0 : ');
ximin1=input('Initial Guess 2, x[-1] : ');
es=input('Stopping Criterion, es : ');
imax=input('iteration limit, imax : ');
disp(' ');
disp('
i
x[i-1]
xi
x[i+1]
ea')

F(xi)

F(x[i-1])

xinew=0;
for i = 0:imax
if i==0
xi=x0;
else
xi=xinew;
end
xiold=xinew;
xinew=xi-(F(xi)*(ximin1-xi))/(F(ximin1)- F(xi));
ea=abs((xinew-xiold)/xinew)*100;
if F(xinew) == 0
fprintf('An exact solution y =%11.6f was found',xinew)
break
end
fprintf('%5i %11.4f %11.4f %11.4f %11.4f %11.4f %11.4f\n',i, ximin1, xi,
xinew, F(xi), F(ximin1), ea)
ximin1=xi;
if ea < es
if F(xinew) >= -0.05 && F(xinew) <= 0.05 && xinew > 0;
fprintf('\n The approximate solution is y = %11.6f \n ',xinew)
break
else
fprintf('\n Solution was not obtained ,try use other initial
guess \n ')
break
end
end
if i == imax
fprintf('\n Solution was not obtained in %i iterations \n',imax)
break
end
end
disp(' ');

>> Bisection
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Lower Limit, xl : 0.5
Upper Limit, xu : 2.5
Stopping Criterion, es : 0.01

iteration

xl

xu

xr

0.5000

2.5000

1.5000

-0.0309

100.0000

1.5000

2.5000

2.0000

0.6018

25.0000

1.5000

2.0000

1.7500

0.3789

14.2857

1.5000

1.7500

1.6250

0.2069

7.6923

1.5000

1.6250

1.5625

0.0980

4.0000

1.5000

1.5625

1.5313

0.0363

2.0408

1.5000

1.5313

1.5156

0.0034

1.0309

1.5000

1.5156

1.5078

-0.0136

0.5181

1.5078

1.5156

1.5117

-0.0051

0.2584

10

1.5117

1.5156

1.5137

-0.0008

0.1290

11

1.5137

1.5156

1.5146

0.0013

0.0645

12

1.5137

1.5146

1.5142

0.0002

0.0322

13

1.5137

1.5142

1.5139

-0.0003

0.0161

14

1.5139

1.5142

1.5140

-0.0000

0.0081

The approximate solution is y =

1.514038

f(xr)

ea

>> FalsePos
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Lower Limit, xl : 0.5
Upper Limit, xu : 2.5
Stopping Criterion, es : 0.01

iteration

xl

xu

xr

f(xr)

ea

0.5000

2.5000

2.4508

0.7999

100.0000

0.5000

2.4508

2.4036

0.7861

1.9638

0.5000

2.4036

2.3583

0.7718

1.9203

0.5000

2.3583

2.3149

0.7569

1.8758

0.5000

2.3149

2.2733

0.7414

1.8303

0.5000

2.2733

2.2335

0.7255

1.7839

0.5000

2.2335

2.1953

0.7090

1.7367

.
.
.
89

0.5000

1.5172

1.5170

0.0063

0.0140

90

0.5000

1.5170

1.5168

0.0058

0.0130

91

0.5000

1.5168

1.5166

0.0055

0.0121

92

0.5000

1.5166

1.5164

0.0051

0.0113

93

0.5000

1.5164

1.5163

0.0047

0.0106

94

0.5000

1.5163

1.5161

0.0044

0.0098

The approximate solution is y =

1.516107

>> ModFalsePos
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Lower Limit, xl : 0.5
Upper Limit, xu : 2.5
Stopping Criterion, es : 0.01

iteration

xl

xu

xr

0.5000

2.5000

2.4508

0.7999

100.0000

0.5000

2.4508

2.4036

0.7861

1.9638

0.5000

2.4036

2.3152

0.7570

3.8213

0.5000

2.3152

2.1594

0.6923

7.2131

0.5000

2.1594

1.9162

0.5406

12.6894

0.5000

1.9162

1.6168

0.1936

18.5208

0.5000

1.6168

1.4369

-0.1862

12.5234

1.4369

1.6168

1.5251

0.0234

5.7840

1.4369

1.5251

1.5152

0.0025

0.6502

10

1.4369

1.5152

1.5132

-0.0019

0.1349

11

1.5132

1.5152

1.5141

0.0000

0.0586

12

1.5132

1.5141

1.5141

0.0000

0.0001

The approximate solution is y =

1.514055

f(xr)

ea

>> NewtonRaphson
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Initial Guess, x0 : 0.5
Stopping Criterion, es : 0.01
iteration limit, imax : 15

xi

x[i+1]

F(xi)

DiffF(xi)

ea

0.5000

0.6571

-32.2582

205.3969

23.9026

0.6571

0.8531

-13.2549

67.6118

22.9803

0.8531

1.0807

-5.2898

23.2395

21.0618

1.0807

1.3052

-1.9706

8.7770

17.2018

1.3052

1.4598

-0.6200

4.0123

10.5848

1.4598

1.5101

-0.1266

2.5158

3.3330

1.5101

1.5140

-0.0086

2.1830

0.2608

1.5140

1.5141

-0.0000

2.1593

0.0014

The approximate solution is y =

1.514055

>> Secant
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Initial Guess 1, x0 : 2.5
Initial Guess 2, x[-1] : 0.5
Stopping Criterion, es : 0.01
iteration limit, imax : 15

x[i-1]

0.5000

xi

x[i+1]

F(xi)

F(x[i-1])

ea

2.5000

2.4508

0.8130

-32.2582

100.0000

2.5000

2.4508

-0.5379

0.7999

0.8130

555.6171

2.4508

-0.5379

2.5259

32.6642

0.7999

121.2963

-0.5379

2.5259

2.6047

0.8195

32.6642

3.0270

2.5259

2.6047

-1.0445

0.8376

0.8195

349.3667

2.6047

-1.0445

3.2466

5.5996

0.8376

132.1731

-1.0445

3.2466

4.0954

0.9247

5.5996

20.7252

3.2466

4.0954

-15.1910

0.9672

0.9247

126.9592

4.0954

-15.1910

549.3729

1.0015

0.9672

102.7651

-15.1910

549.3729 386945.3715

1.0000

1.0015

99.8580

.
.
.
13

-Inf

NaN

NaN

NaN

NaN

NaN

14

NaN

NaN

NaN

NaN

NaN

NaN

15

NaN

NaN

NaN

NaN

NaN

NaN

Solution was not obtained in 15 iterations

>> Secant
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Initial Guess 1, x0 : 2
Initial Guess 2, x[-1] : 1
Stopping Criterion, es : 0.01
iteration limit, imax : 15

x[i-1]

1.0000

xi

x[i+1]

F(xi)

2.0000

1.8233

0.6018

-2.8041

100.0000

2.0000

1.8233

1.2611

0.4579

0.6018

44.5834

1.8233

1.2611

1.6203

-0.8104

0.4579

22.1714

1.2611

1.6203

1.5494

0.1994

-0.8104

4.5779

1.6203

1.5494

1.5087

0.0727

0.1994

2.6988

1.5494

1.5087

1.5143

-0.0117

0.0727

0.3728

1.5087

1.5143

1.5141

0.0006

-0.0117

0.0174

1.5143

1.5141

1.5141

0.0000

0.0006

0.0001

The approximate solution is y =

1.514055

F(x[i-1])

ea

>> FalsePos
Function : 1-20^2/(9.81*(3*y+y^2/2)^3)*(3+y)
Lower Limit, xl : 1
Upper Limit, xu : 2
Stopping Criterion, es : 0.01

iteration

xl

xu

xr

1.0000

2.0000

1.8233

0.4579

100.0000

1.0000

1.8233

1.7077

0.3266

6.7676

1.0000

1.7077

1.6339

0.2210

4.5192

1.0000

1.6339

1.5876

0.1438

2.9174

1.0000

1.5876

1.5589

0.0911

1.8392

1.0000

1.5589

1.5413

0.0567

1.1413

1.0000

1.5413

1.5306

0.0349

0.7010

1.0000

1.5306

1.5241

0.0213

0.4278

1.0000

1.5241

1.5201

0.0130

0.2601

10

1.0000

1.5201

1.5177

0.0079

0.1577

11

1.0000

1.5177

1.5163

0.0048

0.0955

12

1.0000

1.5163

1.5154

0.0029

0.0578

13

1.0000

1.5154

1.5149

0.0017

0.0349

14

1.0000

1.5149

1.5145

0.0011

0.0211

15

1.0000

1.5145

1.5143

0.0006

0.0128

16

1.0000

1.5143

1.5142

0.0004

0.0077

The approximate solution is y =

1.514233

f(xr)

ea

You might also like