2
votes

So far from what I have read this error can be caused by confusing or redundant naming within the program but I don't think that is the issue here since everything is declared clearly. From what I can see in this my issue is coming from the declaration of piecewise that is then being run through integration below and therefor the program is attempting to access a array cell that doesn't exist. If this is the case I have so far been stumped at how to fix this issue. Any assistance with this problem would be greatly appreciated.

syms t k n

fct = @(t)evalin(symengine,['subs(piecewise([0 <= t and t < 2,',...
'sin((Pi*t^2)/4)],[t <= 2 and t < 3, 5*t-t^2-6], [t <=3 and t < 4, 0],',...
                '[Otherwise, t-4]),t=',regexprep(mat2str(x),' ',','),')']);

evalin(symengine,'assume(k,Type::Integer)');

a = @(fct,t,k) int(fct*cos(k*pi*t/4)/4,t,-2,8);
b = @(fct,t,k) int(fct*sin(k*pi*t/4)/4,t,-2,8);

FourierSeries = @(fct,t,n) a(fct,t,0)/4 + ...
symsum(a(fct,t,k)*cos(k*pi*t/4) + b(fct,t,k)*sin(k*pi*t/4),k,1,n);

pretty(FourierSeries(t,25,1))



ezplot(FourierSeries(t,25,1),-2,8)
hold on
ezplot(fct,-2,8)
hold off
title('Partial sum with n=25')

The complete error text is as follows:

??? Attempt to reference field of non-structure array.

Error in ==> sym.int at 56 r = mupadmex('symobj::intdef',f.s,x.s,a.s,b.s);

Error in ==> @(fct,t,k)int(fct*cos(k*pi*t/4)/4,t,-2,8)

Error in ==> @(fct,t,n)a(fct,t,0)/4+symsum(a(fct,t,k)*cos(k*pi*t/4)+b(fct,t,k)*sin(k*pi*t/4),k,1,n)

Error in ==> FourierProgram at 16 pretty(FourierSeries(t,25,1))

1
When it says ??? Attempt to reference field of non-structure array. is this simply a data type error? If so could the original 'fct' in my program be easily converted over to a structure array or actually defined as one in the first place using something like cell2struct? - Carl
Please see existing questions with this error message and try the solutions there. If nothing works, please update your question with that info :) - abcd

1 Answers

0
votes

This was asked long ago, but I'll provide an answer since one was never given.

As your error indicates, the issue is with this line and how the anonymous function a is called:

a = @(fct,t,k) int(fct*cos(k*pi*t/4)/4,t,-2,8);

The sym/int function expects the second argument (the variable with which the integration is performed with respect to) to be a symbolic variable. However, you're calling FourierSeries(t,25,1), which passes the value 25 as the integration variable.

This bit of code should replicate the issue in your version of Matlab (back in 2011 when this was asked):

 syms t k;
 int(t*cos(k*pi*t/4)/4,25,-2,8)

However, in R2015a I now get a different (and a bit clearer) error message:

Cannot integrate with respect to ''. The integration variable must be a symbolic variable.