"Arrays have incompatible sizes for this operation" with for loop in integral2

I am going to find a fixed point for some intergal represented function.
(this is just an example, I do not know if the fixed point exists or not) by the iteration:
My code is like that:
input = @(y,s) y;
N_it=10;
output=cell(1, N_it);
output{1}=input;
density = @(z,t,s) exp( -(z.^2)./(2.*(s-t)) ) ./ (sqrt(2*pi).*sqrt(s-t));
for i=1:N_it
input=output{i};
integral = @(y,t) integral2( @(z,s) input(z,s).*density(z-y,t,s),0,100,@(t)t,T,'RelTol',0,'AbsTol',1e-5);
n_output=@(y,t)integral(y,t);
output{i+1}=n_output;
end
For N_it =1, I can get the answer, there is no error. For N_it>1, when I find the value of n_output(1,1), there is an error "Arrays have incompatible sizes for this operation." and then a very long list indicating my problematic lines.

답변 (1개)

input = @(y,s) y;
N_it=10;
output=cell(1, N_it);
output{1}=input;
density = @(z,t,s) exp( -(z.^2)./(2.*(s-t)) ./ (sqrt(2*pi).*sqrt(s-t)));
for i=1:N_it
input=output{i};
integral = @(y,t) integral2( @(z,s) input(z,s).*density(z-y,t,s),0,100,@(t)t,T,'RelTol',0,'AbsTol',1e-5);
n_output=@(y,t)integral(y,t);
output{i+1}=n_output;
end
output(1,1)
ans = 1×1 cell array
{@(y,s)y}

댓글 수: 8

the parenthesis is used incorrectly when writing the formula in this line
density = @(z,t,s) exp( -(z.^2)./(2.*(s-t)) ./ (sqrt(2*pi).*sqrt(s-t)));
%-> <--
Thank you for the comment. But I am so sorry that I can not see any mistake there. May be if we look at a simplest case
density =@(z,t,s) sin(z.*(s-t));
this still gives the same error. For N_it =1, I can get the answer, there is no error. For N_it>1, there is an error.
Torsten
Torsten 2024년 3월 13일
편집: Torsten 2024년 3월 13일
Did you think about how many integrations it will need to evaluate output{10}(2,4), e.g. ? This single value will take days - if it finishes at all. Not to speak of the complete function output{10}.
VBBV
VBBV 2024년 3월 13일
편집: VBBV 2024년 3월 13일
@ho man, When N_it >1 the code works without error if you include the parenthesis according to the expressions you have The outputs shown are when N_it = 10
@VBBV I still get the same error if I replace the exp by the sine in the comment when N_it >1.
@Torsten Do you have any suggestion to tackle this?
@ho man I dont get error though. can you share your code when you replace exp with sin function as shown in your comment ? btw try with ArrayValued as true argument in the integral2 function. Change the tolerance range and check
input = @(y,s) y;
N_it=10;
output=cell(1, N_it);
output{1}=input;
density = @(z,t,s) sin(z.*(s-t));
for i=1:N_it
input=output{i};
integral = @(y,t) integral2( @(z,s) input(z,s).*density(z-y,t,s),0,100,@(t)t,T,'RelTol',0,'AbsTol',1e-5, ...
'ArrayValued',1); % use array valued argument
n_output=@(y,t)integral(y,t);
output{i+1}=n_output;
end
output(1,1)
ans = 1×1 cell array
{@(y,s)y}
Yes, your code is correct. Thank you so much. My code is the same as yours but the last line "output(1,1)" is replaced by "n_output(1,1)". What is the mistake here?
@VBBV I finally see the extra ) in the exp function in the very beginnin code, thank you.

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

질문:

2024년 3월 13일

댓글:

2024년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by