Question: Fitting system of odes by Optimization

restart;
Digits := 25;

# Setup.
de := diff( x(t), t ) = a * y(t), diff( y(t), t ) = b * x(t);
ic := x(0) = 1, y(0) = 0;
sol := dsolve( { de, ic } ):

# Sample values for specific parameter values.
a0, b0 := 1.0, 0.25:
T := evalf( [ seq( i, i=1..5 ) ] ):
X := [ seq( eval['recurse']( x(t), [ op( sol ), a = a0, b = b0 ] ), t=T ) ]:
Y := [ seq( eval['recurse']( y(t), [ op( sol ), a = a0, b = b0 ] ), t=T ) ]:

# Numerical solution.
sol := dsolve( { de, ic }, 'numeric', 'range'=min(T)..max(T), 'abserr'=1e-15, 'maxfun'=0, 'parameters'=[a,b] ):

# Procedure to return values for specific time and parameter values.
u := proc( t, a, b )
	sol( 'parameters' = [ ':-a' = a, ':-b' = b ] ):
	try
		return eval( [ x( ':-t' ), y( ':-t' ) ], sol( t ) ):  
	catch:
		return 10000:
    end try:
end proc:

# Procedure to return objective function.
r := proc( a, b )
	local A, t, p, q, x, y:
	A := ListTools:-Flatten( [ seq( u( t, a, b ), t=T ) ] -~ zip( (x,y) -> [x,y], X, Y ) ):
	return foldl( (p,q) -> p + q^2, 0, op(A) ):
end proc:

# Find parameter values for bet fit.
Optimization:-Minimize( 'r'(a,b), a=-10..10, b=-10..10 );

I was provided the code above by Maple support, to fit a model to data using the Optimization package. I have tried to adapt this to a situation where one might only have the X variable data and so the model should only fit to that data.  I did this by deleting the , y( ':-t' ) in the try catch statement and then having the zip statement read as zip( (x) -> [x], X).

When I try to run that code, I get the error:

Error, (in Optimization:-NLPSolve) invalid input: zip uses a 3rd argument, b, which is missing

Anybody have an idea how to solve this?

 

Jo

Please Wait...