How can I vectorize a function?
155 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
David Franco
am 1 Mär. 2018
Kommentiert: Star Strider
am 24 Mär. 2018
The function I want to vectorize is Cross-in-Tray Function (2-D):
f(X,Y) = -0.0001*(abs(sin(X)*sin(Y)*exp(abs(100-sqrt(X^2+Y^2)/pi)))+1)^0.1;
I want to use the command (to plot the function):
fsurf(@(x,y) crossintrayfcn([x,y]))
I have this two function codes:
function z = crossintrayfcn(xx)
x = xx(:,1);
y = xx(:,2);
expcomponent = abs(100-(sqrt(x.^2 + y.^2)/pi));
z = -0.0001*((abs(sin(x).*sin(y).*exp(expcomponent))+1).^0.1);
end
And:
function [y] = crossintrayfcn(xx)
x1 = xx(1);
x2 = xx(2);
fact1 = sin(x1)*sin(x2);
fact2 = exp(abs(100 - sqrt(x1^2 + x2^2)/pi));
y = -0.0001 * (abs(fact1*fact2) + 1)^0.1;
end
But they plot nothing!
Thanks!
0 Kommentare
Akzeptierte Antwort
Star Strider
am 1 Mär. 2018
Vectorising it simply requires using element-wise operations:
f = @(X,Y) -0.0001*(abs(sin(X).*sin(Y).*exp(abs(100-sqrt(X.^2+Y.^2)/pi)))+1).^0.1;
[x,y] = meshgrid(linspace(-10, 10, 49));
figure
surfc(x, y, f(x,y))
grid on
9 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!