|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title>Learn You a Physics for Great Good!</title> |
| 6 | + <link rel="stylesheet" type="text/css" href="../style.css"> |
| 7 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script> |
| 8 | + </head> |
| 9 | + |
| 10 | + <body> |
| 11 | + <header> |
| 12 | + <a href="../index.html"><h1>Learn You a <span class="physics">Physics</span> for Great Good!</h1></a> |
| 13 | + <h1>>>> WORK IN PROGRESS <<<</h1> |
| 14 | + <h2>Calculus / Visualization</h2> |
| 15 | + <nav> |
| 16 | + <span>[src: <a href="https://github.com/DSLsofMath/BScProj2018/blob/master/Physics/src/Calculus/VisVerApp.lhs">Calculus/VisVerApp.lhs</a>]</span> |
| 17 | + <span>Previous: <a href="../Calculus/Integral calculus.html">Integral calculus</a></span> |
| 18 | + <a href="../index.html">Table of contents</a> |
| 19 | + <span>Next: <a href="../Vectors/Vector.html">Vector</a></span> |
| 20 | + </nav> |
| 21 | + </header> |
| 22 | + |
| 23 | + <main> |
| 24 | + <div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">module</span> <span class="dt">Calculus.VisVerApp</span> <span class="kw">where</span> |
| 25 | + |
| 26 | +<span class="kw">import </span><span class="dt">Calculus</span> |
| 27 | + |
| 28 | +<span class="kw">import </span><span class="dt">Hatlab.Plot</span></code></pre></div> |
| 29 | +<h2 id="plotting-with-hatlab">Plotting with Hatlab</h2> |
| 30 | +<p>The brain likes seeing things. Let's give it a good looking reward!</p> |
| 31 | +<p>We'll now make combined use of all of our nice functions. <code>simplify</code>, <code>derive</code>, <code>integrate</code>, <code>eval</code>, and <code>show</code>, all together: the most ambitious crossover event in history!</p> |
| 32 | +<p>First, we create some function expressions ready to be <code>show</code>n and <code>eval</code>uated.</p> |
| 33 | +<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell">f <span class="fu">=</span> <span class="dt">Const</span> <span class="dv">3</span> <span class="fu">:*</span> <span class="dt">Id</span><span class="fu">:^</span><span class="dt">Const</span> <span class="dv">2</span> |
| 34 | +f' <span class="fu">=</span> simplify (derive f) |
| 35 | +_F <span class="fu">=</span> simplify (integrate f <span class="dv">0</span>)</code></pre></div> |
| 36 | +<p>Then, we define a helper function to plot a list of function expressions with Hatlab.</p> |
| 37 | +<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">plotFunExprs ::</span> [<span class="dt">FunExpr</span>] <span class="ot">-></span> <span class="dt">IO</span> () |
| 38 | +plotFunExprs <span class="fu">=</span> plot <span class="fu">.</span> fmap (\f <span class="ot">-></span> <span class="dt">Fun</span> (eval f) (show f))</code></pre></div> |
| 39 | +<p>Now try it for yourself! Let's see the fruits of our labour!</p> |
| 40 | +<div class="sourceCode"><pre class="sourceCode haskell"><code class="sourceCode haskell">ghci<span class="fu">></span> plotFunExprs [f, f', _F]</code></pre></div> |
| 41 | +<p>For fun, we can also plot the same functions but using our approximative functions for differentiation and integration</p> |
| 42 | +<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell">g x <span class="fu">=</span> <span class="dv">3</span> <span class="fu">*</span> x<span class="fu">^</span><span class="dv">2</span> |
| 43 | +g' x <span class="fu">=</span> deriveApprox g <span class="fl">0.001</span> x |
| 44 | +_G x <span class="fu">=</span> integrateApprox g <span class="fl">0.001</span> <span class="dv">0</span> x</code></pre></div> |
| 45 | +<p>Then plot with</p> |
| 46 | +<div class="sourceCode"><pre class="sourceCode haskell"><code class="sourceCode haskell">ghci<span class="fu">></span> plot [<span class="dt">Fun</span> g <span class="st">"3x^2"</span>, <span class="dt">Fun</span> g' <span class="st">"D(3x^2)"</span>, <span class="dt">Fun</span> _G <span class="st">"I(3x^2)"</span>]</code></pre></div> |
| 47 | +<p>Waddaya know! They look identical! I guess it just goes to show that a good approximation is often good enough.</p> |
| 48 | + |
| 49 | + </main> |
| 50 | + |
| 51 | + <footer> |
| 52 | + <nav> |
| 53 | + <span>[src: <a href="https://github.com/DSLsofMath/BScProj2018/blob/master/Physics/src/Calculus/VisVerApp.lhs">Calculus/VisVerApp.lhs</a>]</span> |
| 54 | + <span>Previous: <a href="../Calculus/Integral calculus.html">Integral calculus</a></span> |
| 55 | + <a href="../index.html">Table of contents</a> |
| 56 | + <span>Next: <a href="../Vectors/Vector.html">Vector</a></span> |
| 57 | + </nav> |
| 58 | + © Kandidatboisen (2018), GPL |
| 59 | + </footer> |
| 60 | + </body> |
| 61 | +</html> |
0 commit comments