How to resolve, "Nonscalar arrays of function handles are not allowed; use cell arrays instead."
3 views (last 30 days)
Show older comments
Hey everyone,
I know this question is asked a lot but I'm still having troubles trying to resolve the issue. I have a working code for this, so the homework problem is done. However, I have a pet peeve where I always try to make things look as nice as possible. The original problem was to expand a function into a Fourier Series and plot it. I was able to do that manually by computing 21 different integrals (-10 to 10) and then adding them all up.
However since I wanted it to look pretty, I wanted to try to do it in a for loop since that seemed like the best option in order to have full control over how many coefficients I want to use to show how accuracy increases or decreases as the number of coefficients increases or decreases respectively.
This is my current code:
%Methods For Loop
close all
C=1;
V=5;
R=4;
t=linspace(0,20,22);
%-------------------------------------------------------------------------%
%Original Function - (V*C*(1-exp(-t/((R*C)))))
%Integrand - (V*C*(1-exp(-t/((R*C)))))*(exp((-1i*4*n*pi*t)/(R*C))))
for n=1:21
Coeff=@(t) (V.*C.*(1-exp(-t/((R.*C))))).*(exp((-1i.*4.*(n-11).*pi.*t)/(R.*C)));
I(n)= (2/(R*C))*integral(Coeff,0,2);
E(n)=@(t) exp((4*1i*(n-11)*pi*t)/(R*C));
end
CN=I*E
end
I cannot for the life of me seem to resolve this issue. I would greatly appreciate if anyone could offer any insight!
0 Comments
Answers (2)
Guillaume
on 1 Dec 2015
Edited: Guillaume
on 1 Dec 2015
An alternative to Walter's solution that avoids the TCN recursion:
C=1;
V=5;
R=4;
t=linspace(0,20,22);
for n = 1:21
Coeff=@(t) (V.*C.*(1-exp(-t/((R.*C))))).*(exp((-1i.*4.*(n-11).*pi.*t)/(R.*C)));
I(n) = (2/(R*C))*integral(Coeff,0,2);
E{n} = @(t) exp((4*1i*(n-11)*pi*t)/(R*C));
CN{n} = @(t) I(n) * E{n}(t);
end
%sum of the first n CN{i}:
TCN = @(n, t) sum(cell2mat(arrayfun(@(i) CN{i}(t), (1:n)', 'UniformOutput', false)));
%obviously, the maximum value for n is 21, since you calculated 21 CN
%at this point you can plot individual CN{i}
%e.g, to plot the first n CN:
n = 5; %for example
figure; hold on;
arrayfun(@(i) plot(t, CN{i}(t), 'DisplayName', sprintf('CN{%d}', i)), 1:n);
%and the sum of the first n CN:
plot(t, TCN(n, t), 'DisplayName', 'TCN')
legend('show');
2 Comments
Guillaume
on 2 Dec 2015
I and E are exactly the same as your original code, except E is a cell array since, as you found out, you can't put function handles in a plain array. CN is just a cell array of function handles, each the respective product of I and E.
If the CN do not plot properly, then the problem is with your original formula.
TCN is a function handle that:
- concatenate vertically the first n CN(t) vectors.
- sum the columns of the vectors
SO TCN(n, t) is basically the sum of the first n CN applied to the t vector.
My guess is that there's something wrong in the Coeff, I or E formula.
Walter Roberson
on 1 Dec 2015
in the loop
E{n} = @(t) exp((4*1i*(n-11)*pi*t)/(R*C));
CN{n} = @(t) I(n) * E{n}(t);
and remove the CN = I*E after the loop.
I do not know why you are wanting to construct E as function handles. When you do, your CN has to be function handles too as you are multiplying I (numeric) by E (a function handle)
And I do not see any summation. I also do not see any place that you apply your function handles to the actual t values you have defined.
4 Comments
See Also
Categories
Find more on Digital Filter Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!