Defining vector of integrals with undefined function

4 ビュー (過去 30 日間)
Florian Spicher
Florian Spicher 2021 年 10 月 26 日
回答済み: Yash 2024 年 2 月 21 日
Hi! What I'd like to do is to define a function r, which takes a function f and a partition xi as arguments. r would be a vector, which elements would be defined by the integral from 0 to 1 of f*bj. Where f is the given function and the bj's are functions defined before in my program. The only thing that really matters for these functions are that they depend on x.
My problem is I don't really know how to do this. Here's my lattest attempt. Thanks for the help!
function int_r=r(f,xi)
n=size(xi,2);
int_r=zeros(1,n);
for j=1:n
int_r(j)=integrate(@(x)f(x)*b(j,x,xi),0,1));
end
end

回答 (1 件)

Yash
Yash 2024 年 2 月 21 日
Hey,
Your code looks almost correct. The only issue is with the syntax of the integral function. You should replace integrate with integral, which is the correct MATLAB function for numerical integration. Here's the corrected version:
function int_r = r(f, xi)
n = size(xi, 2);
int_r = zeros(1, n);
for j = 1:n
int_r(j) = integral(@(x) f(x) * b(j, x, xi), 0, 1);
end
end
Read about the integral function here: https://www.mathworks.com/help/matlab/ref/integral.html
Hope this helps!

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by