Plot surface from non-anonymus function

2 ビュー (過去 30 日間)
Santiago Ortiz Laverde
Santiago Ortiz Laverde 2021 年 2 月 27 日
コメント済み: Star Strider 2021 年 2 月 27 日
Hello guys,
I'd like to plot a surface from a global function. I don't know how to create the function, so it is applied to every value in a multidimensional array (mesh grid). There plenty of examples using anonymous functions, but I haven't found any from a non-anonymous one (which I call "global").
Take this function as an example
function z = test(w)
x = w(1);
y = w(2);
z = 2*x.^2 + 2*y.^2 +10*(1.1*y.^2 +3*x.^2).^0.5;
end
Is it possible?
Regards,
Santiago

採用された回答

Star Strider
Star Strider 2021 年 2 月 27 日
See if this does what you want —
function z = test(w)
x = w{1};
y = w{2};
z = 2*x.^2 + 2*y.^2 +10*(1.1*y.^2 +3*x.^2).^0.5;
end
x = -10:10;
y = -15:5;
[W{1},W{2}] = ndgrid(x,y);
Z = test(W);
figure
surf(W{1},W{2},Z)
grid on
Note that I created ‘w’ as cell arrays to pass its elements as matrices. Other than that change, your function is as you wrote it.
  2 件のコメント
Santiago Ortiz Laverde
Santiago Ortiz Laverde 2021 年 2 月 27 日
Thank you!, It worked. I just didn't know about 'cell arrays'.
Star Strider
Star Strider 2021 年 2 月 27 日
As always, my pleasure!
They’re quite useful!

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

その他の回答 (1 件)

Hernia Baby
Hernia Baby 2021 年 2 月 27 日
編集済み: Hernia Baby 2021 年 2 月 27 日
clc,clear,close all;
w = [1:10;2:11];
x = w(1,:);
y = w(2,:);
f = @(x,y) 2*x.^2 + 2*y.^2 +10*(1.1*y.^2 +3*x.^2).^0.5;
[X,Y] = meshgrid(x,y);
z = f(X,Y);
figure
surf(X,Y,z)
grid on

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by