필터 지우기
필터 지우기

specify domain of a function

조회 수: 29 (최근 30일)
Thomas Ligon
Thomas Ligon 2017년 5월 24일
이동: Dyuman Joshi 2024년 1월 9일
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에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by