Menu

Derivatives of Integral

Help
Saeed Azad
2018-03-13
2018-03-24
  • Saeed Azad

    Saeed Azad - 2018-03-13

    Hi,

    I'm using Adigator to create the derivative file of an objective function that uses a form of nimerical integration (trapz for instance). Howver, this is the error I receive:

    *Error using trapz (line 53)
    X must be a vector.
    
    *Error in adigatortempfunc1 (line 12)*
    *output=trapz(y);
    
    *Error in adigator (line 510)
    FunctionInfo = adigatortempfunc1(FunctionInfo,UserFunInputs);*
    
    *Error in Main (line 37)
    Outputs = adigator('Objfun',{d,gX,u},'Objfunxder',adigatorOptions('overwrite',1))**
    

    I used the following code to create the derivative file:

    gX = adigatorCreateDerivInput([Inf 2],struct('vodname','X','vodsize',[Inf ... (2)],'nzlocs',[(1:2).' (1:2).']));
    u= adigatorCreateAuxInput([Inf,1]);
    d= adigatorCreateAuxInput([1,2]);
    t= adigatorCreateAuxInput([Inf,1]);
    Outputs = adigator('Objfun',{d,gX,u,t},'Objfun_xder',adigatorOptions('overwrite',1))
    

    The objective function is very simple and I'm using it to get a sense of how adigator works.:

    function output=Objfun(d,x,u,t)
    y=x(:,2)+3*x(:,1)+2*d(:,1)+3*d(:,2)+u;
    output=trapz(t,y);
    

    How can I fix this? I appreciate your help.

     

    Last edit: Saeed Azad 2018-03-13
  • Matthew J. Weinstein

    Hi Saeed,

    ADiGator works based off of operator overloading and can thus only differentiate matlab functions that have been overloaded (or whose source code it can read). The issue is that I never overloaded trapz().

    A workaround is to just write out the math for trapz. This, however, will not work with vectorization as it violates the vectorization assumption that dF(X(i))/dX(j) = 0 forall i~=j. That is, your code sums over the vectorized dimension.

    So, in your case you would need to fix the vectorized dimension, and then trapz would need to be written out something like:

    k = 1:(length(t)-1);
    kp1 = 2:length(t);
    output = 0.5*sum( (y(k)+y(kp1))./(t(kp1)-t(k)) );
    

    Hope that helps!

    Matt W.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.