How to resolve, "Nonscalar arrays of function handles are not allowed; use cell arrays instead."

조회 수: 6 (최근 30일)
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!

답변 (2개)

Guillaume
Guillaume 2015년 12월 1일
편집: Guillaume 2015년 12월 1일
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
Cody Stein
Cody Stein 2015년 12월 1일
편집: Cody Stein 2015년 12월 1일
I'm having trouble understanding exactly what you did. Also when I plot it, it doesn't seem to plot the correct plot, it should look something like this screen shot, however it is all wonky when I tried that code on my computer. Do you know why? Thank you!
Guillaume
Guillaume 2015년 12월 2일
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
Walter Roberson 2015년 12월 1일
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
Cody Stein
Cody Stein 2015년 12월 1일
편집: Cody Stein 2015년 12월 1일
How would I go about plotting this against t?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by