Undefined function or method 'piecewise' for input arguments of type 'sym'. Error

I am attempting to write a program to compute partial sums of a trigonometric Fourier series of a piecewise function but I am running into problems actually defining the piecewise function itself. The code when simplified will compute the coefficients of the series and graph the function and the partial sums of any simple function but will not for the piecewise I am attempting at. I am admittedly only about 50% sure on the code for the piecewise in general since I am new to MATLAB itself but I cannot get past this error start actually debugging any other problems.
syms t k L n
evalin(symengine,'assume(k,Type::Integer)');
a = @(f,t,k,L) int(f*cos(k*pi*t/L)/L,t,-L,L);
b = @(f,t,k,L) int(f*sin(k*pi*t/L)/L,t,-L,L);
fs = @(f,t,n,L) a(f,t,0,L)/2 + ...
symsum(a(f,t,k,L)*cos(k*pi*t/L) + b(f,t,k,L)*sin(k*pi*t/L),k,1,n);
f = piecewise(t);
% first range
x1 = t(0 <= t & t < 2);
f(0 <= t & t < 2) = sin((pi*x1^2)/4);
% second range
x2 = t(2 <= t & t < 3);
f(2 <= t & t < 3) = 5*x2-x2.^2-6;
% third range
x3 = t(3 <= t & t < 4);
f(3 <= t & t < 4) = 0;
% fourth range
x4 = t(4 < t & t < 0);
f(4 < t & t < 0) = t - 4;
pretty(fs(f,t,25,1))
ezplot(fs(f,t,25,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=25')
Any assistance on this problem would be greatly appreciated.

 Réponse acceptée

Variant of the numerical solution - FourierSeriesForCarl.m
function fs = FourierSeriesForCarl(t,k,L)
function x = piesewiseforCarl(t)
x = zeros(size(t));
t1 = t < 0 | t >= 4;
x(t1) = t(t1) - 4;
t2 = 0 <= t & t < 2;
x(t2) = sin((pi*t(t2).^2)/4);
t3 = 2 <= t & t < 3;
tt3 = t(t3);
x(t3) = 5*tt3-tt3.^2-6;
t4 = 3 <= t & t < 4;
x(t4) = 0;
end
k = k(:);
t = t(:).';
fs = sum(bsxfun(@times,arrayfun(@(k)quad(@(t1)piesewiseforCarl(t1).*...
cos(k*pi*t1/L)/L,-L,L),k),cos(k*pi*t/L)) +...
bsxfun(@times,arrayfun(@(k)quad(@(t1)piesewiseforCarl(t1).*...
sin(k*pi*t1/L)/L,-L,L),k),sin(k*pi*t/L)));
end
ADD 13.09.2011 about 10:40 MSK
about your function f with use MuPAD
f = @(x)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),' ',','),')']);
if x is the double array

5 commentaires

Using feval(symengine, 'subs', sym('piecewise(....)'), t, x)
would probably make things a bit easier.
I am, though, not convinced at the moment that this is necessary or desirable to substitute numeric values.
@(x)evalin did clear up a lot of issues but the implicit integral error still remains.
Please post your current code version.
syms t k L 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,L) int(f*cos(k*pi*t/L)/L,t,-L,L);
b = @(f,t,k,L) int(f*sin(k*pi*t/L)/L,t,-L,L);
fs = @(f,t,n,L) a(f,t,0,L)/2 + ...
symsum(a(f,t,k,L)*cos(k*pi*t/L) + b(f,t,k,L)*sin(k*pi*t/L),k,1,n);
f = fs;
pretty(fs(f,t,25,1))
ezplot(fs(f,t,25,1),-1,1)
hold on
ezplot(f,-1,1)
hold off
title('Partial sum with n=25
Was able to break the problem down and simplify the main equation at least a little bit. No longer getting the cannot find implicit integral error thanks to a professor pointing out something very obvious.
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)/2 + ...
symsum(a(f,t,k)*cos(k*pi*t/4) + b(f,t,k)*sin(k*pi*t/4),k,1,n);
f = fs;
pretty(fs(f,t,25,1))
ezplot(fs(f,t,25,1),-2,8)
hold on
ezplot(f,-2,8)
hold off
title('Partial sum with n=25')

Connectez-vous pour commenter.

Plus de réponses (2)

"piecewise" is not exposed from the Symbolic Toolbox to MATLAB. It has to be constructed inside the symbolic engine.

4 commentaires

Notice that the documentation for piecewise() as at http://www.mathworks.com/help/toolbox/mupad/stdlib/piecewis.html
The "stdlib" is a strong clue that this is a function defined in MuPad but not exported to MATLAB.
You are also using quite the wrong way to define a symbolic piecewise function. piecewise() requires that all of the conditions be passed to it at the beginning. You cannot simply declare a variable to be piecewise and then assert logical constraints. In MuPad:
f := t -> piecewise([0 <= t and t < 2, sin((Pi*t^2)/4)], [t <= 2 and t < 3, 5*t-t^2-6], [t <=3 & t < 4, 0], [Otherwise, t-4]);
Note: your last condition was impossible, requiring t to be simultaneously less than 0 and greater than 4, so I have interpreted it here as "or" instead of "and", which also takes care of the case of t < 0.
Reminder note: The above is in MuPad! If you want to return the resulting object to the MATLAB level, you would need to evalin(symengine,'f := <etc>')
Ok so using the MuPad with the piecewise function I could completely skip having to include the second file which you pointed out does nothing but send MATLAB into a loop. The only problem I'm still having with the evalin(symengine,'f := <>') is I can't seem to get f defined for the following integration.
Sorry if I coming off as sluggish and well moronic but I was informed that I had to learn this program only about two or so weeks ago so forgive the stupid mistakes I do really appreciate the help.
Unfortunately, I do not have the symbolic toolbox, so I am unable to test to see what kind of object is returned from that evalin() call.
What does it return for you? If it returns a symbolic object, are you still trying to multiply that symbolic object (which would be a function) by something else, instead of multiplying the function *called at a given argument* ?
Not
int(f*cos(k*pi*t/L)/L,t,-L,L)
but
int(f(t)*cos(k*pi*t/L)/L,t,-L,L)
Evalin() returns the described funtion as a awnser in the command window but diretly after that the program crashes with errors like MuPAD Illegal integrand int. I will check if this is caused by how I am then calling f in the program. Again thank you.

Connectez-vous pour commenter.

Hi Carl, Have you defined piecewise() as a function and saved your piecewise.m file in a folder that you add to the MATLAB path?
The error you report looks like MATLAB does not know about this function.
Wayne

1 commentaire

I have tried to fix this problem but no matter what I do one part of the program refuses to see or find the other part of the program. I continue to get errors either stating that the path could not be found or that a variable was not defined in the function portion of the program.
Thank you for the help though I am at least seeing new errors.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by