Undefined function or method 'piecewise' for input arguments of type 'sym'. Error
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 12 Sep. 2011
1 Stimme
"piecewise" is not exposed from the Symbolic Toolbox to MATLAB. It has to be constructed inside the symbolic engine.
4 Kommentare
Walter Roberson
am 12 Sep. 2011
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>')
Carl
am 13 Sep. 2011
Walter Roberson
am 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)
Carl
am 13 Sep. 2011
Wayne King
am 12 Sep. 2011
0 Stimmen
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 Kommentar
Carl
am 12 Sep. 2011
Kategorien
Mehr zu Utilities for the Solver finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!