Integration using quad(@myfun,a,b)

I have the following function
function AW1= AW_int()
ASqr=aSqrSum();
BSqr=bSqrSum();
C=CSum();
delta2=(ASqr.*BSqr)-(C.^2);
delta=delta2.^(1/2);
AW1=delta./ASqr;
format long e
delta2;
delta;
%delta2; delta;
AW1;
end
Using the following functions for ASqr, BSqr and C
function ASqr =aSqrSum(x,N)
N=[1000 10000 100000];
x=[0.9 0.99 0.999 0.9999];
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
function C = CSum(x,N)
N=[1000 10000 100000];
x=[0.9 0.99 0.999 0.9999];
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
format long e
ASqr;
end
function BSqr =bSqrSum(x,N)
N=[1000 10000 100000];
x=[0.9 0.99 0.999 0.9999];
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
When i try to integrate the function AW_int using
Q = quad(@AW_int,0,0.9999)
The following error is returned:
Error using AW_int
Too many input arguments.
Error in quad (line 72)
y = f(x, varargin{:});
Would anyone be able to help me fix this? As I have tried a few different things but the same error is still be returned.
Thanks

 Accepted Answer

Your AW_int function doesn’t take any input arguments:
function AW1= AW_int()
Also, you are not passing any arguments to these functions within AW_int, so they will likely throw a similar error when AW_int executes:
ASqr=aSqrSum();
BSqr=bSqrSum();
C=CSum();
They all require x and N as inputs, so pass those to AW_int and then to the other functions.

14 Comments

I changed the code in AW_int.m to the following
function AW1= AW_int(x,N)
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;
%delta2; delta;
AW1;
end
Now when I run the command
Q = quad(@AW_int,0,0.9999)
The following error is being returned
Error using AW_int (line 4)
Not enough input arguments.
Error in quad (line 72)
y = f(x, varargin{:});
Im unsure by what you mean as to pass the inputs?
I’m not certain what you are doing with your code. The function definitions don’t seem to be necessary anyway. Simplifying your code to this:
N=[1000 10000 100000];
x=[0.9 0.99 0.999 0.9999];
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
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
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
delta2=(ASqr.*BSqr)-(C.^2);
delta=delta2.^(1/2);
AW1=delta./ASqr;
produces a (3x4) matrix:
AW1 =
5.2632e+000 50.2512e+000 262.8884e+000 288.4154e+000
5.2632e+000 50.2513e+000 500.2499e+000 2.6267e+003
5.2632e+000 50.2513e+000 500.2501e+000 5.0002e+003
What do you want to do with it?
integrate the function AW_int with respect to x?
Adding this statement after AW1:
Q = trapz(x', AW1')
gives:
Q =
4.1554e+000 6.3825e+000 7.4506e+000
it seems for different values of N. Does that do what you want?
Thankyou! Yes its for a research thing. Can it not be done using quad? As I know its more accurate
My pleasure!
To use quad you need to have a continuously-valued function. (The same goes for integrate.) You may recast your problem to be a continuously-valued function, but you can’t use quad with your AW1 here.
Thanks again for your help. As I'm integrating with respect to x, is it possible to do this between a and b, where a = 0 and b = 0.9999 (the highest value of x)?
Yes. It’s probably easy to do this with ‘anonymous functions’ and use integrate to do it with a bit more precision. I’ve been puzzling out what you’re doing from your code, thus far without much success. If you could describe what you’re doing (with respect to your calculations, since you would likely prefer not to discuss the details of your research here), we might be able to do what you want with more precision and flexibility.
I have the following equation:
= 1/π ∫_a^b〖(∆/A^2 )〗dx
where a = 0 and b = 0.9999.
ASqr is equal to the function I have given above, and delta is also already shown. The values for ASqr and delta were calculated based on values of x close to,but not equal to, 1. (0.9 0.99 0.999 0.9999) and N is sufficently large (1000 10000 100000).ASqr is the geometric sequence - ∑x^(2j).
Confused here. If a = 0 then isn’t your integral identically zero?
I thought was calculated from . How do and enter into all this?
Do you have a PDF of a paper that you could attach to your original question (‘paperclip’ icon) that could explain this in a bit more detail?
Ive attached a file, thank you again!
The PDF contains the code you’ve already posted. I was sort of hoping for an analytic discussion of the expressions you want to integrate. I would like to see if I can code (preferably ‘anonymous’) functions from it.
Do you have a PDF of a published paper reference with this information you can upload as an attachment? (A link alone may not work. I don’t have access to a uni library, so am restricted to the journals I subscribe to.)
Sorry.. hope this helps
Definitely helps, but I’m a bit baffled by the implied recursion. You calculate
∆² = A²B²-C²
all of which are functions of x, and with
A² = ∑x²ʲ
then go back and integrate ∆/A² w.r.t. x.
Also, when I calculate (with EN the asymptotic expression), I get:
Q/(EN*pi) = 1.2031 1.3860 1.2943
perhaps accounted for by σ² not appearing anywhere, but just a guess on my part.
If there’s no recursion, and you treat ∆/A² essentially as a constant, then you pretty much have calculated Q about as well as can be expected, except for dividing it by pi. I am not convinced that using continuous functions and integrate would be of any significant benefit.
If there is recursion, then it seems to me you carry out the summations and calculate ∆/A² for each x(j), integrate from a to b, and repeat until you reach j=(n-1). Maybe I’m missing something.

Sign in to comment.

More Answers (0)

Asked:

Amy
on 4 Apr 2014

Commented:

on 5 Apr 2014

Community Treasure Hunt

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

Start Hunting!