forum.alglib.net
http://forum.alglib.net/

Use the ode solver in Delphi
http://forum.alglib.net/viewtopic.php?f=2&t=4380
Page 1 of 1

Author:  akame2021 [ Wed Mar 31, 2021 5:20 pm ]
Post subject:  Use the ode solver in Delphi

Hi everyone,

I'm trying to use your ode solver in Delphi. And I have one question about the function in your XALGLIB.pas file. In you manual, you have the following example:

program XTestALGLIB;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
SysUtils, Classes, Math, XALGLIB;

procedure ode_function_1_diff(const y: TVector; x: Double; dy: TVector; obj: Pointer);
begin
//
// This callback calculates f(y[],x)=-y[0]
//
// An additional parameter obj may be used to pass some data to this
// callback (if you pass some pointer value as obj parameter of
// optimizer method, it will forward this pointer to callback).

//
// IMPORTANT: you should NOT overwrite dy parameter by another
// instance of TVector array: say, you should not write
// code like ";SetLength(dy,N)".
//
// The reason is that dy points to preallocated array;
// if you reallocate it, optimizer will have no way of
// finding out reference to new array.
//
// Thus, you should work with dy parameter without
// reallocation/reinitializing it.
//
dy[0] := -y[0];
end;

var
y: TVector;
x: TVector;
eps: Double;
h: Double;
s: Todesolverstate;
m: TALGLIBInteger;
xtbl: TVector;
ytbl: TMatrix;
rep: Todesolverreport;

begin
s:=nil;

y:=Str2Vector('[1]');
x:=Str2Vector('[0, 1, 2, 3]');
eps:=0.00001;
h:=0;
odesolverrkck(y, x, eps, h, s);
odesolversolve(s, ode_function_1_diff, nil);
odesolverresults(s, m, xtbl, ytbl, rep);
WriteLn(Format('%d', [m])); // EXPECTED: 4
WriteLn(Format('%s', [FormatVector(xtbl,2)])); // EXPECTED: [0, 1, 2, 3]
WriteLn(Format('%s', [FormatMatrix(ytbl,2)])); // EXPECTED: [[1], [0.367], [0.135], [0.050]]

FreeAndNil(s);

ReadLn;
ExitCode:=0;
end.

The Diff function has an input obj, I'm trying to pass some parameter to this pointer but I got some 'c0000005 ACCESS_VIOLATION' error at this step. Can anyone explain how to use this obj to pass some parameter from outside of the function? Thank you very much!!

Sincerely,
AKA

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/