How to program a function with 2 variables

1 ビュー (過去 30 日間)
Jens Petit-jean
Jens Petit-jean 2021 年 2 月 16 日
コメント済み: Walter Roberson 2021 年 2 月 19 日
Hello, I need to program this as a function (not as an anonymous, but as a function that you can use multiple times in other programs). I can do this when there is only one variables but I really can't do it with 2 variables. Can someone help me?
Thanks in advance

回答 (1 件)

Pranav Verma
Pranav Verma 2021 年 2 月 19 日
Hi Jens,
You can write a simple function taking x and k as input and put the conditions as mentioned:
function val = func(x,k)
val = 1;
if(isinteger(int64(k) ))
if (4*k <= x && x < 4*k + 2)
val = 4 - (2 * (x - (4 * k) - 1)^2);
elseif (isinteger(k) && 4*k-2 <= x && x < 4*k)
val = 2 * (x - (4*k) + 1)^2;
Hope this helps!
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 19 日
The equations are not really functions of two variables. I posted the single-variable solution at



Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by