TheAlgorithms-C/d4/d99/ode__semi__implicit__euler_8c.html

509 lines
38 KiB
HTML
Raw Normal View History

<!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.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: numerical_methods/ode_semi_implicit_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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<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.2 -->
<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('d4/d99/ode__semi__implicit__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">
<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">ode_semi_implicit_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/Semi-implicit_Euler_method" target="_blank">semi implicit 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_semi_implicit_euler.c:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/d85/ode__semi__implicit__euler_8c__incl.svg" width="331" 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: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/d99/ode__semi__implicit__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/d99/ode__semi__implicit__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/d99/ode__semi__implicit__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/d99/ode__semi__implicit__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/d99/ode__semi__implicit__euler_8c.html#a8caee977b26888d34040b122e0e28e3a">More...</a><br /></td></tr>
<tr class="separator:a8caee977b26888d34040b122e0e28e3a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a720b7e995d2bbc615f94a2c7dbcf84eb"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a720b7e995d2bbc615f94a2c7dbcf84eb">semi_implicit_euler_step</a> (double dx, double *x, double *y, double *dy)</td></tr>
<tr class="memdesc:a720b7e995d2bbc615f94a2c7dbcf84eb"><td class="mdescLeft">&#160;</td><td class="mdescRight">Compute next step approximation using the semi-implicit-Euler method. <a href="../../d4/d99/ode__semi__implicit__euler_8c.html#a720b7e995d2bbc615f94a2c7dbcf84eb">More...</a><br /></td></tr>
<tr class="separator:a720b7e995d2bbc615f94a2c7dbcf84eb"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ad80059877222f885b549f2d0a3dc6b55"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/d99/ode__semi__implicit__euler_8c.html#ad80059877222f885b549f2d0a3dc6b55">semi_implicit_euler</a> (double dx, double x0, double x_max, double *y, char save_to_file)</td></tr>
<tr class="memdesc:ad80059877222f885b549f2d0a3dc6b55"><td class="mdescLeft">&#160;</td><td class="mdescRight">Compute approximation using the semi-implicit-Euler method in the given limits. <a href="../../d4/d99/ode__semi__implicit__euler_8c.html#ad80059877222f885b549f2d0a3dc6b55">More...</a><br /></td></tr>
<tr class="separator:ad80059877222f885b549f2d0a3dc6b55"><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/d99/ode__semi__implicit__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/d99/ode__semi__implicit__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/Semi-implicit_Euler_method" target="_blank">semi implicit 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>semi_implicit_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_semi_implicit_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/d99/ode__semi__implicit__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 --><p> <a href="https://en.wikipedia.org/wiki/Van_der_Pol_oscillator"><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/numerical_methods/van_der_pol_implicit_euler.svg" alt="Van der Pol Oscillator solution" style="pointer-events: none;" class="inline"/></a></p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d4/d07/ode__forward__euler_8c.html" title="Solve a multivariable first order ordinary differential equation (ODEs) using forward Euler method">ode_forward_euler.c</a>, <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> </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;&nbsp;</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"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span>{</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> y[0] = cos(x[0]);</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> y[1] = -sin(x[0]);</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="a0ddf1224851353fc92bfbff6f499fa97" name="a0ddf1224851353fc92bfbff6f499fa97"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a0ddf1224851353fc92bfbff6f499fa97">&#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"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span>{</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> <span class="keywordtype">double</span> X0 = 0.f; <span class="comment">/* initial value of x0 */</span></div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> <span class="keywordtype">double</span> X_MAX = 10.F; <span class="comment">/* upper limit of integration */</span></div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</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"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> <span class="keywordtype">double</span> step_size;</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> </div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> <span class="keywordflow">if</span> (argc == 1)</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> {</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> printf(<span class="stringliteral">&quot;\nEnter the step size: &quot;</span>);</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> scanf(<span class="stringliteral">&quot;%lg&quot;</span>, &amp;step_size);</div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> }</div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> <span class="keywordflow">else</span></div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> <span class="comment">// use commandline argument as independent variable step size</span></div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> step_size = atof(argv[1]);</div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> </div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> <span class="comment">// get approximate solution</span></div>
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span> <span class="keywordtype">double</span> total_time = <a class="code hl_function" href="../../d4/d99/ode__semi__implicit__euler_8c.html#ad80059877222f885b549f2d0a3dc6b55">semi_implicit_euler</a>(step_size, X0, X_MAX, Y0, 1);</div>
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span> printf(<span class="stringliteral">&quot;\tTime = %.6g ms\n&quot;</span>, total_time);</div>
<div class="line"><a id="l00166" name="l00166"></a><span class="lineno"> 166</span> </div>
<div class="line"><a id="l00167" name="l00167"></a><span class="lineno"> 167</span> <span class="comment">/* compute exact solution for comparion */</span></div>
<div class="line"><a id="l00168" name="l00168"></a><span class="lineno"> 168</span> FILE *fp = fopen(<span class="stringliteral">&quot;exact.csv&quot;</span>, <span class="stringliteral">&quot;w+&quot;</span>);</div>
<div class="line"><a id="l00169" name="l00169"></a><span class="lineno"> 169</span> <span class="keywordflow">if</span> (fp == NULL)</div>
<div class="line"><a id="l00170" name="l00170"></a><span class="lineno"> 170</span> {</div>
<div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> perror(<span class="stringliteral">&quot;Error! &quot;</span>);</div>
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span> }</div>
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span> <span class="keywordtype">double</span> x = X0;</div>
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> <span class="keywordtype">double</span> *y = &amp;(Y0[0]);</div>
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span> printf(<span class="stringliteral">&quot;Finding exact solution\n&quot;</span>);</div>
<div class="line"><a id="l00177" name="l00177"></a><span class="lineno"> 177</span> clock_t t1 = clock();</div>
<div class="line"><a id="l00178" name="l00178"></a><span class="lineno"> 178</span> </div>
<div class="line"><a id="l00179" name="l00179"></a><span class="lineno"> 179</span> <span class="keywordflow">do</span></div>
<div class="line"><a id="l00180" name="l00180"></a><span class="lineno"> 180</span> {</div>
<div class="line"><a id="l00181" name="l00181"></a><span class="lineno"> 181</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"><a id="l00182" name="l00182"></a><span class="lineno"> 182</span> <a class="code hl_function" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a8caee977b26888d34040b122e0e28e3a">exact_solution</a>(&amp;x, y);</div>
<div class="line"><a id="l00183" name="l00183"></a><span class="lineno"> 183</span> x += step_size;</div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> } <span class="keywordflow">while</span> (x &lt;= X_MAX);</div>
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> </div>
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span> clock_t t2 = clock();</div>
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span> total_time = (t2 - t1) / CLOCKS_PER_SEC;</div>
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span> printf(<span class="stringliteral">&quot;\tTime = %.6g ms\n&quot;</span>, total_time);</div>
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span> fclose(fp);</div>
<div class="line"><a id="l00190" name="l00190"></a><span class="lineno"> 190</span> </div>
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span>}</div>
<div class="ttc" id="aode__semi__implicit__euler_8c_html_a8caee977b26888d34040b122e0e28e3a"><div class="ttname"><a href="../../d4/d99/ode__semi__implicit__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_semi_implicit_euler.c:71</div></div>
<div class="ttc" id="aode__semi__implicit__euler_8c_html_ad80059877222f885b549f2d0a3dc6b55"><div class="ttname"><a href="../../d4/d99/ode__semi__implicit__euler_8c.html#ad80059877222f885b549f2d0a3dc6b55">semi_implicit_euler</a></div><div class="ttdeci">double semi_implicit_euler(double dx, double x0, double x_max, double *y, char save_to_file)</div><div class="ttdoc">Compute approximation using the semi-implicit-Euler method in the given limits.</div><div class="ttdef"><b>Definition:</b> ode_semi_implicit_euler.c:109</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/d99/ode__semi__implicit__euler_8c_a0ddf1224851353fc92bfbff6f499fa97_cgraph.svg" width="580" 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>
<a id="a97075291390a68c262ed66e157a57eb4" name="a97075291390a68c262ed66e157a57eb4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a97075291390a68c262ed66e157a57eb4">&#9670;&nbsp;</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"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span>{</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</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"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> dy[0] = y[1]; <span class="comment">// x dot</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> dy[1] = -omega * omega * y[0]; <span class="comment">// y dot</span></div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ad80059877222f885b549f2d0a3dc6b55" name="ad80059877222f885b549f2d0a3dc6b55"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ad80059877222f885b549f2d0a3dc6b55">&#9670;&nbsp;</a></span>semi_implicit_euler()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double semi_implicit_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 semi-implicit-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"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span>{</div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> <span class="keywordtype">double</span> dy[<a class="code hl_define" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a9ceb646336224ee890a269d0b4600d09">order</a>];</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> </div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> FILE *fp = NULL;</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> <span class="keywordflow">if</span> (save_to_file)</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> {</div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> fp = fopen(<span class="stringliteral">&quot;semi_implicit_euler.csv&quot;</span>, <span class="stringliteral">&quot;w+&quot;</span>);</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> <span class="keywordflow">if</span> (fp == NULL)</div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> {</div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> perror(<span class="stringliteral">&quot;Error! &quot;</span>);</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> }</div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> }</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> </div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> <span class="comment">/* start integration */</span></div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> clock_t t1 = clock();</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> <span class="keywordtype">double</span> x = x0;</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> <span class="keywordflow">do</span> <span class="comment">// iterate for each step of independent variable</span></div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> {</div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> <span class="keywordflow">if</span> (save_to_file &amp;&amp; fp)</div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</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"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> <a class="code hl_function" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a720b7e995d2bbc615f94a2c7dbcf84eb">semi_implicit_euler_step</a>(dx, &amp;x, y, dy); <span class="comment">// perform integration</span></div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> x += dx; <span class="comment">// update step</span></div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> } <span class="keywordflow">while</span> (x &lt;= x_max); <span class="comment">// till upper limit of independent variable</span></div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> <span class="comment">/* end of integration */</span></div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> clock_t t2 = clock();</div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> </div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span> <span class="keywordflow">if</span> (save_to_file &amp;&amp; fp)</div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span> fclose(fp);</div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span> </div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span> <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)(t2 - t1) / CLOCKS_PER_SEC;</div>
<div class="line"><a id="l00142" name="l00142"></a><span class="lineno"> 142</span>}</div>
<div class="ttc" id="aode__semi__implicit__euler_8c_html_a720b7e995d2bbc615f94a2c7dbcf84eb"><div class="ttname"><a href="../../d4/d99/ode__semi__implicit__euler_8c.html#a720b7e995d2bbc615f94a2c7dbcf84eb">semi_implicit_euler_step</a></div><div class="ttdeci">void semi_implicit_euler_step(double dx, double *x, double *y, double *dy)</div><div class="ttdoc">Compute next step approximation using the semi-implicit-Euler method.</div><div class="ttdef"><b>Definition:</b> ode_semi_implicit_euler.c:85</div></div>
<div class="ttc" id="aode__semi__implicit__euler_8c_html_a9ceb646336224ee890a269d0b4600d09"><div class="ttname"><a href="../../d4/d99/ode__semi__implicit__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_semi_implicit_euler.c:47</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/d99/ode__semi__implicit__euler_8c_ad80059877222f885b549f2d0a3dc6b55_cgraph.svg" width="482" height="38"><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="a720b7e995d2bbc615f94a2c7dbcf84eb" name="a720b7e995d2bbc615f94a2c7dbcf84eb"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a720b7e995d2bbc615f94a2c7dbcf84eb">&#9670;&nbsp;</a></span>semi_implicit_euler_step()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void semi_implicit_euler_step </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>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 semi-implicit-Euler method. </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 \(y_n+\frac{1}{2}dx\,f\left(x_n,y_n\right)\) </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span>{</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> <span class="keywordtype">int</span> o;</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> </div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> <a class="code hl_function" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a97075291390a68c262ed66e157a57eb4">problem</a>(x, y, dy); <span class="comment">// update dy once</span></div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> y[0] += dx * dy[0]; <span class="comment">// update y0</span></div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span> </div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> <a class="code hl_function" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a97075291390a68c262ed66e157a57eb4">problem</a>(x, y, dy); <span class="comment">// update dy once more</span></div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span> </div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span> <span class="keywordflow">for</span> (o = 1; o &lt; <a class="code hl_define" href="../../d4/d99/ode__semi__implicit__euler_8c.html#a9ceb646336224ee890a269d0b4600d09">order</a>; o++)</div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> y[o] += dx * dy[o]; <span class="comment">// update remaining using new dy</span></div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span> *x += dx;</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span>}</div>
<div class="ttc" id="aode__semi__implicit__euler_8c_html_a97075291390a68c262ed66e157a57eb4"><div class="ttname"><a href="../../d4/d99/ode__semi__implicit__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_semi_implicit_euler.c:58</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/d99/ode__semi__implicit__euler_8c_a720b7e995d2bbc615f94a2c7dbcf84eb_cgraph.svg" width="298" height="38"><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="../../d4/d99/ode__semi__implicit__euler_8c.html">ode_semi_implicit_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.2 </li>
</ul>
</div>
</body>
</html>