2017-10-13 72 views
0

我試圖用數值積分函數「積分」創建陣列處理的函數,它返回多項式的矢量

我創建四個多項式函數

[x; x^2; x^3; x^4] 

功能現在我想整合這個矢量按行使用積分函數。 我試圖使一個功能手柄功能,它通過了對「積分」功能

function f = test(x) 
    f = [x,x^2,x^3,x^4]; 
end 

但是,調用它時,命令行,我得到以下錯誤:

[email protected] 
integral(test_var,0,1) 
Error using^
One argument must be a square matrix and the other must be a scalar. 
Use POWER (.^) for elementwise power. 

Error in test (line 2) 
    f = [x,x^2,x^3,x^4]; 

Error in integralCalc/iterateScalarValued (line 314) 
      fx = FUN(t); 

Error in integralCalc/vadapt (line 132) 
     [q,errbnd] = iterateScalarValued(u,tinterval,pathlen); 

Error in integralCalc (line 75) 
    [q,errbnd] = vadapt(@AtoBInvTransform,interval); 

Error in integral (line 88) 
Q = integralCalc(fun,a,b,opstruct); 

回答

0

按照documentation of integral

q = integral(fun, xmin, xmax)

  1. 對於標量值函數fun需要使得它接受矢量輸入,併產生輸出向量來定義函數。

    對於標值的問題,函數y = fun(x)必須接受一個向量參數,x,並返回一個向量結果,y。這通常意味着樂趣必須使用數組運算符而不是矩陣運算符。例如,使用.*times)而不是*mtimes)。

  2. 如果你有一個矢量值函數fun您需要使用輸入標誌'ArrayValued'

    使用這個標誌true,表明fun是接受標量輸入功能並返回一個矢量,矩陣或ND數組輸出。

    ,並在這種情況下,上述第1項的規定是沒有必要的:

    如果設置'ArrayValued'選項設置爲true,那麼fun必須接受一個標量和返回固定大小的數組。


所以,你需要添加的輸入標誌'ArrayValued',表明你有一個矢量值函數:

f = @(x) [x; x^2; x^3; x^4]; % or [x; x.^2; x.^3; x.^4]; 
integral(f, 0, 1, 'ArrayValued', true) % or integral(@f, 0, 1, 'ArrayValued', true) 
             % if f is defined in a file 

ans = 
    0.500000000000000 
    0.333333333333333 
    0.250000000000000 
    0.200000000000000