Accessing sinc(0) using the Symbolic Math Toolbox.

I have this code-
syms t;
sinc_function = symfun(sinc(t),t);
Now, sinc_function(0) gives an error message 'Division by zero' but the command sinc(0) gives me the correct value 1. I want to get the correct value from the sinc_function. How can I do this? It can be rectified by piecewise function, but I have the MATLAB 2015a version, so the piecewise function is not present there.

 채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 26일

1 개 추천

The Symbolic Toolbox does not define a sinc() function. You are getting the purely numeric sinc() function from the signal processing toolbox. That function works by testing the input for 0 using logical indexing. Unfortunately when you pass in a symbolic variable, that is not equal to 0 (because it is a symbol, not 0), so no special handling is arranged for that case.
You will have to program around this.
You would not be able to use piecewise() even if you had R2016b, because symfun() cannot generate code for piecewise().
You will need to reconsider using sinc() in a symbolic function. For example, define
syms SINC(t)
and code in terms of SINC(t) without having defined SINC(t) anywhere. Call matlabFunction. You will get a warning about unknown function SINC, but it will be written into the handle. Then you bring SINC.m onto your path, defined as
function y = SINC(x)
y = sinc(x);
Now that is done, you can run the anonymous function you generated.
If your had SINC.m on the path at the time you did the matlabFunction, it would have called the function with the symbolic expression as parameter, leading to sin(t)/t being generated right in the code. But with SINC.m not on the path at the time of the matlabFunction, it just generates the name in the function handle, and then when you put it on the path and run the function handle it will find the SINC.m

댓글 수: 4

abdelhak
abdelhak 2024년 11월 21일
이동: DGM 2024년 11월 21일
'sinc' requires one of the following:
Signal Processing Toolbox
Symbolic Math Toolbox
DGM
DGM 2024년 11월 21일
sinc() was added to the symbolic toolbox in R2018b
sinc = @(x) sin(x ./ (x + (x == 0))) + (x == 0);
Signal Processing and Symbolic Math Toolboxes implement the normalized sinc function, so (also, the parentheses needed to be fixed)
nsinc = @(x) sin(pi*x) ./ (pi*x + (x == 0)) + (x == 0);
x = 0.4;
[nsinc(x),sinc(x),double(sinc(sym(x)))]
ans = 1×3
0.7568 0.7568 0.7568
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Signal Processing Toolbox에 대해 자세히 알아보기

태그

질문:

2017년 1월 26일

댓글:

2024년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by