meshgrid or ngrid?

9 次查看(过去 30 天)
ttopal
ttopal 2016-8-9
回答: KSSV 2016-8-9
Hi, I have lately started to use meshgrid and ngrid thanks to the answer to my previous question. But I can not find a way to transform this code to either of them. Here on this code I have one calculationPoint, which normally has to be a meshgrid of -20:20 on both X and Y direction. Any suggestion on how to proceed would be appreciated.
clc;
S = 21;
t = linspace(-pi,pi,S);
knownPoints = [cos(t);sin(t)]; %(x,y)
f = exp(1i*t);
d = zeros (1,S);
calculationPoint = [10;9];
for idx=1:S
tmp1 = calculationPoint(1,1)-knownPoints(1,idx);
tmp2 = calculationPoint(2,1)-knownPoints(2,idx);
d(idx) = sqrt(tmp1^2+tmp2^2);
end
g = d.^2 + 1./d.^3;
z = 1/S*sum(g.*f); % z function is of parameter calculation point

回答(1 个)

KSSV
KSSV 2016-8-9
Your d, g are arrays of same size as t....You can use mesh grid on d/g... Are you looking for some thing like below?
clc;
S = 21;
t = linspace(-pi,pi,S);
knownPoints = [cos(t);sin(t)]; %(x,y)
f = exp(1i*t);
d = zeros (1,S);
calculationPoint = [10;9];
for idx=1:S
tmp1 = calculationPoint(1,1)-knownPoints(1,idx);
tmp2 = calculationPoint(2,1)-knownPoints(2,idx);
d(idx) = sqrt(tmp1^2+tmp2^2);
end
g = d.^2 + 1./d.^3;
[T,G] = meshgrid(t,g) ;
z = 1/S*sum(g.*f); % z function is of parameter calculation point

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by