0

I am relatively new to c++ coding and try to write a program to solve differential equations numerically. I use codeBlocks as the compiler for that and work under windows. The numeric solver already works well. My program contains of some very long formulas which are created by mathematica and converted into cpp-language. Then the formulas are stored in a .txt-file.I can already read the formula as a string, but not use it to calculate things because the program has to interpret the formula as a double-type math and not as a string. The problem here is, that my formula does not contain only numbers, but letters as variables (their value is set in the program) and other mathematical symbols. That is why I think I cannot just use "atof" (http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof) or other conversion functions. (If I am wrong at this point, I would be glad to learn how to use the function for this problem!)

Here is some example code from my little program:

//Program to solve ODEs
#include <iostream>
#include <math.h>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h> //to measure the time
#include <stdio.h>
#include <conio.h>

using namespace std;

int main(void)
{
    double k1=0;

    ifstream file("Formelvu1.txt");//file with the fromula
    string line;
    stringstream longform;
    while(getline(file, line)){ //read the formula and store them
            longform << line;   //store the string in "longform"
            cout << longform;
        }
            return 0;

for(double t=0; t<10; t++){

        k1 = (longform) * t; //simple operation with the formula

}
return 0;
}

This code doesn't work, becuase longform is no double... longform is a string with something like: ab+pow(t,3)-sin(tb)/x.

I already found several questions related to this topic, but none of them was easy enough for me to understand or the right thing I want to do:

How can I convert string to double in C++?

From what I understand is this guy trying the nearest from what I wish to do:

Evaluate math formula from String using ScriptEngine

But I don't understand the code completely. If it is useful for my problem: What does this part do?

try{
    return (Double)engine.eval(tmp);
}
    catch(Exception fexp)
    {

    }

I also heard about parser which can interpret the xpressions line muparser: http://muparser.beltoforion.de/mup_eval.html But I don't know if this would be more than I need...

I appreciate every answer/response and help with this problem. Thank you!

Community
  • 1
  • 1
Arne H.
  • 3
  • 2

1 Answers1

0

You have to study carefully what is included in the basic C/C++ language and standard library and what not.


However, if you get a C++ compatible expression from mathematica, then you can let the C++ compiler do its work as in

double myfunc(double a, double b, double c, double t, double x) {
    return
    #include "Formelvu1.txt"
    ;
}
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
  • Thank you very much for your response! It works well, but the simulation is very slow now. Does this code read the txt every time I call myfunc? Is there any similar or other method which could be faster? Thank you again for that quick answer. – Arne H. Sep 03 '15 at 18:43
  • No, the preprocessor of the compiler reads the file once, then it is compiled as normal, inline, code. Typical reasons for abnormally slow programs are frequently writing to the console or frequently writing to a file with too short a buffer. Use a profiler to find out where the program spends most of its time. – Lutz Lehmann Sep 03 '15 at 20:34