I have a cell of functions, which I want to add after integration of each in the cell. Further, I have to multply each fucntion with a constant from another cell. This new cells of anonymous functions thus obtained is to be added .i.e the elements have to be summed up. I tried using a for loop. Following is a approach I used,but I do not seem to be getting the right constant i.e. in the function MATLAB just shows cons{j} instead of the actual value stored there (a symbolic constant). Neither am I getting the summed up total funtion. Kindly help.
f = {@(x)x , @(x)x^2}
syms a b
c = {a b}
g = @(y) y
h = 0;
for j=1:length(2)
h = @(x) c{j}*integral(f{j}(x), 0,1) + h
end

 채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 1월 2일
Use symbolic integration -
syms x a b
f = {x x^2}
f = 1×2 cell array
{[x]} {[x^2]}
c = [a b];
h = 0;
for j=1:numel(f)
h = c(j)*int(f{j}, x, 0, 1) + h;
end
h
h = 

댓글 수: 6

Turns out I can not use symbolic variable to carry out my integration. You might remember it from my previous querry on Multivariable Integration. Is there any way I can do this in an interative method?
I see.
Try this -
f = @(x) [x x.^2]
f = function_handle with value:
@(x)[x,x.^2]
out = integral(f, 0, 1, 'ArrayValued', 1)
out = 1×2
0.5000 0.3333
syms a b
c = [a b];
h = c.*out
h = 
Aviral Srivastava
Aviral Srivastava 2024년 1월 2일
편집: Aviral Srivastava 2024년 1월 2일
Sorry, this doesn't seem to be working for me: -
f = @(x) [x , x.^2]
syms a b
c = [a b]
g = @(y) y
h = @(x,y) f(x).*g(y)
out = @(y) integral(@(x) h(x,y), 0,1,true)
out = @(y) c.*out(y)
out(5)
'ArrayValued' is missing from the integral() call -
f = @(x) [x , x.^2];
syms a b
c = [a b];
g = @(y) y;
h = @(x,y) f(x).*g(y);
fun = @(y) c.*integral(@(x) h(x,y), 0,1, 'ArrayValued', 1)
fun = function_handle with value:
@(y)c.*integral(@(x)h(x,y),0,1,'ArrayValued',1)
out = fun(5)
out = 
Thank you!
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by