0% found this document useful (0 votes)
127 views

Source Code Example Root Finding Visual Studio

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Source Code Example Root Finding Visual Studio

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

...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 (Math.Abs(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\Program.cs 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 (Math.Abs(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 (Math.Abs(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 (Math.Abs(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 = Math.Abs(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\Program.cs 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 (Math.Abs(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 Math.Sqrt(35.0 ­ 2.0 * x);
139 }
140
141 static double F1(double x)
142 {
143 return Math.Sin(x) ­ x * x * x * x;
144 }
145
146 static double F1prime(double x)
147 {
148 return Math.Cos(x) ­ 4.0 * x * x * x;
149 }
150 #endregion
151
152 static void Main(string[]  )
153 {
154 try
155 {
156 Console.WriteLine("\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\Program.cs 4
162 x = LinearIncrementalSearch(F, x, deltaX, n);
163 Console.WriteLine("\nSolution " + i.ToString() + " = " + 
x.ToString());
164 Console.WriteLine("Solution confirmation: f(x) = " + F
(x).ToString());
165 }
166 Console.ReadLine();
167
168 Console.WriteLine("\n\nTesting Bisection Method\n");
169 x = BisectionMethod(F, 1.0, 2.0, 0.0001);
170 Console.WriteLine("Solution from the bisection method: " + 
x.ToString());
171 Console.WriteLine("Solution confirmation: f(x) = " + F
(x).ToString());
172 Console.ReadLine();
173
174 Console.WriteLine("\n\nTesting Secant Method\n");
175 x = SecantMethod(F, 1.0, 1.5, 0.0001);
176 Console.WriteLine("Solution from the secant method: " + 
x.ToString());
177 Console.WriteLine("Solution confirmation: f(x) = " + F
(x).ToString());
178 Console.ReadLine();
179
180 Console.WriteLine("\n\nTesting False Position Method\n");
181 x = FalsePositionMethod(F, 1.0, 2.0, 0.0001);
182 Console.WriteLine("Solution from the false position method: " 
+ x.ToString());
183 Console.WriteLine("Solution confirmation: f(x) = " + F
(x).ToString());
184 Console.ReadLine();
185
186 Console.WriteLine("\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 Console.WriteLine("solution from the fixed point method: " + 
x.ToString());
192 Console.WriteLine("Expected solution = 5.00");
193 Console.ReadLine();
194
195 Console.WriteLine("\n\nTesting Testing Newton­Raphson Method
\n");
196 x = NewtonRaphsonMethod(F1, F1prime, 1.0, 0.0001);
197 Console.WriteLine("Solution from the Newton­Raphson method: " 
+ x.ToString());
198 Console.WriteLine("Solution confirmation: f(x) = " + F1
(x).ToString());
199 Console.ReadLine();
200 }
201 catch (Exception ex)
202 {
203 Console.WriteLine("Fatal error: " + ex.Message);
204 Console.ReadLine();
205 }
...hapter09_CodeExamples\Chapter09_CodeExamples\Program.cs 5
206 }
207 }
208 }
209

You might also like