I created an anonymous function handle like this:
f = @(x,y)sqr(x)+sqr(y)
This is a sphere with the two variables x
and y
. It seems to work since I can call something like
f(2,3)
and MATLAB gives me the right result ans = 13
.
In the last step I want to plot that function from let's say -7 to 7 for both x
and y
. So I call
fmesh(f,[-7 7])
and the right figure pops up. So far, so good.
For some reasons, which I don't wanna specify here, I now want to edit the function handle to this:
f = @(x)sqr(x(1))+sqr(x(2))
This should be the same sphere but this time with the 2 variables x(1)
and x(2)
.
Since the function now wants an array as argument I edited the test call
f([2,3])
and it still gives me the right result ans = 13
.
But here is the problem: How do you plot the function that wants an array as argument?
The same mesh command as before of course fails, since [-7,7]
has the wrong dimension. The same goes for [[-7 7] [-7 7]]
and [[-7 7];[-7 7]]
.
How can I get a working plot from that new function handle? Thanks in advance!
What are lambda functions in Python? In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
An anonymous function is a function without a name, it is executed from where it is defined. Alternatively, you can define the debugging function before using it.
An anonymous function in javascript is not accessible after its initial creation. Therefore, we need to assign it to a variable, so that we can use its value later. They are always invoked (called) using the variable name. Also, we create anonymous functions in JavaScript, where we want to use functions as values.
You can't get fmesh
to pass the x
and y
values as an array like what you expect. What you can do is to wrap your anonymous function f
within another anonymous function which simply re-arranges the input.
g = @(x,y)f([x, y]);
fmesh(g, [-7 7])
A more generalized solution which takes all inputs and puts them into an array would be
g = @(varargin)f([varargin{:}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With