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])
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
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
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
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
2023년 9월 20일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

