I want to integrate the following expression:
A = x + x^2 + x^3;
How can one do it using a for loop? For instance, I am trying something like as follows:
for k = 1:1:3
fun1{k} = @(x)(x.^k);
end
A_sum = cellfun(@sum,fun1{k});
int = integral(A_sum, 0, 1)
But this results in an error.
I know that the above expression can be evaluated easily without using a for loop. But the actual expression that I want to evaluate contains the summation of about 200 complex terms.

 채택된 답변

Star Strider
Star Strider 2021년 3월 16일

0 개 추천

The loop is not necessary.
Try this:
fun1 = @(x,k)(x.^k);
k = 1:3;
intv = integral(@(x)sum(fun1(x,k)), 0, 1, 'ArrayValued',1)
Check = integral(@(x) x + x.^2 + x.^3, 0, 1, 'ArrayValued',1) % Check That Vectorisation Worked
Both produce the same result:
intv =
1.083333333333333
Check =
1.083333333333333
.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2021년 3월 16일

답변:

2021년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by