[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Calling fsolve from C++
From: |
Tatsuro MATSUOKA |
Subject: |
Re: Calling fsolve from C++ |
Date: |
Sun, 4 May 2014 08:00:25 +0900 (JST) |
--- On Sat, 2014/5/3, vicmota wrote:
> I've seen this example.
> I use this to call /fzero /and it works:
>
> octave_value_list fzero_args;
> octave_value_list fcn;
> fcn(0) = "x^7/(x-8) - 5*x + 143";
> fzero_args(0) = Finline(fcn, 1)(0);
> fzero_args(1) = octave_value(3);
> octave_value_list zero = feval("fzero", fzero_args, 2);
> printf("ans = %.3lf\n", zero(0).double_value());
>
> But please note that I pass an inline function handle to /feval/, with 1
> equation and 1 variable (x).
> What I wish to do is to define a set of functions in C++ to pass through
> /feval /to /fsolve/ (aka System of non-linear equations).
>
> Is this even possible?
>
I have invoked the further:
test function is written in the oct file (Perhaps it may contribute speed up)
//testfs.cc
#include <octave/oct.h>
DEFUN_DLD (testfs, args, nargout,
"test function of fsolve")
{
ColumnVector x (args(0).vector_value ());
ColumnVector y(2);
y(0) = x(0)*x(0) + x(1)*x(1) - 36;
y(1) = x(0)*x(0) - x(1)*x(1) - 1;
return octave_value(y);
}
octave test function:
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/toplev.h>
int
main (void)
{
string_vector argv (2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main (2, argv.c_str_vec (), 1); octave_value_list fsolve_args;
octave_value_list fcn;
ColumnVector ini_x(2);
fsolve_args(0) = "testfs";
ini_x(0) = -5; ini_x(1) = 5;
fsolve_args(1) = octave_value(ini_x);
octave_value_list zero = feval("fsolve", fsolve_args, 2);
std::cout << zero(0).vector_value()
<< std::endl;
clean_up_and_exit (0);
}
$ mkoctfile testfs.cc
$ mkoctfile --link-stand-alone fsolvetest.cc -o fsolvetest
$ ./fsolvetest
sh-3.1$ ./fsolvetest
2-dimensional array (2x1)
data: -4.30116
4.1833
The results are consistent with those obtained by the Octave interpreter:
octave:12> function y = f (x)
> y = zeros (2, 1);
> y(1) = x(1)^2 + x(2)^2 - 36;
> y(2) = x(1)^2 - x(2)^2 - 1;
> endfunction
octave:13>
octave:13> [xy, fval] = fsolve ('f', [-5; 5]);
octave:14> xy
xy =
-4.3012
4.1833
Regards
Tatsuro