Plotting Anonymous Function of two variables

Hello,
I have defined the following Anonymous Function of two variables:
f1 = @(x) erf(x(1))+cos(x(2));
When writing the command for plotting via fsurf
fsurf(f1,[-5 0 -5 5])
Error using surf
Z must be a matrix, not a scalar or vector.
I get the following warning and my figure remains empty:
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize
your function to return an output with the same size and shape as the input arguments.
So, what am I doing wrong?
I know that I can plot the function with
f1 = @(x,y) erf(x)+cos(y);
fsurf(f1,[-5 0 -5 5])
but I want to plot it like at the beginning.
How can I plot a Anonymous Function that is defined as mentioned at the beginning?
Regards,
Michael

5 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 19 日
"I have defined the following Anonymous Function of two variables:"
No, that is an anonymous function of a single variable i.e. x.
From the documentation of fsurf - "Specify a function of the form z = f(x,y). The function must accept two matrix input arguments and return a matrix output argument of the same size."
"How can I plot a Anonymous Function that is defined as mentioned at the beginning?"
Any particular reason why you want to use that approach?
Michael Haag
Michael Haag 2023 年 9 月 19 日
Thank you for the hint to the doc of fsurf.
Yes, there is a particular reason. When doing some optimization with i.e. fmincon, the objective function has to be defined as mentioned and if I want to plot that function I cannot do this easily with fsurf.
What is your recommendation to plot the function defined by
f1 = @(x) erf(x(1))+cos(x(2));
?
Matt J
Matt J 2023 年 9 月 19 日
編集済み: Matt J 2023 年 9 月 19 日
When doing some optimization with i.e. fmincon
For situations like that, I would do,
s = @(x1,x2) erf(x1)+cos(x2); %use this for surface plotting
objective = @(x) s(x(1), x(2)); %use this for fmincon
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 20 日
"the objective function has to be defined as mentioned"
I am bit skeptical about "has to".
Can you give more details about what you are trying to? Can you share the objective function?
Michael Haag
Michael Haag 2023 年 9 月 20 日
s = @(x1,x2) erf(x1)+cos(x2); %use this for surface plotting
objective = @(x) s(x(1), x(2)); %use this for fmincon
This is exactly what I need! Thanks a lot.
@Dyuman Joshi: I just want to minimize a simple function. My intention was to figure out how to define the function thta I am able to minimize it with fmincon and to plot it afterwards.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2023 年 9 月 19 日

0 投票

f1 = @(x) erf(x(1))+cos(x(2));
fsurf(@(x1,x2) f1([x1,x2]) , [-5 0 -5 5])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.

2 件のコメント

Michael Haag
Michael Haag 2023 年 9 月 19 日
編集済み: Michael Haag 2023 年 9 月 19 日
Thank's a lot. But the warning is still issued. Is there a nicer way to do the plotting with the function defined as mentioned?
Matt J
Matt J 2023 年 9 月 19 日
編集済み: Matt J 2023 年 9 月 19 日
You're welcome, but please Accept-click the answer.
No, you cannot avoid the warning with the input syntax you've chosen, except with warning off.
f1 = @(x) erf(x(1))+cos(x(2));
warning off
fsurf(@(x1,x2) f1([x1,x2]) , [-5 0 -5 5])
warning on

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020b

質問済み:

2023 年 9 月 19 日

コメント済み:

2023 年 9 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by