How to use integral with an implicit function defined through a sum

3 ビュー (過去 30 日間)
Alex Dytso
Alex Dytso 2020 年 7 月 16 日
回答済み: Steven Lord 2020 年 7 月 16 日
Suppose we have a function define as follows:
fz=@(z) exp(-(z).^2/2);
g=@(y) sum ( v(1:n/2).*fz((y- v(n/2+1:n))));
here v is a vector of length n that is known and is used to define the fucntion g.
Now I want to integrated g from [-5,5] using command
integral(@(y) g(y), -5,5).
I, however, get an error that matrix dimensions must agree.
I get where the issue is. It occurs because I defined a function through a vector and pass another vector as input.
Is there a way of fixing this?
  2 件のコメント
David Hill
David Hill 2020 年 7 月 16 日
What is the v function?
Alex Dytso
Alex Dytso 2020 年 7 月 16 日
編集済み: Alex Dytso 2020 年 7 月 16 日
Right, I forgot to say what it is. It's a vector of length n that is known and is used to define a function g.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2020 年 7 月 16 日
By default, integral will call your function with an array and expects your function to return an array of the same size as the output. If you specify the name-value pair arguments 'ArrayValued', true in your integral call it will instead call your function with a scalar and expect an array as output.
v = 2:5;
y1 = integral(@(x) x.^v, 0, 1, 'ArrayValued', true)
y2 = integral(@(x) x.^v, 0, 1) % Throws an error

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by