Author Topic: Programming as Mathematics  (Read 7048 times)

0 Members and 1 Guest are viewing this topic.

Nation of One

  • { }
  • { ∅, { ∅ } }
  • Posts: 4756
  • Life teaches me not to want it.
    • What Now?
Re: math code ---> euler and runge-kutta
« Reply #15 on: March 16, 2016, 12:00:28 am »
I find it very satisfying to understand just enough about code and math to be able to find source code and alter it to suit my needs.  For instance, when in the start of a problem set in one of the DE texts, the authors suggest one use a computer algebra system, or, alternatively writing a program for solving initial value problems, I am thankful that I at least have the confidence to tweak the code ...

If someone's code is written for working with input and output files, I like to alter it to interact with me at the console.  Also, I don't mind redefining the differential equation for each case right in the source code and recompile it each time.  I just comment out all the equations in case I need one again.  Oh, and lest I pull the kind of thing Sickmind Fraud pulled (not acknowledging Schopenhauer's influence on his theories), I better mention that euler3.cpp was more than a little inspired by code written  Alex Godunov. Last revision - March 2007. I found it here, (Computational Physics Programs C/C++)


Also, at compile time, sometimes it is just something as simple as adding a flag such as -static or -std=c++11 to get it to run ...

When I wake up in the morning, I will be glad I tinkered on some code BEFORE diving into the exercises with pencil and paper.  Often, I like to transcribe the working source code into a notebook in pencil since the code itself basically explains the algorithms.   For years I have always liked to make code look as much like the mathematics as possible, calling functions simply f(x, t) or what have you.

Rather than have 3 separate programs to help me check some problems in a numerical methods section, this code (euler3.cpp) gives a menu:

 Solver for first order Ordinary Differential Equations
 
 x'(t) = f1(x,t)   equation
 x(ti) = xi        initial condition
 
 Methods (select one by a key)
 key = 0; simple Euler
 key = 1; modified Euler (predictor-corrector)
 key = 2; 4-th order Runge-Kutta

to compile with gnu gcc:

g++ -g euler3.cpp -o e3 -static

then, to run: ./e3

I like to slow down long enough to compare results with the "spreadsheet versions."

Actually, I find writing simple C++ code, even if spending a little extra time formatting the output so it's easier to read, ends up being more fun than using the spreadsheet method.  With the code, you just have to define the differential equation and recompile, whereas with a spreadsheet, you have to change the cell for y', and then copy and fill down - rather tedious. 

So, in the version I am using, I formatted the output for the console, even placing an increment counter n on both sides of the values of "independent variable" t and "dependent function" x.

I swear, 20 years ago, when I began studying programming in earnest and returning to college to start over with Calculus and Physics, this really is what I envisioned ... growing older by the day and being able to tinker around with computer programs that helped me analyze* mathematical problems.  This is all I ever wanted, so, even though I may appear to be the classic "loser" who has lived an uneventful life, at least, along the way, I have been able to take just enough interest in what the "eggheads" have been up to so as to ... well .. how else to phrase this? ... to be mentally stimulated.

I could not have asked for more from or for myself.  I think I may actually be content.   :-\

* analyze vs. analyse?

You will notice I use the "American English" as opposed to the "British English".  I'm always doubting myself when the editor puts those squiggly red lines under such words.  I kept thinking I forgot how to spell humor or endeavor when it is just the absence of the British u.

I beg forgiveness of the creators of the freeboards free forum software for my lack of manners.  I'm not confused, just stubborn.   I don't take orders from software.  ;)

One of these days, Holden, you might catch this math-code fever ... then you will surely not want to report to a boss ... since you will become a mathematical madman.   :D
« Last Edit: March 16, 2016, 11:36:47 pm by H »
Things They Will Never Tell YouArthur Schopenhauer has been the most radical and defiant of all troublemakers.

Gorticide @ Nothing that is so, is so DOT edu

~ Tabak und Kaffee Süchtigen ~