通过另一个函数scilab构建函数

时间:2013-11-10 18:12:30

标签: scilab

我需要构建一个可以在Scilab中构建其他函数的函数。我将尝试用一个例子来解释。

//A1 and A2 g12 are functions from R^2->R^2
// Here is what I can do. 
deff('[Xprime]=Sys2(t,X)','Xprime=[A1(t,X(1:2)),A2(t,X(3:4))+g2(t,X(1:2),X(3:4))]')

现在,我不知道有多少功能A1,A2,......会有。所以我需要将它们存储在列表中。

l1 = list(); l2 = list();
l1.($+1) = A1; l1.($+1) = A2; l1.($+1) = A3; ...
l2.($+1) = g1; l2.($+1) = g2; l2.($+1) = g3; ...

我想实现这样的功能:

function Xprime=Sys(l1, l2)
       //... I do not know what to type ...
endfunction 

此功能将输出:

deff('[Xprime]=Sys2(t,X)','Xprime=[A1(t,X(1:2))+g1(t,X(1:2)),A2(t,X(3:4))+g2(t,X(3:4)), ...]')

我希望这是可以理解的。

1 个答案:

答案 0 :(得分:1)

我写了一个简短的设置,可能会有所帮助。它现在还没有完全发挥作用,但它可能会让你知道如何实现你的目标。

funcprot(0) //Mute warnings about functions definitions

function [Xprime_header, Xprime_body]=Sys(l1, l2)

Xprime_header = '[Xprime]=Sys2(t,X)'

// Add the beginning of the body
Xprime_body = "Xprime=["

count = length(l1)

// Add each argument
for i = 1:count        
    Xprime_body = Xprime_body + string( l1(i) ) + "(t,X(" + string(i) + "," + string(i+1) + "))"  

    Xprime_body = Xprime_body + "+" + string( l2(i) )  + "(t,X(" + string(i) + "," + string(i+1) + "))"

    if( i < count )            
        Xprime_body = Xprime_body + ","
    end              
end

// Add the last part
Xprime_body = Xprime_body + ',X(' + string(count-1) + ":" + string(count) + ")]"

endfunction 

l1 = list(); l2 = list();
l1($+1) = 'A1'; l1($+1) = 'A2'; l1($+1) = 'A3'; 
l2($+1) = 'g1'; l2($+1) = 'g2'; l2($+1) = 'g3'; 

[ Xprime_header, Xprime_body ] = Sys( l1, l2)

disp( Xprime_body )

// Deff your function here, so it is in the main scope
deff( Xprime_header, Xprime_body)