Lecture HW 04 Roots
Lecture HW 04 Roots
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
Eq3
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!')
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)
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
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.
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
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
I=30e3;
E=50e3;
dydx=w0/(120*E*I*L) * (-5*x^4+6*L^2*x^2L^4);
bisectionmethod(0,550,@problem13)
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
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