Using a vector input in integral function.

6 ビュー (過去 30 日間)
fizzy
fizzy 2020 年 7 月 30 日
コメント済み: fizzy 2020 年 7 月 31 日
Let's say I have a function "myfun" of variable x and its integral:
myfun = @(x) x; % A continuous function of variable x (exist for any x value between integration limit, 1 to 5)
myfunint = integral(myfun,1,5); % integration between x=1 and x=5
Now I have a vector which is also a function of this same variable x (but it's a vector!)
myvec= [0.5 1.5 2.5 3.5 4.5]; % values corresponding to x = 1,2,3,4,5
and my new function "myfunnew" is a multiplication of myvec with myfun..something like
myfunNew = @(x) myvec(x).*x; % Not continuous because myvec(x) only exists for certain x values (x=1,2,3...5) and not for values inbetween.
myfunNewint = integral(myfunNew,1,5)
and I want to use integral function on myfunNew.
But it won't work because "myvec" is a vector and "myvec(x)" doesn't exist for a lot of values of x between 1 and 5 that may be required by integral function when it evaluates integral. So my question is:
  • How can I convert this vector "myvec" to a continuous function (preferably using linear interpolation for missing values) so that I can use it in "myfunNew" such that it becomes integrable.
I can not use trapz.
  2 件のコメント
madhan ravi
madhan ravi 2020 年 7 月 30 日
Why not use ArrayValued option to true?
fizzy
fizzy 2020 年 7 月 30 日
Because that's not what I need. The result of integration, myfunNewint, will be a single value.

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

採用された回答

Alan Stevens
Alan Stevens 2020 年 7 月 31 日
Like this?
X = 1:5;
myvec= [0.5 1.5 2.5 3.5 4.5]; % values corresponding to x = 1,2,3,4,5
vec =@(x) interp1(X,myvec,x); % linear interpolation
myfunNew = @(x) vec(x).*x;
myfunNewint = integral(myfunNew,1,5)
  1 件のコメント
fizzy
fizzy 2020 年 7 月 31 日
Exactly! This is so simple. I guess I wasn't using the correct words in google search

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by