Why am I getting this error? The integrand function must return an output vector of the same length as the input vector.

12 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Patrik Ek
Patrik Ek il 2 Apr 2014
Modificato: Patrik Ek il 2 Apr 2014
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 Commenti
Amy
Amy il 4 Apr 2014
When I remove the x and N inputs (lines 2 and 3 of the AW_int.m) the following error is being thrown up:
Error using AW_int (line 4)
Not enough input arguments.
Is there no need to define the inputs for ASqr etc again as well?
Thanks

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Oil, Gas & Petrochemical in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by