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.
채택된 답변
추가 답변 (2개)
Walter Roberson
2011년 9월 12일
1 개 추천
"piecewise" is not exposed from the Symbolic Toolbox to MATLAB. It has to be constructed inside the symbolic engine.
댓글 수: 4
Walter Roberson
2011년 9월 12일
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
2011년 9월 13일
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
2011년 9월 13일
Wayne King
2011년 9월 12일
0 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Utilities for the Solver에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!