Parameters estimation on Lotka Volterra model with Scilab

风格不统一 提交于 2019-12-13 04:40:53

问题


I'm trying to make a parameters estimation on Lotka-Volterra model with Scilab (I am a total neophyte). When I try to run the script, Scilab warns about incoherent subtraction. I guess my problem is the same as in this topic, but the solution there uses a Matlab function.

Here is my script:

// 1. Create Lotka Volterra function

function [dY]=LotkaVolterra(t,X,c,n,m,e)
    IngestC = c * X(1) * X(2)
    GrowthP = n * X(1)
    MortC = m * X(2)
    dY(1) = GrowthP - IngestC
    dY(2) = IngestC * e - MortC
endfunction

// 2. Define the Nonlinear Least Squares functions

function f = Differences ( x ) 
    // Returns the difference between the simulated differential 
    // equation and the experimental data.
    c = x(1) ;n = x(2);m = x(3);e = x(4);y0 = y_exp(1,:);t0 = 0
    y_calc=ode(y0',t0,t,list(LotkaVolterra,c,n,m,e)) 
    diffmat = y_calc' - y_exp
    f = diffmat(:)
endfunction 

function val = L_Squares ( x ) 
    // Computes the sum of squares of the differences.
    f = Differences ( x ) 
    val = sum(f.^2)
endfunction 

// Experimental data 
t = [0:19]'; 
H=[20,20,20,12,28,58,75,75,88,61,75,88,69,32,13,21,30,2,153,148];
L=[30,45,49,40,21,8,6,5,10,20,33,34,30,21,14,8,4,4,14,38];
y_exp=[H',L'];


// compute the model cost function
function [f, g, ind] = modelCost (x, ind)
    f = L_Squares ( x )
    g = derivative ( L_Squares , x )
endfunction

// use of optim function with loops to avoid local minimum 
tic
i=0
fitminx=zeros(4,100);
fitminy=zeros(1,100);
for c=[0:0.1:1]
    for n=[0:0.1:1]
        for m=[0:0.1:1]
            for e=[0:0.1:1]
                i=i+1
                x0 = [c;n;m;e]
                [ fopt , xopt , gopt ] = optim ( modelCost , x0 )
                fitminx(:,i)=xopt;
                fitminy(:,i)=fopt;
            end
        end
    end 
end
[a,b]=min(fitminy)
fitminx(:,a)
toc

the error message is :

lsoda--  at t (=r1), mxstep (=i1) steps   
needed before reaching tout
      where i1 is :        500                                                  
      where r1 is :   0.4145715729197D+01                                       
Attention : Le résultat est peut être inexact.

 !--error 9 
Soustraction incohérente.
at line       4 of function Differences called by :  
at line       2 of function L_Squares called by :  
at line      16 of function %R_ called by :  
at line      15 of function %deriv1_ called by :  
at line      58 of function derivative called by :  
at line       3 of function modelCost called by :  
                [ fopt , xopt , gopt ] = optim ( modelCost , x0 )

thanks for the interest and the time you give to my problem (and sorry for my english)


回答1:


Duplicate of my answer here

The problem is that the solver somehow reach a point where it cannot solve the ode on every t and stops at a certain point. Thus your y_calc is smaller than y_exp in size.

If this is not a problem for you, it is simply solved by changing diffmat on line 6 of the Differences function to

diffmat = y_calc' - y_exp(1:size(y_calc',1),:)



回答2:


Your problem is that during the optimization c,n,m,e parameters get negative values. Just add constraints in your optim call, like this:

[fopt, xopt, gopt] = optim(modelCost, 'b', zeros(4,1), %inf*ones(4,1), x0)



回答3:


The problem is in

diffmat = y_calc' - y_exp

Adding the following code:

disp( "Y_calc dimensions:");
disp( size(y_calc'));

disp( "y_exp dimensions:");
disp( size(y_exp));    

Found:

Y_calc dimensions:    
91.    2.  

y_exp dimensions:    
20.    2.  

I know nothing about the expected behaviour and the expected matrix sizes, but that is at least the root cause of the error.



来源:https://stackoverflow.com/questions/22614164/parameters-estimation-on-lotka-volterra-model-with-scilab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!