How do I define a piecewise function using special conditions?

15 ビュー (過去 30 日間)
Humb
Humb 2017 年 5 月 14 日
編集済み: Andrei Bobrov 2017 年 5 月 14 日
Hello, thank you for helping me. I am using a script to calculate the dft of a function entered simbolically in terms of the variable "n"using the following:
syms n;
f = input('Enter the function\n');
Everything works fine so far, my question is whether I can enter the following piecewise function with special conditions as an argument to the script and how:
f[n]= 3 for n even.
f[n]= -2 for n odd.
Thank you again.
  1 件のコメント
John D'Errico
John D'Errico 2017 年 5 月 14 日
Why do you need symbolic here?
And how is a function that is defined ONLY on the integers a piecewise function?
Why are you trying to input a function using input? Time for you to learn how to define a function.
Finally, learn how to define an index, or for that matter, the arguments to a function. f[n] is NOT valid syntax in MATLAB.
Clearly your most important project is to read the basic getting started tutorials in MATLAB.

サインインしてコメントする。

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 5 月 14 日
編集済み: Andrei Bobrov 2017 年 5 月 14 日
f = input('Enter the function: ','s') % enter expression: 3-5*rem(n,2)
f = str2func(['@(n)',f])
use f:
>> f(1:10)
ans =
-2 3 -2 3 -2 3 -2 3 -2 3

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by