TheAlgorithms-C/dd/d08/newton__raphson__root_8c.html
2022-01-14 20:11:27 +00:00

274 lines
17 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: numerical_methods/newton_raphson_root.c File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('dd/d08/newton__raphson__root_8c.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">newton_raphson_root.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Find approximate solution for \(f(x) = 0\) using Newton-Raphson interpolation algorithm.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;complex.h&gt;</code><br />
<code>#include &lt;limits.h&gt;</code><br />
<code>#include &lt;math.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for newton_raphson_root.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d8/d11/newton__raphson__root_8c__incl.svg" width="526" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:af270a96662132d0385cb6b4637c5a689"><td class="memItemLeft" align="right" valign="top"><a id="af270a96662132d0385cb6b4637c5a689" name="af270a96662132d0385cb6b4637c5a689"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>ACCURACY</b>&#160;&#160;&#160;1e-10</td></tr>
<tr class="memdesc:af270a96662132d0385cb6b4637c5a689"><td class="mdescLeft">&#160;</td><td class="mdescRight">solution accuracy <br /></td></tr>
<tr class="separator:af270a96662132d0385cb6b4637c5a689"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a72f87d423a488946b319627a454d3925"><td class="memItemLeft" align="right" valign="top">double complex&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d08/newton__raphson__root_8c.html#a72f87d423a488946b319627a454d3925">func</a> (double complex x)</td></tr>
<tr class="memdesc:a72f87d423a488946b319627a454d3925"><td class="mdescLeft">&#160;</td><td class="mdescRight">Return value of the function to find the root for. <a href="../../dd/d08/newton__raphson__root_8c.html#a72f87d423a488946b319627a454d3925">More...</a><br /></td></tr>
<tr class="separator:a72f87d423a488946b319627a454d3925"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae713a1fd0c275fbec7edf263ac2c0337"><td class="memItemLeft" align="right" valign="top">double complex&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d08/newton__raphson__root_8c.html#ae713a1fd0c275fbec7edf263ac2c0337">d_func</a> (double complex x)</td></tr>
<tr class="memdesc:ae713a1fd0c275fbec7edf263ac2c0337"><td class="mdescLeft">&#160;</td><td class="mdescRight">Return first order derivative of the function. <a href="../../dd/d08/newton__raphson__root_8c.html#ae713a1fd0c275fbec7edf263ac2c0337">More...</a><br /></td></tr>
<tr class="separator:ae713a1fd0c275fbec7edf263ac2c0337"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d08/newton__raphson__root_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft">&#160;</td><td class="mdescRight">main function <a href="../../dd/d08/newton__raphson__root_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">More...</a><br /></td></tr>
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >Find approximate solution for \(f(x) = 0\) using Newton-Raphson interpolation algorithm. </p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ae713a1fd0c275fbec7edf263ac2c0337" name="ae713a1fd0c275fbec7edf263ac2c0337"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae713a1fd0c275fbec7edf263ac2c0337">&#9670;&nbsp;</a></span>d_func()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double complex d_func </td>
<td>(</td>
<td class="paramtype">double complex&#160;</td>
<td class="paramname"><em>x</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Return first order derivative of the function. </p>
<p >\(f&#39;(x)\) </p>
<div class="fragment"><div class="line"><span class="lineno"> 32</span>{ <span class="keywordflow">return</span> 2. * x; }</div>
</div><!-- fragment -->
</div>
</div>
<a id="a72f87d423a488946b319627a454d3925" name="a72f87d423a488946b319627a454d3925"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a72f87d423a488946b319627a454d3925">&#9670;&nbsp;</a></span>func()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double complex func </td>
<td>(</td>
<td class="paramtype">double complex&#160;</td>
<td class="paramname"><em>x</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Return value of the function to find the root for. </p>
<p >\(f(x)\) </p>
<div class="fragment"><div class="line"><span class="lineno"> 23</span>{</div>
<div class="line"><span class="lineno"> 24</span> <span class="keywordflow">return</span> x * x - 3.; <span class="comment">/* x^2 = 3 - solution is sqrt(3) */</span></div>
<div class="line"><span class="lineno"> 25</span> <span class="comment">// return x * x - 2.; /* x^2 = 2 - solution is sqrt(2) */</span></div>
<div class="line"><span class="lineno"> 26</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="a3c04138a5bfe5d72780bb7e82a18e627"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">&#9670;&nbsp;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>argc</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char **&#160;</td>
<td class="paramname"><em>argv</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>main function </p>
<div class="fragment"><div class="line"><span class="lineno"> 38</span>{</div>
<div class="line"><span class="lineno"> 39</span> <span class="keywordtype">double</span> delta = 1;</div>
<div class="line"><span class="lineno"> 40</span> <span class="keywordtype">double</span> complex cdelta = 1;</div>
<div class="line"><span class="lineno"> 41</span> </div>
<div class="line"><span class="lineno"> 42</span> <span class="comment">/* initialize random seed: */</span></div>
<div class="line"><span class="lineno"> 43</span> srand(time(NULL));</div>
<div class="line"><span class="lineno"> 44</span> </div>
<div class="line"><span class="lineno"> 45</span> <span class="comment">/* random initial approximation */</span></div>
<div class="line"><span class="lineno"> 46</span> <span class="keywordtype">double</span> complex root = (rand() % 100 - 50) + (rand() % 100 - 50) * I;</div>
<div class="line"><span class="lineno"> 47</span> </div>
<div class="line"><span class="lineno"> 48</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> counter = 0;</div>
<div class="line"><span class="lineno"> 49</span> <span class="comment">/* iterate till a convergence is reached */</span></div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordflow">while</span> (delta &gt; <a class="code hl_define" href="../../dd/d08/newton__raphson__root_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a> &amp;&amp; counter &lt; ULONG_MAX)</div>
<div class="line"><span class="lineno"> 51</span> {</div>
<div class="line"><span class="lineno"> 52</span> cdelta = <a class="code hl_function" href="../../dd/d08/newton__raphson__root_8c.html#a72f87d423a488946b319627a454d3925">func</a>(root) / <a class="code hl_function" href="../../dd/d08/newton__raphson__root_8c.html#ae713a1fd0c275fbec7edf263ac2c0337">d_func</a>(root);</div>
<div class="line"><span class="lineno"> 53</span> root += -cdelta;</div>
<div class="line"><span class="lineno"> 54</span> counter++;</div>
<div class="line"><span class="lineno"> 55</span> delta = fabs(cabs(cdelta));</div>
<div class="line"><span class="lineno"> 56</span> </div>
<div class="line"><span class="lineno"> 57</span><span class="preprocessor">#if defined(DEBUG) || !defined(NDEBUG)</span></div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">if</span> (counter % 50 == 0)</div>
<div class="line"><span class="lineno"> 59</span> {</div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordtype">double</span> r = creal(root);</div>
<div class="line"><span class="lineno"> 61</span> <span class="keywordtype">double</span> c = cimag(root);</div>
<div class="line"><span class="lineno"> 62</span> </div>
<div class="line"><span class="lineno"> 63</span> printf(<span class="stringliteral">&quot;Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n&quot;</span>, counter,</div>
<div class="line"><span class="lineno"> 64</span> r, c &gt;= 0 ? <span class="charliteral">&#39;+&#39;</span> : <span class="charliteral">&#39;-&#39;</span>, c &gt;= 0 ? c : -c, delta);</div>
<div class="line"><span class="lineno"> 65</span> }</div>
<div class="line"><span class="lineno"> 66</span><span class="preprocessor">#endif</span></div>
<div class="line"><span class="lineno"> 67</span> }</div>
<div class="line"><span class="lineno"> 68</span> </div>
<div class="line"><span class="lineno"> 69</span> <span class="keywordtype">double</span> r = creal(root);</div>
<div class="line"><span class="lineno"> 70</span> <span class="keywordtype">double</span> c = fabs(cimag(root)) &lt; <a class="code hl_define" href="../../dd/d08/newton__raphson__root_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a> ? 0 : cimag(root);</div>
<div class="line"><span class="lineno"> 71</span> </div>
<div class="line"><span class="lineno"> 72</span> printf(<span class="stringliteral">&quot;Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n&quot;</span>, counter, r,</div>
<div class="line"><span class="lineno"> 73</span> c &gt;= 0 ? <span class="charliteral">&#39;+&#39;</span> : <span class="charliteral">&#39;-&#39;</span>, c &gt;= 0 ? c : -c, delta);</div>
<div class="line"><span class="lineno"> 74</span> </div>
<div class="line"><span class="lineno"> 75</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 76</span>}</div>
<div class="ttc" id="anewton__raphson__root_8c_html_a72f87d423a488946b319627a454d3925"><div class="ttname"><a href="../../dd/d08/newton__raphson__root_8c.html#a72f87d423a488946b319627a454d3925">func</a></div><div class="ttdeci">double complex func(double complex x)</div><div class="ttdoc">Return value of the function to find the root for.</div><div class="ttdef"><b>Definition:</b> newton_raphson_root.c:22</div></div>
<div class="ttc" id="anewton__raphson__root_8c_html_ae713a1fd0c275fbec7edf263ac2c0337"><div class="ttname"><a href="../../dd/d08/newton__raphson__root_8c.html#ae713a1fd0c275fbec7edf263ac2c0337">d_func</a></div><div class="ttdeci">double complex d_func(double complex x)</div><div class="ttdoc">Return first order derivative of the function.</div><div class="ttdef"><b>Definition:</b> newton_raphson_root.c:32</div></div>
<div class="ttc" id="anewton__raphson__root_8c_html_af270a96662132d0385cb6b4637c5a689"><div class="ttname"><a href="../../dd/d08/newton__raphson__root_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a></div><div class="ttdeci">#define ACCURACY</div><div class="ttdoc">solution accuracy</div><div class="ttdef"><b>Definition:</b> newton_raphson_root.c:16</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/d08/newton__raphson__root_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="171" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_9c6faab82c22511b50177aa2e38e2780.html">numerical_methods</a></li><li class="navelem"><a class="el" href="../../dd/d08/newton__raphson__root_8c.html">newton_raphson_root.c</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3 </li>
</ul>
</div>
</body>
</html>