Numerical Methods To Solve F (X) 0.: Graphical Solution
Numerical Methods To Solve F (X) 0.: Graphical Solution
Numerical Methods To Solve F (X) 0.: Graphical Solution
Numerical Methods
f(x) = (x - 2)(x + 3) - (5 - x)
Graphical Solution:
Guess what the answer is and plot f(x) in this range. Note that it helps to know the answer
(approximately) to find the answer. A plot of f(x) is as follows:
-->x = [-5:0.01:5]';
-->y1 = (x-2).*(x+3);
-->y2 = 5-x;
-->f = y1-y2;
-->plot(x,f)
-->xgrid(5);
-->xlabel('x');
-->ylabel('f(x)')
January 3, 2011
NDSU
Numerical Methods
Interval Halving:
Specify to points:
One where f(x1) < 0
One where f(x2) > 0
Try the midpoint between x1 and x2.
x3 = (x1 + x2)/2
If f(x3) > 0, replace x2 with x3.
If f(x3) < 0, replace x1 with x3.
For example, let the initial guess be
x1 = -1
f(x1) < 0
x2 = +5
f(x2) > 0
-->x1 = -1;
-->f1 = (x1-2).*(x1+3) - (5-x1)
- 12.
-->x2 = +5;
-->f2 = (x2-2).*(x2+3) - (5-x2)
24.
This is negative, so replace the left endpoint (x1) with x3 and repeat
-->x1 = x3;
-->x3 = (x1+x2)/2
3.5
-->f3 = (x3-2).*(x3+3) - (5-x3)
8.25
(Iteration 2): Now the midpoint is 3.5 and f() is positive. Replace the right endpoint with the midpoint:
-->x2 = x3;
-->x3 = (x1+x2)/2
2.75
-->f3 = (x3-2).*(x3+3) - (5-x3)
2.0625
(Iteration 3): Again, f() at the midpoint is positive, so replace the right endpoint
-->x2 = x3;
-->x3 = (x1+x2)/2
2.375
-->f3 = (x3-2).*(x3+3) - (5-x3)
- 0.609375
2
January 3, 2011
NDSU
Numerical Methods
(Iteration 4)
-->x1 = x3;
-->x3 = (x1+x2)/2
2.5625
-->f3 = (x3-2).*(x3+3) - (5-x3)
0.6914062
(Iteration 5)
-->x2 = x3;
-->x3 = (x1+x2)/2
2.46875
-->f3 = (x3-2).*(x3+3) - (5-x3)
0.0322266
Keep going until f() is close enough to zero. The answer is close to .246875.
California Method:
This is about the same as interval halving, but interpolate to find the next guess rather than using the
midpoint. For example, for the previous figure, you'd interpolate by assuming a linear relationship for
f(x) and find the zero crossing given the endpoints. In this case, you'd guess x = +1 (the zero crossing
assuming a linear interpolation between the endpoints.) Since f(x1) is negative, replace the right endpoint
and repeat.
January 3, 2011
NDSU
Numerical Methods
(Iteration 2): This doesn't give f(x) = 0, so keep iterating. Replace the left endpoint and repeat.
-->x1 = x3;
-->f1 = (x1-2).*(x1+3) - (5-x1);
-->x3 = x1 - (x2-x1)/(f2-f1)*f1
2.
-->f3 = (x3-2).*(x3+3) - (5-x3)
- 3.
(Iteration 3): This doesn't give f(x) = 0, so keep iterating. Replace the left endpoint and repeat.
-->x1 = x3;
-->f1 = (x1-2).*(x1+3) - (5-x1)
-->x3 = x1 - (x2-x1)/(f2-f1)*f1
2.3333333
-->f3 = (x3-2).*(x3+3) - (5-x3)
- 0.8888889
(Iteration 4): This doesn't give f(x) = 0, so keep iterating. Replace the left endpoint and repeat.
-->x1 = x3;
-->f1 = (x1-2).*(x1+3) - (5-x1)
-->x3 = x1 - (x2-x1)/(f2-f1)*f1
2.4285714
-->f3 = (x3-2).*(x3+3) - (5-x3)
- 0.2448980
And so on. When you get close enough to f() = 0, you have fond the solution. It's close to 2.42857.
January 3, 2011
NDSU
Numerical Methods
Newton's Method:
With Newton's method, you just need an initial guess. Find the tangent at that point and see where it
intersects the axis. This is your next guess:
(Iteration 1): f() isn't zero, so find the tangent, find it's zero crossing, and take that as your next guess:
-->x2 = x1 + 0.001;
-->f2 = (x2-2).*(x2+3) - (5-x2)
13.010001
-->x3 = x1 - (x2-x1)/(f2-f1)*f1
2.70013
(Iteration 2): f() isn't zero, so find the tangent, find it's zero crossing, and take that as your next guess:
-->x2 = x1 + 0.001;
-->f2 = (x2-2).*(x2+3) - (5-x2)
1.6983632
-->x3 = x1 - (x2-x1)/(f2-f1)*f1
2.4716605
January 3, 2011
NDSU
Numerical Methods
(Iteration 3): f() isn't zero, so find the tangent, find it's zero crossing, and take that as your next guess:
-->x2 = x1 + 0.001;
-->f2 = (x2-2).*(x2+3) - (5-x2)
0.0593711
-->x3 = x1 - (x2-x1)/(f2-f1)*f1
2.4641109
x = 2.641109
Note that Newton's method converges much faster and you don't need an initial guess that's above and
below zero. You do need an initial guess, however.
It helps to know what the answer is to find the answer,
Methods with the name of 'Newton' or 'Gauss' tend to be good methods to use.
January 3, 2011