specify domain of a function

47 次查看(过去 30 天)
When I define a function, how do I specify the domain, i.e. the values for which it is defined?
I am examining the behavior of a function that I can only express numerically. In other words, if the function is z=fun(x,y), then I can find the value of z for any x and y, but I cannot express it in closed form, i.e. in terms of algebraic or trigonometric expressions.
Important: x and y must be between -1 and 1, so fun is a mapping from [-1,1]x[-1,1]->R, where square brackets indicate the closed interval.
One thing I did was to define a grid of x and y values and create an NxN matrix of z values. In addition, I have vectors for x and y that map the indices of the matrix to the values between -1 and 1. Then, I can find the minimum, maximum, and gradient. I don't know of any way to tell the built-in MATLAB function gradient what the domain is, but it seems to be doing OK.
The I tried to use hessian from the derivest package (Adaptive Robust Numerical Differentiation) derivest The problem is that derivest/hessian does not know where the boundary of the domain is, so when it calculates a derivative, in goes out of bounds of the matrix (function).
At the moment, my problem occurs when I use derives to calculate the hessian numerically, but this appears to me like a very general question in MATLAB: How to specify the domain of a function.

采纳的回答

John D'Errico
John D'Errico 2017-5-24
Functions are assumed to live on the entire real line. There is no explicit facility to say that f(x) (or f(x,y)) only lives on a finite domain.
If you write code for a function, that code can have a test in it, to error out perhaps if the domain is exceeded, or you might choose to do anything else that seems appropriate to you. Perhaps issue a warning, an error, or truncate any points to the interval bounds. Nothing stops you from doing so. In fact, it is good programming practice to use error checks in your code. For example, suppose your function works only on real numbers, but a complex number is passed in, or inf, or NaN, or [].
But if you use other code, passing it a function, unless that code is enabled to deal with a finite interval, then no, you cannot make it magically know how to handle functions that live only on a finite domain. Sorry, but the derivest tools are not so equipped. That would have made things considerably more complicated for me to have written.
  2 个评论
Thomas Ligon
Thomas Ligon 2017-5-25
移动:Dyuman Joshi 2024-1-9
Thanks John, that's good information. I actually did create such a check, and return [rc,z], where rc is a return code, but I didn't expect functions written for other purposes to take account of rc, or even z=NaN.
Thomas Ligon
Thomas Ligon 2017-5-25
移动:Dyuman Joshi 2024-1-9
One more little thing. When I call the built-in MATLAB function gradient, I give it the matrix form of the function, which is why it knows where the limits are. Of course, that solution is limited by creating a limited-resolution grid and using that to calculate the gradient.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by