Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numerical Methods To Solve F (X) 0.: Graphical Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

NDSU

Numerical Methods

ECE 321 - JSG

Numerical Methods to Solve f(x)=0.


One type of problem you'll see over and over again is solving for x where f(x) = 0. For example, find the
solution to
y = (x-2)(x+3)
y=5-x
You can convert this to f(x) = 0 by
Guess x
Solve for y in each equation
Take the difference
The solution is when the difference is zero:

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)')

There are two solutions: -5 and +2.5 (ish).

January 3, 2011

NDSU

Numerical Methods

ECE 321 - JSG

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.

The midpoint is:


-->x3 = (x1+x2)/2
2.
-->f3 = (x3-2).*(x3+3) - (5-x3)
- 3.

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

ECE 321 - JSG

(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.

assuming a linear relationship, the zero crossing should be at


x x
x 3 = x 1 f(x 22)f(x1 1 ) f(x 1 )

Iterating in SciLab gives the following:


3

January 3, 2011

NDSU

Numerical Methods

ECE 321 - JSG

Your initial guess (endpoints) are -1 and +5:


-->x1 = -1;
-->f1 = (x1-2).*(x1+3) - (5-x1)
- 12.
-->x2 = 5;
-->f2 = (x2-2).*(x2+3) - (5-x2)
24.

(Iteration 1): Use linear interpolation to guess the solution


-->x3 = x1 - (x2-x1)/(f2-f1)*f1
1.
-->f3 = (x3-2).*(x3+3) - (5-x3)
- 8.

(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

ECE 321 - JSG

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:

For this problem guess x=4 for your initial guess:


-->x1 = 4;
-->f1 = (x1-2).*(x1+3) - (5-x1)
13.

(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

Check if you've found the zero crossing:


-->x1 = x3;
-->f1 = (x1-2).*(x1+3) - (5-x1)
1.6909619

(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

Check if you've found the zero crossing:


-->x1 = x3;
-->f1 = (x1-2).*(x1+3) - (5-x1)
0.0524268

January 3, 2011

NDSU

Numerical Methods

ECE 321 - JSG

(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

Check if you've found the zero crossing:


-->x1 = x3;
-->f1 = (x1-2).*(x1+3) - (5-x1)
0.0000645

That's pretty close to zero, the answer is


ans:

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

You might also like