Undefined function or method 'piecewise' for input arguments of type 'sym'. Error
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Andrei Bobrov
on 12 Sep 2011
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 Comments
More Answers (2)
Walter Roberson
on 12 Sep 2011
"piecewise" is not exposed from the Symbolic Toolbox to MATLAB. It has to be constructed inside the symbolic engine.
4 Comments
Walter Roberson
on 13 Sep 2011
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)
Wayne King
on 12 Sep 2011
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!