Definite Integral of a Complicated Function

5 views (last 30 days)
I've been trying for a few hours to figure out what I'm doing wrong here and would really appreciate a push in the right direction. I'm trying to compute the definite integral of the abs(y(t)).^2 from 5 to 27 but no matter how I try to do it I get the following errors:
"Error using integralCalc/finalInputChecks (line 515) Output of the function must be the same size as the input. If FUN is an array-valued integrand, set the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315) finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132) [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75) [q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88) Q = integralCalc(fun,a,b,opstruct);"
I feel like I have to be close because my first integral of x works, it's just my integral of y that doesn't. Any help will be greatly appreciated. I've attached all of my code below.
if true
%
%%Part (A) -- Graph of x(t)
t=[-655:0.1:655];
x = @(t) 9.*(-6<=t & t<0) + (12-8.*t).*(0<=t & t<3)+ 0.*(t<-6 * t>=3);
figure()
plot(t,x(t))
axis([-10 10 -20, 20])
grid on;
xlabel('t')
ylabel('x(t)')
title('Homework 4 - Question 1')
%%Part(A) -- Graph of y(t) (before Sumation)
k=[-655:655];
[p q] = meshgrid(t,k.*11);
y = (-1).^(k+1)*x(p+q);
figure()
plot(t,y)
axis([-20 20 -20 20])
grid on;
xlabel('t')
ylabel('y(t)')
title('Roughly 2 Periods of y(t) before Summation')
figure()
plot(t,cumsum(y))
axis([0 44 -200 1000])
grid on;
xlabel('t')
ylabel('y(t)')
title('Graph of y(t) with Summation')
%%Part (C)
xa = @(t) abs(x(t)).^2;
Energyx = integral(xa,-Inf,Inf)
ya = @(t) cumsum(y);
yaa = @(t) abs(ya(t)).^2;
powery = integral(yaa,5,27)
end

Answers (1)

John D'Errico
John D'Errico on 25 Jan 2015
Edited: John D'Errico on 25 Jan 2015
Well, for starters, I took a VERY quick look at your code. There I found these two lines:
t=[-655:0.1:655];
x = @(t) 9.*(-6<=t & t<0) + (12-8.*t).*(0<=t & t<3)+ 0.*(t<-6 * t>=3);
for vector t, the expression x(t) will fail to evaluate properly. Oh, it will evaluate. Just not evaluate properly. The issue is here:
(t<-6 * t>=3)
See that this is NOT the same as
((t<-6) .* (t>=3))
Why does it fail? Because 6*t is higher in the order of operations than a multiply. So matlab actually parses what you wrote as...
((t<(-6 * t)) >=3)
So it first compares t with -6*t. Is that true or false? A test returns either 0 or 1, where false is zero, true is 1. When will 0 or 1 EVER be greater than 3? Last time I checked, that never happens.
If you wish to understand what happened, try this at the command line:
10 > 3*5
ans =
0
MATLAT parses this expression as:
10 > (3*5)
It does the multiply first, THEN it does the comparison. So if I change it to:
10 > 3*2
ans =
1
this returns true.
In fact, this is not your problem, since no matter what result comes from the test you wrote, you multiplied by 0. The test was irrelevant, since whatever you got out then got dumped in the bit bucket by multiplying by zero. And since your piecewise function is apparently zero outside of that interval, you got lucky, and the proper result came out, despite the error.
Since I'm not terribly sure what your code is supposed to do, I really don't know if it is doing it properly past that point, although I saw a couple of things that had me mildly worried. In general, with such problems, test EVERY line of code. Verify that your code did exactly what it should have done, what you expected it to do.
  2 Comments
Christopher Feigal
Christopher Feigal on 25 Jan 2015
Edited: Christopher Feigal on 25 Jan 2015
Thanks for taking the time to comment. I totally understand that should've been an & sign but missed it because I got lucky and my graph did in fact still plot exactly as it was supposed to. I'm trying to measure the power over a period of the function, by integrating the abs(cumsum(y))^2 from 5 to 27. but keep getting those error messages and I can't figure out why.
Also, I just started using matlab two days ago so if there is other things that have you worried I'm really not shocked, everyone has to begin somewhere.
John D'Errico
John D'Errico on 25 Jan 2015
I think your main problem stems from the fact that you are trying to compute the integral of an array, not a function.
Why you used -inf and inf as limits I don't know, but that is in general a bad idea. MATLAB will not know that your function is zero over the entire domain, except for a very tiny part.
You might find trapz or cumtrapz of value.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!