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

6 views (last 30 days)
I'm almost at a lost for word on what to ask at this point. For every problem fixed another takes its place. This error is coming from the mupad used to define the piecewise function at the beginning of the program. So far from what I read MATLAB doesn't like the fact that I am using evalin() and to counter this I can define the variable in question f as some related data class as a possible way to fix the problem but I have so far had no luck.
SCRIPT
syms t k n
evalin(symengine,'assume(k,Type::Integer)');
f = @(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),' ',','),')']);
a = @(f,t,k) int(f*cos(k*pi*t/4)/4,t,-2,8);
b = @(f,t,k) int(f*sin(k*pi*t/4)/4,t,-2,8);
fs = @(f,t,n) a(f,t,0)/4 + ...
symsum(a(f,t,k)*cos(k*pi*t/4) + b(f,t,k)*sin(k*pi*t/4),k,1,n);
pretty(fs(t,25,1))
ezplot(fs(t,25,1),-2,8)
hold on
ezplot(f,-2,8)
hold off
title('Partial sum with n=25')
ERROR TEXT
??? 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 ==> @(f,t,k)int(f*cos(k*pi*t/4)/4,t,-2,8)
Error in ==> @(f,t,n)a(f,t,0)/4+symsum(a(f,t,k)*cos(k*pi*t/4)+b(f,t,k)*sin(k*pi*t/4),k,1,n)
Error in ==> FS_M at 16
pretty(fs(t,25,1))

Answers (1)

Walter Roberson
Walter Roberson on 14 Sep 2011
You are passing 25 as the second argument to fs, where it becomes the dummy variable named "t". The first subexpression of fs is a(f,t,0) so that is going to be a(f,25,0) where f is the first argument that was passed in to fs. In a, the second argument gets received into the dummy variable named "t", and the 0 will get received in to the dummy variable named "k"; the item that was passed in to the first argument of fs becomes the dummy variable named "f" in a().
At that point you calculate int(f*cos(k*pi*t/4)/4,t,-2,8) so let us do the substitutions for the dummy variables t and k, values 25 and 0 respectively. int(f*cos(0*pi*25/4)/4,25,-2,8) so with minor simplification that becomes int(f*1/4,25,-2,8) where f is whatever value was passed as the first argument to fs.
Question: Is it legal to integrate f/4 over 25=-2 to 25=8 ?
Now, as to what exactly f is at this point such that f/4 is being integrated: the first argument you passed to fs() was t, where it become f, which was passed to a(), there to be named f, so f is, at this point, the symbolic variable t.
I don't know about you, but I tend to get confused about t being passed to a function that has a different dummy argument named t. Tends to get me confused about which t means what. Tends to lead me to mistakes like trying to integrate with respect to a variable that is not in fact the name of a symbolic variable.
So, if you wouldn't mind, as a favor to me as a reader of the code, could you rewrite to use different dummy variable names so that I do not get confused about a symbolic variable named t versus a numeric variable also named t ?
  3 Comments
Walter Roberson
Walter Roberson on 14 Sep 2011
That _is_ more readable, but you are still faced with the fact that fct would be a function handle, and it is not legal to multiply a function handle by a value. At the very least in a and b, inside the int(), you would need to use fct(t0)*something instead of fct*something .
I believe if you print out the value of fct and examine it, you will find that you have not defined t0 and you have not used t; you will also find that you have not defined L for it.
I recommend that you try manually calling
fct(-5:.5:5)
and seeing what you get out.
*I* would not use fct as an anonymous function and I would not use the subs() and mat2str() hack: I would just let fct be a symbolic variable. With the correction I pointed out about needing to call fct and not just multiply it, I think the rest would work out.
Walter Roberson
Walter Roberson on 14 Sep 2011
Amendment: I would let fct be a symbolic function,
fct = evalin(symengine, 't0 -> piecewise([....])')
And by "the rest" I meant the int() itself, provided the other parts were fixed. Which might include having to pass L to fct as well:
fct = evalin(symengine, '(t0,L) -> piecewise([....])')
and in the int() calls, using fct(t0,L)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!