How to program a function with 2 variables
조회 수: 7 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2021년 2월 19일
Duplicated by later https://www.mathworks.com/matlabcentral/answers/747757-functions-with-2-variables?s_tid=srchtitle which has solutions.
답변 (1개)
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;
end
end
end
Hope this helps!
Thanks
댓글 수: 1
Walter Roberson
2021년 2월 19일
The equations are not really functions of two variables. I posted the single-variable solution at https://www.mathworks.com/matlabcentral/answers/747757-functions-with-2-variables#answer_625627
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!