TheAlgorithms-C/d4/d07/ode__forward__euler_8c.html
2022-09-08 00:17:03 +00:00

519 lines
34 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" lang="en-US">
<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.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: numerical_methods/ode_forward_euler.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.5 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../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('d4/d07/ode__forward__euler_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">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</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">ode_forward_euler.c File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Solve a multivariable first order <a href="https://en.wikipedia.org/wiki/Ordinary_differential_equation" target="_blank">ordinary differential equation (ODEs)</a> using <a href="https://en.wikipedia.org/wiki/Numerical_methods_for_ordinary_differential_equations#Euler_method" target="_blank">forward Euler method</a>
<a href="#details">More...</a></p>
<div class="textblock"><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 ode_forward_euler.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6c/ode__forward__euler_8c__incl.svg" width="331" height="124"><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:a9ceb646336224ee890a269d0b4600d09"><td class="memItemLeft" align="right" valign="top"><a id="a9ceb646336224ee890a269d0b4600d09" name="a9ceb646336224ee890a269d0b4600d09"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>order</b>&#160;&#160;&#160;2</td></tr>
<tr class="memdesc:a9ceb646336224ee890a269d0b4600d09"><td class="mdescLeft">&#160;</td><td class="mdescRight">number of dependent variables in <a class="el" href="../../d4/d07/ode__forward__euler_8c.html#a97075291390a68c262ed66e157a57eb4" title="Problem statement for a system with first-order differential equations.">problem</a> <br /></td></tr>
<tr class="separator:a9ceb646336224ee890a269d0b4600d09"><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:a97075291390a68c262ed66e157a57eb4"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d07/ode__forward__euler_8c.html#a97075291390a68c262ed66e157a57eb4">problem</a> (const double *x, double *y, double *dy)</td></tr>
<tr class="memdesc:a97075291390a68c262ed66e157a57eb4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Problem statement for a system with first-order differential equations. <a href="../../d4/d07/ode__forward__euler_8c.html#a97075291390a68c262ed66e157a57eb4">More...</a><br /></td></tr>
<tr class="separator:a97075291390a68c262ed66e157a57eb4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a8caee977b26888d34040b122e0e28e3a"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d07/ode__forward__euler_8c.html#a8caee977b26888d34040b122e0e28e3a">exact_solution</a> (const double *x, double *y)</td></tr>
<tr class="memdesc:a8caee977b26888d34040b122e0e28e3a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Exact solution of the problem. <a href="../../d4/d07/ode__forward__euler_8c.html#a8caee977b26888d34040b122e0e28e3a">More...</a><br /></td></tr>
<tr class="separator:a8caee977b26888d34040b122e0e28e3a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae6c9413953c8d9d4bc9e374b29586350"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d07/ode__forward__euler_8c.html#ae6c9413953c8d9d4bc9e374b29586350">forward_euler_step</a> (const double dx, const double *x, double *y, double *dy)</td></tr>
<tr class="memdesc:ae6c9413953c8d9d4bc9e374b29586350"><td class="mdescLeft">&#160;</td><td class="mdescRight">Compute next step approximation using the forward-Euler method. <a href="../../d4/d07/ode__forward__euler_8c.html#ae6c9413953c8d9d4bc9e374b29586350">More...</a><br /></td></tr>
<tr class="separator:ae6c9413953c8d9d4bc9e374b29586350"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aaf88ad8f9f7c39fc38f3f03d6fea9df9"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d07/ode__forward__euler_8c.html#aaf88ad8f9f7c39fc38f3f03d6fea9df9">forward_euler</a> (double dx, double x0, double x_max, double *y, char save_to_file)</td></tr>
<tr class="memdesc:aaf88ad8f9f7c39fc38f3f03d6fea9df9"><td class="mdescLeft">&#160;</td><td class="mdescRight">Compute approximation using the forward-Euler method in the given limits. <a href="../../d4/d07/ode__forward__euler_8c.html#aaf88ad8f9f7c39fc38f3f03d6fea9df9">More...</a><br /></td></tr>
<tr class="separator:aaf88ad8f9f7c39fc38f3f03d6fea9df9"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a0ddf1224851353fc92bfbff6f499fa97"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d07/ode__forward__euler_8c.html#a0ddf1224851353fc92bfbff6f499fa97">main</a> (int argc, char *argv[])</td></tr>
<tr class="memdesc:a0ddf1224851353fc92bfbff6f499fa97"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main Function. <a href="../../d4/d07/ode__forward__euler_8c.html#a0ddf1224851353fc92bfbff6f499fa97">More...</a><br /></td></tr>
<tr class="separator:a0ddf1224851353fc92bfbff6f499fa97"><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 >Solve a multivariable first order <a href="https://en.wikipedia.org/wiki/Ordinary_differential_equation" target="_blank">ordinary differential equation (ODEs)</a> using <a href="https://en.wikipedia.org/wiki/Numerical_methods_for_ordinary_differential_equations#Euler_method" target="_blank">forward Euler method</a> </p>
<dl class="section author"><dt>Authors</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
<p>The ODE being solved is: </p><p class="formulaDsp">
\begin{eqnarray*}
\dot{u} &amp;=&amp; v\\
\dot{v} &amp;=&amp; -\omega^2 u\\
\omega &amp;=&amp; 1\\
[x_0, u_0, v_0] &amp;=&amp; [0,1,0]\qquad\ldots\text{(initial values)}
\end{eqnarray*}
</p>
<p> The exact solution for the above problem is: </p><p class="formulaDsp">
\begin{eqnarray*}
u(x) &amp;=&amp; \cos(x)\\
v(x) &amp;=&amp; -\sin(x)\\
\end{eqnarray*}
</p>
<p> The computation results are stored to a text file <code>forward_euler.csv</code> and the exact soltuion results in <code>exact.csv</code> for comparison. <img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/numerical_methods/ode_forward_euler.svg" alt="Implementation solution" style="pointer-events: none;" class="inline"/></p>
<p >To implement <a href="https://en.wikipedia.org/wiki/Van_der_Pol_oscillator" target="_blank">Van der Pol oscillator</a>, change the <a class="el" href="../../d4/d07/ode__forward__euler_8c.html#a97075291390a68c262ed66e157a57eb4" title="Problem statement for a system with first-order differential equations.">problem</a> function to: </p><div class="fragment"><div class="line"><span class="keyword">const</span> <span class="keywordtype">double</span> mu = 2.0;</div>
<div class="line">dy[0] = y[1];</div>
<div class="line">dy[1] = mu * (1.f - y[0] * y[0]) * y[1] - y[0];</div>
</div><!-- fragment --> <dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d1/dc2/ode__midpoint__euler_8c.html" title="Solve a multivariable first order ordinary differential equation (ODEs) using midpoint Euler method">ode_midpoint_euler.c</a>, <a class="el" href="../../d4/d99/ode__semi__implicit__euler_8c.html" title="Solve a multivariable first order ordinary differential equation (ODEs) using semi implicit Euler met...">ode_semi_implicit_euler.c</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a8caee977b26888d34040b122e0e28e3a" name="a8caee977b26888d34040b122e0e28e3a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a8caee977b26888d34040b122e0e28e3a">&#9670;&#160;</a></span>exact_solution()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void exact_solution </td>
<td>(</td>
<td class="paramtype">const double *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>y</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Exact solution of the problem. </p>
<p >Used for solution comparison.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>independent variable </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">y</td><td>dependent variable </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 69</span>{</div>
<div class="line"><span class="lineno"> 70</span> y[0] = cos(x[0]);</div>
<div class="line"><span class="lineno"> 71</span> y[1] = -sin(x[0]);</div>
<div class="line"><span class="lineno"> 72</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="aaf88ad8f9f7c39fc38f3f03d6fea9df9" name="aaf88ad8f9f7c39fc38f3f03d6fea9df9"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aaf88ad8f9f7c39fc38f3f03d6fea9df9">&#9670;&#160;</a></span>forward_euler()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double forward_euler </td>
<td>(</td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>dx</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>x0</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>x_max</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">char&#160;</td>
<td class="paramname"><em>save_to_file</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Compute approximation using the forward-Euler method in the given limits. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">dx</td><td>step size </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">x0</td><td>initial value of independent variable </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">x_max</td><td>final value of independent variable </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">y</td><td>take \(y_n\) and compute \(y_{n+1}\) </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">save_to_file</td><td>flag to save results to a CSV file (1) or not (0) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>time taken for computation in seconds </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 101</span>{</div>
<div class="line"><span class="lineno"> 102</span> <span class="keywordtype">double</span> dy[<a class="code hl_define" href="../../d4/d07/ode__forward__euler_8c.html#a9ceb646336224ee890a269d0b4600d09">order</a>];</div>
<div class="line"><span class="lineno"> 103</span> </div>
<div class="line"><span class="lineno"> 104</span> FILE *fp = NULL;</div>
<div class="line"><span class="lineno"> 105</span> <span class="keywordflow">if</span> (save_to_file)</div>
<div class="line"><span class="lineno"> 106</span> {</div>
<div class="line"><span class="lineno"> 107</span> fp = fopen(<span class="stringliteral">&quot;forward_euler.csv&quot;</span>, <span class="stringliteral">&quot;w+&quot;</span>);</div>
<div class="line"><span class="lineno"> 108</span> <span class="keywordflow">if</span> (fp == NULL)</div>
<div class="line"><span class="lineno"> 109</span> {</div>
<div class="line"><span class="lineno"> 110</span> perror(<span class="stringliteral">&quot;Error! &quot;</span>);</div>
<div class="line"><span class="lineno"> 111</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><span class="lineno"> 112</span> }</div>
<div class="line"><span class="lineno"> 113</span> }</div>
<div class="line"><span class="lineno"> 114</span> </div>
<div class="line"><span class="lineno"> 115</span> <span class="comment">/* start integration */</span></div>
<div class="line"><span class="lineno"> 116</span> clock_t t1 = clock();</div>
<div class="line"><span class="lineno"> 117</span> <span class="keywordtype">double</span> x = x0;</div>
<div class="line"><span class="lineno"> 118</span> <span class="keywordflow">do</span> <span class="comment">// iterate for each step of independent variable</span></div>
<div class="line"><span class="lineno"> 119</span> {</div>
<div class="line"><span class="lineno"> 120</span> <span class="keywordflow">if</span> (save_to_file &amp;&amp; fp)</div>
<div class="line"><span class="lineno"> 121</span> fprintf(fp, <span class="stringliteral">&quot;%.4g,%.4g,%.4g\n&quot;</span>, x, y[0], y[1]); <span class="comment">// write to file</span></div>
<div class="line"><span class="lineno"> 122</span> <a class="code hl_function" href="../../d4/d07/ode__forward__euler_8c.html#ae6c9413953c8d9d4bc9e374b29586350">forward_euler_step</a>(dx, &amp;x, y, dy); <span class="comment">// perform integration</span></div>
<div class="line"><span class="lineno"> 123</span> x += dx; <span class="comment">// update step</span></div>
<div class="line"><span class="lineno"> 124</span> } <span class="keywordflow">while</span> (x &lt;= x_max); <span class="comment">// till upper limit of independent variable</span></div>
<div class="line"><span class="lineno"> 125</span> <span class="comment">/* end of integration */</span></div>
<div class="line"><span class="lineno"> 126</span> clock_t t2 = clock();</div>
<div class="line"><span class="lineno"> 127</span> </div>
<div class="line"><span class="lineno"> 128</span> <span class="keywordflow">if</span> (save_to_file &amp;&amp; fp)</div>
<div class="line"><span class="lineno"> 129</span> fclose(fp);</div>
<div class="line"><span class="lineno"> 130</span> </div>
<div class="line"><span class="lineno"> 131</span> <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)(t2 - t1) / CLOCKS_PER_SEC;</div>
<div class="line"><span class="lineno"> 132</span>}</div>
<div class="ttc" id="aode__forward__euler_8c_html_a9ceb646336224ee890a269d0b4600d09"><div class="ttname"><a href="../../d4/d07/ode__forward__euler_8c.html#a9ceb646336224ee890a269d0b4600d09">order</a></div><div class="ttdeci">#define order</div><div class="ttdoc">number of dependent variables in problem</div><div class="ttdef"><b>Definition:</b> ode_forward_euler.c:44</div></div>
<div class="ttc" id="aode__forward__euler_8c_html_ae6c9413953c8d9d4bc9e374b29586350"><div class="ttname"><a href="../../d4/d07/ode__forward__euler_8c.html#ae6c9413953c8d9d4bc9e374b29586350">forward_euler_step</a></div><div class="ttdeci">void forward_euler_step(const double dx, const double *x, double *y, double *dy)</div><div class="ttdoc">Compute next step approximation using the forward-Euler method.</div><div class="ttdef"><b>Definition:</b> ode_forward_euler.c:82</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="../../d4/d07/ode__forward__euler_8c_aaf88ad8f9f7c39fc38f3f03d6fea9df9_cgraph.svg" width="407" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ae6c9413953c8d9d4bc9e374b29586350" name="ae6c9413953c8d9d4bc9e374b29586350"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae6c9413953c8d9d4bc9e374b29586350">&#9670;&#160;</a></span>forward_euler_step()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void forward_euler_step </td>
<td>(</td>
<td class="paramtype">const double&#160;</td>
<td class="paramname"><em>dx</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const double *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>dy</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Compute next step approximation using the forward-Euler method. </p>
<p class="formulaDsp">
\[y_{n+1}=y_n + dx\cdot f\left(x_n,y_n\right)\]
</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">dx</td><td>step size </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">x</td><td>take \(x_n\) and compute \(x_{n+1}\) </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">y</td><td>take \(y_n\) and compute \(y_{n+1}\) </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">dy</td><td>compute \(f\left(x_n,y_n\right)\) </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 83</span>{</div>
<div class="line"><span class="lineno"> 84</span> <span class="keywordtype">int</span> o;</div>
<div class="line"><span class="lineno"> 85</span> <a class="code hl_function" href="../../d4/d07/ode__forward__euler_8c.html#a97075291390a68c262ed66e157a57eb4">problem</a>(x, y, dy);</div>
<div class="line"><span class="lineno"> 86</span> <span class="keywordflow">for</span> (o = 0; o &lt; <a class="code hl_define" href="../../d4/d07/ode__forward__euler_8c.html#a9ceb646336224ee890a269d0b4600d09">order</a>; o++) y[o] += dx * dy[o];</div>
<div class="line"><span class="lineno"> 87</span>}</div>
<div class="ttc" id="aode__forward__euler_8c_html_a97075291390a68c262ed66e157a57eb4"><div class="ttname"><a href="../../d4/d07/ode__forward__euler_8c.html#a97075291390a68c262ed66e157a57eb4">problem</a></div><div class="ttdeci">void problem(const double *x, double *y, double *dy)</div><div class="ttdoc">Problem statement for a system with first-order differential equations.</div><div class="ttdef"><b>Definition:</b> ode_forward_euler.c:55</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="../../d4/d07/ode__forward__euler_8c_ae6c9413953c8d9d4bc9e374b29586350_cgraph.svg" width="259" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a0ddf1224851353fc92bfbff6f499fa97" name="a0ddf1224851353fc92bfbff6f499fa97"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0ddf1224851353fc92bfbff6f499fa97">&#9670;&#160;</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"> 138</span>{</div>
<div class="line"><span class="lineno"> 139</span> <span class="keywordtype">double</span> X0 = 0.f; <span class="comment">/* initial value of x0 */</span></div>
<div class="line"><span class="lineno"> 140</span> <span class="keywordtype">double</span> X_MAX = 10.F; <span class="comment">/* upper limit of integration */</span></div>
<div class="line"><span class="lineno"> 141</span> <span class="keywordtype">double</span> Y0[] = {1.f, 0.f}; <span class="comment">/* initial value Y = y(x = x_0) */</span></div>
<div class="line"><span class="lineno"> 142</span> <span class="keywordtype">double</span> step_size;</div>
<div class="line"><span class="lineno"> 143</span> </div>
<div class="line"><span class="lineno"> 144</span> <span class="keywordflow">if</span> (argc == 1)</div>
<div class="line"><span class="lineno"> 145</span> {</div>
<div class="line"><span class="lineno"> 146</span> printf(<span class="stringliteral">&quot;\nEnter the step size: &quot;</span>);</div>
<div class="line"><span class="lineno"> 147</span> scanf(<span class="stringliteral">&quot;%lg&quot;</span>, &amp;step_size);</div>
<div class="line"><span class="lineno"> 148</span> }</div>
<div class="line"><span class="lineno"> 149</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 150</span> <span class="comment">// use commandline argument as independent variable step size</span></div>
<div class="line"><span class="lineno"> 151</span> step_size = atof(argv[1]);</div>
<div class="line"><span class="lineno"> 152</span> </div>
<div class="line"><span class="lineno"> 153</span> <span class="comment">// get approximate solution</span></div>
<div class="line"><span class="lineno"> 154</span> <span class="keywordtype">double</span> total_time = <a class="code hl_function" href="../../d4/d07/ode__forward__euler_8c.html#aaf88ad8f9f7c39fc38f3f03d6fea9df9">forward_euler</a>(step_size, X0, X_MAX, Y0, 1);</div>
<div class="line"><span class="lineno"> 155</span> printf(<span class="stringliteral">&quot;\tTime = %.6g ms\n&quot;</span>, total_time);</div>
<div class="line"><span class="lineno"> 156</span> </div>
<div class="line"><span class="lineno"> 157</span> <span class="comment">/* compute exact solution for comparion */</span></div>
<div class="line"><span class="lineno"> 158</span> FILE *fp = fopen(<span class="stringliteral">&quot;exact.csv&quot;</span>, <span class="stringliteral">&quot;w+&quot;</span>);</div>
<div class="line"><span class="lineno"> 159</span> <span class="keywordflow">if</span> (fp == NULL)</div>
<div class="line"><span class="lineno"> 160</span> {</div>
<div class="line"><span class="lineno"> 161</span> perror(<span class="stringliteral">&quot;Error! &quot;</span>);</div>
<div class="line"><span class="lineno"> 162</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><span class="lineno"> 163</span> }</div>
<div class="line"><span class="lineno"> 164</span> <span class="keywordtype">double</span> x = X0;</div>
<div class="line"><span class="lineno"> 165</span> <span class="keywordtype">double</span> *y = &amp;(Y0[0]);</div>
<div class="line"><span class="lineno"> 166</span> printf(<span class="stringliteral">&quot;Finding exact solution\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 167</span> clock_t t1 = clock();</div>
<div class="line"><span class="lineno"> 168</span> </div>
<div class="line"><span class="lineno"> 169</span> <span class="keywordflow">do</span></div>
<div class="line"><span class="lineno"> 170</span> {</div>
<div class="line"><span class="lineno"> 171</span> fprintf(fp, <span class="stringliteral">&quot;%.4g,%.4g,%.4g\n&quot;</span>, x, y[0], y[1]); <span class="comment">// write to file</span></div>
<div class="line"><span class="lineno"> 172</span> <a class="code hl_function" href="../../d4/d07/ode__forward__euler_8c.html#a8caee977b26888d34040b122e0e28e3a">exact_solution</a>(&amp;x, y);</div>
<div class="line"><span class="lineno"> 173</span> x += step_size;</div>
<div class="line"><span class="lineno"> 174</span> } <span class="keywordflow">while</span> (x &lt;= X_MAX);</div>
<div class="line"><span class="lineno"> 175</span> </div>
<div class="line"><span class="lineno"> 176</span> clock_t t2 = clock();</div>
<div class="line"><span class="lineno"> 177</span> total_time = (t2 - t1) / CLOCKS_PER_SEC;</div>
<div class="line"><span class="lineno"> 178</span> printf(<span class="stringliteral">&quot;\tTime = %.6g ms\n&quot;</span>, total_time);</div>
<div class="line"><span class="lineno"> 179</span> fclose(fp);</div>
<div class="line"><span class="lineno"> 180</span> </div>
<div class="line"><span class="lineno"> 181</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 182</span>}</div>
<div class="ttc" id="aode__forward__euler_8c_html_a8caee977b26888d34040b122e0e28e3a"><div class="ttname"><a href="../../d4/d07/ode__forward__euler_8c.html#a8caee977b26888d34040b122e0e28e3a">exact_solution</a></div><div class="ttdeci">void exact_solution(const double *x, double *y)</div><div class="ttdoc">Exact solution of the problem.</div><div class="ttdef"><b>Definition:</b> ode_forward_euler.c:68</div></div>
<div class="ttc" id="aode__forward__euler_8c_html_aaf88ad8f9f7c39fc38f3f03d6fea9df9"><div class="ttname"><a href="../../d4/d07/ode__forward__euler_8c.html#aaf88ad8f9f7c39fc38f3f03d6fea9df9">forward_euler</a></div><div class="ttdeci">double forward_euler(double dx, double x0, double x_max, double *y, char save_to_file)</div><div class="ttdoc">Compute approximation using the forward-Euler method in the given limits.</div><div class="ttdef"><b>Definition:</b> ode_forward_euler.c:99</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="../../d4/d07/ode__forward__euler_8c_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg" width="512" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a97075291390a68c262ed66e157a57eb4" name="a97075291390a68c262ed66e157a57eb4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a97075291390a68c262ed66e157a57eb4">&#9670;&#160;</a></span>problem()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void problem </td>
<td>(</td>
<td class="paramtype">const double *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>dy</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Problem statement for a system with first-order differential equations. </p>
<p >Updates the system differential variables. </p><dl class="section note"><dt>Note</dt><dd>This function can be updated to and ode of any order.</dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>independent variable(s) </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">y</td><td>dependent variable(s) </td></tr>
<tr><td class="paramdir">[in,out]</td><td class="paramname">dy</td><td>first-derivative of dependent variable(s) </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 56</span>{</div>
<div class="line"><span class="lineno"> 57</span> <span class="keyword">const</span> <span class="keywordtype">double</span> omega = 1.F; <span class="comment">// some const for the problem</span></div>
<div class="line"><span class="lineno"> 58</span> dy[0] = y[1]; <span class="comment">// x dot</span></div>
<div class="line"><span class="lineno"> 59</span> dy[1] = -omega * omega * y[0]; <span class="comment">// y dot</span></div>
<div class="line"><span class="lineno"> 60</span>}</div>
</div><!-- fragment -->
</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="../../d4/d07/ode__forward__euler_8c.html">ode_forward_euler.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.5 </li>
</ul>
</div>
</body>
</html>