Multivariate Functions with Conditions in Mathematica

雨燕双飞 提交于 2019-12-11 16:52:19

问题


I'm trying to define a bivariate function that takes values depending on whether a condition is met. I make them work for univariate case but I'm stuck with the bivariate case:

g[x_, y_] := 10 /; x < 10
g[x_, y_] := 20 /; (x >= 10 && y < 5)
g[x_, y_] := -5 /; (x >= 10 && y >= 5);

This function never gives me the value of -5.

g[12,10] = 20?


回答1:


This works for me:

Clear[g]
g[x_, y_] /; x < 10 := 10
g[x_, y_] /; x >= 10 \[And] y < 5 := 20
g[x_, y_] /; x >= 10 \[And] y >= 5 := -5

then

In[73]:= g[12, 10]

Out[73]= -5

Why this version works and your version doesn't I'm not sure. Perhaps someone else will come along and tell us



来源:https://stackoverflow.com/questions/14572075/multivariate-functions-with-conditions-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!