Why am I getting this error? The integrand function must return an output vector of the same length as the input vector.
조회 수: 14 (최근 30일)
이전 댓글 표시
Hi
I have the following function
function AW1= AW_int(x,N)
N=[10 100 1000 10000 100000];
x=[0.9 0.99 0.999 0.9999 0.99999 0.999999 0.9999999];
ASqr=aSqrSum(x,N);
BSqr=bSqrSum(x,N);
C=CSum(x,N);
delta2=(ASqr.*BSqr)-(C.^2);
delta=delta2.^(1/2);
AW1=delta./ASqr;
format long e
delta2;
delta;
AW1
end
With the following functions for aSqrSum, bSqrSum and CSum:
function ASqr =aSqrSum(x,N)
N=[10 100 1000 10000 100000];
x=[0.9 0.99 0.999 0.9999 0.99999 0.999999 0.9999999];
ASqr = zeros(length(N), length(x));
for ii = 1:length(N)
for n = 0:N(ii)-1
ASqr(ii,:) = ASqr(ii,:) + (x.^(2*n));
end
end
format long e
ASqr;
end
function BSqr =bSqrSum(x,N)
N=[10 100 1000 10000 100000];
x=[0.9 0.99 0.999 0.9999 0.99999 0.999999 0.9999999];
BSqr = zeros(length(N), length(x));
for ii = 1:length(N)
for n = 0:N(ii)-1
BSqr(ii,:) = BSqr(ii,:) + ((n.^2).*x.^(2.*(n)-2));
end
end
format long e
BSqr;
end
function C = CSum(x,N)
N=[10 100 1000 10000 100000];
x=[0.9 0.99 0.999 0.9999 0.99999 0.999999 0.9999999];
C = zeros(length(N), length(x));
for ii = 1:length(N)
for n = 0:N(ii)-1
C(ii,:) = C(ii,:) + (n.*x.^(2.*(n)-1)) ;
end
end
format long e
C;
end
I want to integrate the function AW_int with respect to x between 0 and 0.9999. However when I run the following in the command line:
quad(@AW_int,0,0.9999)
I get the following error:
Error using quad (line 75)
The integrand function must return an output vector of the same length as the input
vector.
Would anyone be able to explain to me what this error means? As im unsure of where I am going wrong and how to fix it.
Thank you in advanced for any possible help!
댓글 수: 0
채택된 답변
Patrik Ek
2014년 4월 2일
편집: Patrik Ek
2014년 4월 2일
This function you use, quad uses an adaptive recursive algorithm to get an answer with below some tolerance. This method calls the function AW1_int with variable length input vectors, and reuqres the return vector to be of the same size as the input. Since you have x and N as input vectors and then redefines them inside the function, the length of the output will definitely not be the same as the input. Think of it like this.
"A function input must never be redefined inside the function."
It can be modified, but not defined again. In that case the input is unnecessary. Eg
function y = f(x)
x = 0:100; % Not good.
y = x.^2;
end
function y = g(x)
u = x.^2;
x = x+1; % Ok since x is modified, but not redefined.
y = u./x;
function y = h(x)
u = x.^2
x = 13; % Ok since x is used before it is redefined.
y = u*x;
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Oil, Gas & Petrochemical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!