0% found this document useful (0 votes)
47 views5 pages

Numerical Methods for Root Finding

This document contains code examples demonstrating different numerical methods for finding the roots of functions, including linear search, bisection, secant, false position, fixed point, and Newton-Raphson methods. It defines functions for each method and test functions to apply the methods to. The main method runs tests of each numerical method by applying it to a test function and outputting the solution.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views5 pages

Numerical Methods for Root Finding

This document contains code examples demonstrating different numerical methods for finding the roots of functions, including linear search, bisection, secant, false position, fixed point, and Newton-Raphson methods. It defines functions for each method and test functions to apply the methods to. The main method runs tests of each numerical method by applying it to a test function and outputting the solution.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

...hapter09_CodeExamples\Chapter09_CodeExamples\Program.

cs 1
1 using System;
2
3
4
5
6 namespace Chapter09_CodeExamples
7 {
8 class Program
9 {
10 public delegate double Function(double x);
11
12 #region Linear Search
13 public static double LinearIncrementalSearch(Function f, double 
xstart, double deltaX, int nMaxInc)
14 {
15 double x = xstart;
16 double fstart = f(x);
17 double fx = fstart;
18 double fProd = 0;
19 for (int i = 0; i < nMaxInc; i++)
20 {
21 x = xstart + i * deltaX;
22 fx = f(x);
23 fProd = fstart * fx;
24 if (fProd < 0)
25 break;
26 }
27 if (fProd > 0)
28 throw new Exception("Solution not found!");
29 else
30 {
31 return  = x ­ deltaX * fx / (fx ­ f(x ­ deltaX));
32 }
33 }
34 #endregion
35
36 #region Bisection Method
37 public static double BisectionMethod(Function f, double a, double b, 
double epsilon)
38 {
39 double x1 = a;
40 double x2 = b;
41 double fb = f(b);
42 while ([Link](x2 ­ x1) > epsilon)
43 {
44 double midpt = 0.5 * (x1 + x2);
45 if (fb * f(midpt) > 0)
46 x2 = midpt;
47 else
48 x1 = midpt;
49 }
50 return x2 ­ (x2 ­ x1) * f(x2) / (f(x2) ­ f(x1));
51 }
52 #endregion
53
54 #region Secant Method
...hapter09_CodeExamples\Chapter09_CodeExamples\[Link] 2
55 public static double SecantMethod(Function f, double a, double b, 
double epsilon)
56 {
57 double x1 = a;
58 double x2 = b;
59 double fb = f(b);
60 while ([Link](f(x2)) > epsilon)
61 {
62 double mpoint = x2 ­ (x2 ­ x1) * fb / (fb ­ f(x1));
63 x1 = x2;
64 x2 = mpoint;
65 fb = f(x2);
66 }
67 return x2;
68 }
69 #endregion
70
71 #region False Position Method
72 public static double FalsePositionMethod(Function f, double a, double 
b, double epsilon)
73 {
74 double x1 = a;
75 double x2 = b;
76 double fb = f(b);
77 while ([Link](x2 ­ x1) > epsilon)
78 {
79 double xpoint = x2 ­ (x2 ­ x1) * f(x2) / (f(x2) ­ f(x1));
80 if (fb * f(xpoint) > 0)
81 x2 = xpoint;
82 else
83 x1 = xpoint;
84 if ([Link](f(xpoint)) < epsilon)
85 break;
86 }
87 return x2 ­ (x2 ­ x1) * f(x2) / (f(x2) ­ f(x1));
88 }
89 #endregion
90
91 #region Fixed Point Method
92 public static double FixedPointMethod(Function f, double x0, double 
epsilon, int nMaxIter)
93 {
94 double x1 = x0;
95 double  = x0;
96 double currEpsilon = 0.0;
97 for (int i = 0; i < nMaxIter; i++)
98 {
99 x2 = f(x1);
100 currEpsilon = [Link](x1 ­ x2);
101 x1 = x2;
102 if (currEpsilon < epsilon)
103 break;
104 }
105 if (currEpsilon > epsilon)
106 {
107 throw new Exception("Solution not found!");
...hapter09_CodeExamples\Chapter09_CodeExamples\[Link] 3
108 }
109 return x1;
110 }
111 #endregion
112
113 #region Newton­Raphson Method
114 public static double NewtonRaphsonMethod(Function f, Function fprime, 
double x0, double epsilon)
115 {
116 double f0 = f(x0);
117 double x = x0;
118 while ([Link](f(x)) > epsilon)
119 {
120 x ­= f0 / fprime(x);
121 f0 = f(x);
122 }
123 return x;
124 }
125 #endregion
126
127 #region Test Functions
128 static double F(double x)
129 {
130 //Setup for test case: x^3 ­ 5x + 3 = 0
131 return x * x * x ­ 5.0 * x + 3.0;
132 }
133
134 static double Ffixpt(double x)
135 {
136 //Setup for test case: x^2 + 2x ­ 35 = 0
137 //Test function = sqrt(35 ­ 2x)
138 return [Link](35.0 ­ 2.0 * x);
139 }
140
141 static double F1(double x)
142 {
143 return [Link](x) ­ x * x * x * x;
144 }
145
146 static double F1prime(double x)
147 {
148 return [Link](x) ­ 4.0 * x * x * x;
149 }
150 #endregion
151
152 static void Main(string[]  )
153 {
154 try
155 {
156 [Link]("\n\nTesting Linear Incremental Search 
Methodn");
157 double deltaX = 0.01;
158 int n = 500;
159 double x = ­4.0;
160 for (int i = 1; i <= 3; i++)
161 {
...hapter09_CodeExamples\Chapter09_CodeExamples\[Link] 4
162 x = LinearIncrementalSearch(F, x, deltaX, n);
163 [Link]("\nSolution " + [Link]() + " = " + 
[Link]());
164 [Link]("Solution confirmation: f(x) = " + F
(x).ToString());
165 }
166 [Link]();
167
168 [Link]("\n\nTesting Bisection Method\n");
169 x = BisectionMethod(F, 1.0, 2.0, 0.0001);
170 [Link]("Solution from the bisection method: " + 
[Link]());
171 [Link]("Solution confirmation: f(x) = " + F
(x).ToString());
172 [Link]();
173
174 [Link]("\n\nTesting Secant Method\n");
175 x = SecantMethod(F, 1.0, 1.5, 0.0001);
176 [Link]("Solution from the secant method: " + 
[Link]());
177 [Link]("Solution confirmation: f(x) = " + F
(x).ToString());
178 [Link]();
179
180 [Link]("\n\nTesting False Position Method\n");
181 x = FalsePositionMethod(F, 1.0, 2.0, 0.0001);
182 [Link]("Solution from the false position method: " 
+ [Link]());
183 [Link]("Solution confirmation: f(x) = " + F
(x).ToString());
184 [Link]();
185
186 [Link]("\n\nTesting Fixed Point Method\n");
187 double tol = 0.0001;
188 n = 10000;
189 double x0 = 1.6;
190 x = FixedPointMethod(Ffixpt, x0, tol, n);
191 [Link]("solution from the fixed point method: " + 
[Link]());
192 [Link]("Expected solution = 5.00");
193 [Link]();
194
195 [Link]("\n\nTesting Testing Newton­Raphson Method
\n");
196 x = NewtonRaphsonMethod(F1, F1prime, 1.0, 0.0001);
197 [Link]("Solution from the Newton­Raphson method: " 
+ [Link]());
198 [Link]("Solution confirmation: f(x) = " + F1
(x).ToString());
199 [Link]();
200 }
201 catch (Exception ex)
202 {
203 [Link]("Fatal error: " + [Link]);
204 [Link]();
205 }
...hapter09_CodeExamples\Chapter09_CodeExamples\[Link] 5
206 }
207 }
208 }
209

You might also like