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

14 次查看(过去 30 天)
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.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-9-12
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 个评论
Carl
Carl 2011-9-13
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
Carl
Carl 2011-9-13
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')

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2011-9-12
"piecewise" is not exposed from the Symbolic Toolbox to MATLAB. It has to be constructed inside the symbolic engine.
  4 个评论
Walter Roberson
Walter Roberson 2011-9-13
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)
Carl
Carl 2011-9-13
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.

请先登录,再进行评论。


Wayne King
Wayne King 2011-9-12
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 个评论
Carl
Carl 2011-9-12
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by