You are on page 1of 12

Name: insert name here

MECH 2220 Lecture Homework 4

Lecture HW 4 Root Finding


Assigned:
Due:

February 12, 2013


February 19, 2013 (11:00 a.m.)

Name: insert name here


MECH 2220 Lecture Homework 4

Problem Statement #1:


Write a MATLAB script that will find the roots of a given equations using the BISECTION
METHOD. Format your output to look similar to the examples given. You should write your
output to a file. Set the maximum number of iterations to 100 and the tolerance to 0.000005.
Use a tolerance definition of

bn an
where an & bn are the interval endpoints for the nth
2

iteration
Use the script to solve the given equations. Use the interval endpoints as your initial a and b.
Eq1 f(x) = ex - (x2 + 4) = 0 on [2,3]
Eq2

f(x) = x - 2-x = 0 on [1/3,1]

Eq3

f(x) = x3 - 1.8999 x2 + 1.5796 x - 2.1195 = 0 on [1,2].

Code for problem #1:

function [est_x,est_y] =
bisectionmethod(a,b,f)
maxIterations = 100;
minTolerance = 0.000005;
fileID=fopen('bisect.txt','wt+');
fprintf(fileID,'n\t\ta\t\t\tb\t\t\tp\t\t\tf
(p)\n');
if f(a)*f(b) > 0
error('Bad bounds!')
end
if f(a)*f(b) == 0
error('One is already 0.

Bad bounds!')

Name: insert name here


MECH 2220 Lecture Homework 4

end
n=1;
p=(a+b)/2;
while n<=maxIterations && abs(f(p( max([n1,1]) )))>=minTolerance
%
if f(a(n))*f(b(n)) < 0
p(n)=(a(n)+b(n))/2;
if f(a(n))*f(p(n))<0 %Good bounds at a
and p.
a(n+1)=a(n);
b(n+1)=p(n);
elseif f(b(n))*f(p(n))<0 %Now b and p
are good bounds.
a(n+1)=p(n);
b(n+1)=b(n);
end
fprintf(fileID,'%3.0f\t\t%3.6f\t\t
%3.6f\t\t%3.6f\t\t
%3.6f\n',n,a(n),b(n),p(n),f(p(n)));
n=n+1;
end
est_x = p(n-1);
est_y = f(p(n-1));
fprintf(fileID,'The root to the equation on
[%2.3f,%2.3f] was found to be %3.6f where
f(%3.6f)=%3.6f.\n',a(1),b(1),p(n-1),p(n1),f(p(n-1)));
fclose(fileID);
bisectionmethod(2,3,@eq1)
bisectionmethod((1/3),1,@eq2)

Name: insert name here


MECH 2220 Lecture Homework 4
bisectionmethod(1,2,@eq3)

function y = eq1(x)
y=exp(x)-(x^2+4);
end
function y = eq2(x)
y = x-2^(-x);
end
function y = eq3(x)
y=x^3-1.8999*x^2+1.5796*x-2.1195;
end
Solution for problem #1:
n

a
b
p
f(p)
1
2.000000
3.000000
2.500000
1.932494
2
2.000000
2.500000
2.250000
0.425236
3
2.000000
2.250000
2.125000
-0.142728
4
2.125000
2.250000
2.187500
0.127747
5
2.125000
2.187500
2.156250
-0.010732
6
2.156250
2.187500
2.171875
0.057680
7
2.156250
2.171875
2.164063
0.023269
8
2.156250
2.164063
2.160156
0.006218
9
2.156250
2.160156
2.158203
-0.002270
10
2.158203
2.160156
2.159180
0.001971
11
2.158203
2.159180
2.158691
-0.000151
12
2.158691
2.159180
2.158936
0.000910
13
2.158691
2.158936
2.158813
0.000380
14
2.158691
2.158813
2.158752
0.000115
15
2.158691
2.158752
2.158722
-0.000018
16
2.158722
2.158752
2.158737
0.000048
17
2.158722
2.158737
2.158730
0.000015
18
2.158722
2.158730
2.158726
-0.000001
The root to the equation on [ 2, 3] was found to be 2.158726 where f(2.158726)=-0.000001.
n
1
2

a
0.333333
0.333333

b
1.000000
0.666667

p
0.666667
0.500000

f(p)
0.036706
-0.207107

Name: insert name here


MECH 2220 Lecture Homework 4
3
0.500000
0.666667
0.583333
-0.084087
4
0.583333
0.666667
0.625000
-0.023420
5
0.625000
0.666667
0.645833
0.006710
6
0.625000
0.645833
0.635417
-0.008338
7
0.635417
0.645833
0.640625
-0.000810
8
0.640625
0.645833
0.643229
0.002951
9
0.640625
0.643229
0.641927
0.001071
10
0.640625
0.641927
0.641276
0.000130
11
0.640625
0.641276
0.640951
-0.000340
12
0.640951
0.641276
0.641113
-0.000105
13
0.641113
0.641276
0.641195
0.000013
14
0.641113
0.641195
0.641154
-0.000046
15
0.641154
0.641195
0.641174
-0.000017
16
0.641174
0.641195
0.641184
-0.000002
The root to the equation on [ 0.333, 1] was found to be 0.641184 where f(0.641184)=0.000002.
n

a
b
p
f(p)
1
1.000000
2.000000
1.500000
-0.649875
2
1.500000
2.000000
1.750000
0.185731
3
1.500000
1.750000
1.625000
-0.278558
4
1.625000
1.750000
1.687500
-0.058767
5
1.687500
1.750000
1.718750
0.060302
6
1.687500
1.718750
1.703125
-0.000016
7
1.703125
1.718750
1.710938
0.029946
8
1.703125
1.710938
1.707031
0.014916
9
1.703125
1.707031
1.705078
0.007437
10
1.703125
1.705078
1.704102
0.003708
11
1.703125
1.704102
1.703613
0.001845
12
1.703125
1.703613
1.703369
0.000914
13
1.703125
1.703369
1.703247
0.000449
14
1.703125
1.703247
1.703186
0.000216
15
1.703125
1.703186
1.703156
0.000100
16
1.703125
1.703156
1.703140
0.000042
17
1.703125
1.703140
1.703133
0.000013
18
1.703125
1.703133
1.703129
-0.000002
The root to the equation on [1.000,2.000] was found to be 1.703129 where f(1.703129)=0.000002.

Name: insert name here


MECH 2220 Lecture Homework 4

Name: insert name here


MECH 2220 Lecture Homework 4

Problem Statement #2:


Do problem 5.9 (5.8 in the third edition) in the text

Code for problem #2:

%Call functions for 5.8


global osf
osf = 8;
T_8=bisectionmethod(0,35,@problem8)
osf=10;
T_10=bisectionmethod(0,35,@problem8)
osf=14;
T_14=bisectionmethod(0,35,@problem8)

Name: insert name here


MECH 2220 Lecture Homework 4

function y=problem8(x)
global osf
y=-log(osf)-139.34411+1.575701e5/
(x+273.15)-6.642308e7/
(x+273.15)^2+1.2438e10/(x+273.15)^38.621949e11/(x+273.15)^4;
end
Solution for problem #2:
n

a
b
p
f(p)
1
0.000000
35.000000
17.500000
0.178671
2
17.500000
35.000000
26.250000
0.009568
3
26.250000
35.000000
30.625000
-0.067487
4
26.250000
30.625000
28.437500
-0.029485
5
26.250000
28.437500
27.343750
-0.010097
6
26.250000
27.343750
26.796875
-0.000300
7
26.250000
26.796875
26.523438
0.004625
8
26.523438
26.796875
26.660156
0.002160
9
26.660156
26.796875
26.728516
0.000929
10
26.728516
26.796875
26.762695
0.000314
11
26.762695
26.796875
26.779785
0.000007
12
26.779785
26.796875
26.788330
-0.000147
13
26.779785
26.788330
26.784058
-0.000070
14
26.779785
26.784058
26.781921
-0.000032
15
26.779785
26.781921
26.780853
-0.000012
16
26.779785
26.780853
26.780319
-0.000003
The root to the equation on [0.000,35.000] was found to be 26.780319 where f(26.780319)=0.000003.
T_8 =
26.7803
n
1
2
3

a
0.000000
0.000000
8.750000

b
35.000000
17.500000
17.500000

p
17.500000
8.750000
13.125000

f(p)
-0.044472
0.150964
0.049486

Name: insert name here


MECH 2220 Lecture Homework 4
4
13.125000
17.500000
15.312500
0.001624
5
15.312500
17.500000
16.406250
-0.021637
6
15.312500
16.406250
15.859375
-0.010061
7
15.312500
15.859375
15.585938
-0.004232
8
15.312500
15.585938
15.449219
-0.001307
9
15.312500
15.449219
15.380859
0.000158
10
15.380859
15.449219
15.415039
-0.000575
11
15.380859
15.415039
15.397949
-0.000209
12
15.380859
15.397949
15.389404
-0.000026
13
15.380859
15.389404
15.385132
0.000066
14
15.385132
15.389404
15.387268
0.000020
15
15.387268
15.389404
15.388336
-0.000003
The root to the equation on [0.000,35.000] was found to be 15.388336 where f(15.388336)=0.000003.
T_10 =
15.3883
n

a
b
p
1
0.000000
35.000000
17.500000
2
0.000000
17.500000
8.750000
3
0.000000
8.750000
4.375000
4
0.000000
4.375000
2.187500
5
0.000000
2.187500
1.093750
6
1.093750
2.187500
1.640625
7
1.093750
1.640625
1.367188
8
1.367188
1.640625
1.503906
9
1.503906
1.640625
1.572266
10
1.503906
1.572266
1.538086
11
1.538086
1.572266
1.555176
12
1.538086
1.555176
1.546631
13
1.546631
1.555176
1.550903
14
1.550903
1.555176
1.553040
15
1.553040
1.555176
1.554108
16
1.554108
1.555176
1.554642
The root to the equation on [0.000,35.000] was found to be 1.554642 where
f(1.554642)=0.000001.

f(p)
-0.380945
-0.185509
-0.075641
-0.017313
0.012737
-0.002363
0.005168
0.001398
-0.000484
0.000456
-0.000014
0.000221
0.000104
0.000045
0.000016
0.000001

Name: insert name here


MECH 2220 Lecture Homework 4

Problem Statement #3:


Do problem 5.13 (same number for second and third edition) in the text.

Code for problem #3:

function dydx = problem13(x)


w0=2.5;
L=600;

Name: insert name here


MECH 2220 Lecture Homework 4

I=30e3;
E=50e3;
dydx=w0/(120*E*I*L) * (-5*x^4+6*L^2*x^2L^4);
bisectionmethod(0,550,@problem13)

Solution for problem #3:


n

a
b
p
1
0.000000
550.000000
275.000000
2
0.000000
275.000000
137.500000
3
137.500000
275.000000
206.250000
4
206.250000
275.000000
240.625000
5
240.625000
275.000000
257.812500
6
257.812500
275.000000
266.406250
7
266.406250
275.000000
270.703125
8
266.406250
270.703125
268.554688
The root to the equation on [0.000,550.000] was found to be 268.554688 where
f(268.554688)=0.000004.

f(p)
0.000119
-0.002096
-0.001082
-0.000493
-0.000188
-0.000034
0.000042
0.000004

Name: insert name here


MECH 2220 Lecture Homework 4

Sample Output for Equation 3:


Joe Ragan
Bisection Routine
The root to the equation f= x^3-1.8999*x^2+1.5796*x-2.1195 =0 on [1,2]
was found to be 1.703133 where f(1.703133)=0.000013.
The 17 iterations for the bisection routine were recorded as:
n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

a
1.000000
1.500000
1.500000
1.625000
1.687500
1.687500
1.703125
1.703125
1.703125
1.703125
1.703125
1.703125
1.703125
1.703125
1.703125
1.703125
1.703125

b
2.000000
2.000000
1.750000
1.750000
1.750000
1.718750
1.718750
1.710938
1.707031
1.705078
1.704102
1.703613
1.703369
1.703247
1.703186
1.703156
1.703140

p
1.500000
1.750000
1.625000
1.687500
1.718750
1.703125
1.710938
1.707031
1.705078
1.704102
1.703613
1.703369
1.703247
1.703186
1.703156
1.703140
1.703133

f(p)
-0.649875
0.185731
-0.278558
-0.058767
0.060302
-0.000016
0.029946
0.014916
0.007437
0.003708
0.001845
0.000914
0.000449
0.000216
0.000100
0.000042
0.000013

You might also like