skip to Main Content

I need to solve a transcendental equation in C++ using Octave API, but the program crashes. Maybe there is a problem with indexes?

Code:

#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>

int main() {
    // Define the equation as a string
    std::string equation = "(2*x-3)^(2/3) - (x-1)^(2/3)";

    // Construct the Octave command to solve the equation
    std::string command = "fsolve(@(x) " + equation + ", 0)";

    // Parse and evaluate the Octave command
    octave_value_list retval = octave::feval ("eval", octave_value (command));

    // Extract the solution from the Octave output
    double solution = retval(0).double_value();

    // Print the solution
    std::cout << "Solution: x = " << solution << std::endl;

    return 0;
}

Compile:

g++ -o trans trans.cpp -I/usr/include/octave-6.4.0 -I/usr/include/octave-6.4.0/octave -I/usr/include/octave-6.4.0/octave/interpreter -I/usr/include -I/usr/include/octave-6.4.0/octave -I/usr/include/octave-6.4.0/octave/octave-config.h -loctave -loctinterp -Wl,-rpath,/usr/lib/x86_64-linux-gnu/octave/6.4.0 -L/usr/lib/x86_64-linux-gnu/octave/6.4.0 -Wl,--no-as-needed -loctave -loctinterp
cc1plus: warning: /usr/include/octave-6.4.0/octave/octave-config.h: not a directory

Error:

Аварийный останов (образ памяти сброшен на диск)

which translates to “Emergency stop (memory image flushed to disk)” according to Google Translate.

I was able to solve an algebraic equation using the example of finding the roots of a polynomial, but is it possible to solve transcendental equations like in my code above?

2

Answers


  1. Chosen as BEST ANSWER

    Finally got to the debugger, sorry for getting to it late. Changed the code as follows:

    #include <iostream>
    #include <octave/oct.h>
    #include <octave/octave.h>
    #include <octave/parse.h>
    #include <octave/interpreter.h>
    
    using namespace std;
    
    int main() {
        // Define the equation as a string
        std::string equation = "(2*x-3)^(2/3) - (x-1)^(2/3)";
        
        cout << "Easy 1 " << "n";
    
        std::string command = "res = fsolve(@(x) " + equation + ", 0)";
        
        cout << "Easy 2 " << "n";
        
        octave::feval("eval", octave_value(command));
        
        cout << "Easy 3 " << "n";
        
        octave_value_list retval = octave::feval("res");
        
        cout << "Easy 4 " << "n";
    
        // Extract the solution from the Octave output
        double solution = retval(0).double_value();
        
        cout << "Easy 5 " << "n";
    
        // Print the solution
        std::cout << "Solution: x = " << solution << std::endl;
        
        cout << "Easy 6 " << "n";
    
        return 0;
    }
    

    The compilation is still the same.

    Here's what the debugger came up with:

    (gdb) run
    Starting program: /home/zoow/Desktop/OctaveAPI/Transcendent/trans 
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    [New Thread 0x7fffecdff640 (LWP 5006)]
    [New Thread 0x7fffe45fe640 (LWP 5007)]
    [New Thread 0x7fffdbdfd640 (LWP 5008)]
    [New Thread 0x7fffd35fc640 (LWP 5009)]
    [New Thread 0x7fffd2dfb640 (LWP 5010)]
    
    Thread 1 "trans" hit Breakpoint 1, 0x0000555555556751 in main ()
    (gdb) nex
    Ambiguous command "nex": next, nexti.
    (gdb) next
    Single stepping until exit from function main,
    which has no line number information.
    Easy 1 
    Easy 2 
    
    Thread 1 "trans" received signal SIGABRT, Aborted.
    __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737217395584) at ./nptl/pthread_kill.c:44
    44  ./nptl/pthread_kill.c: Нет такого файла или каталога.
    

    The stopping point is on the line:

    octave::feval("eval", octave_value(command));
    

  2. I’m assuming you’re getting a segmentation error. Because you are indexing into an empty array.

    In Octave, eval doesn’t return anything. So octave_value_list retval = octave::feval ("eval", octave_value (command)); is an empty list. retval[0] doesn’t exist.

    Don’t use eval. It’s evil.
    But for simplicity, this is how you’d use it:

    std::string command = "res = fsolve(@(x) " + equation + ", 0)";
    octave::feval("eval", octave_value(command));
    octave_value_list retval = octave::feval("res");
    

    (code not tested.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search