Writing variables as a matrix in the for loop

4 views (last 30 days)
Hello
I wrote a script which computes second moment of a function within a circular area. I chose the points within the circle using a loop, however I could not write the poins I chose to a matrix. I want to write the m(1,1) and m(2,1) and g array to a matrix with k rows and 3 columns. M(m(1,1),m(2,1),g') size (M)= (k,3)
here is my code:
% % % %2D numerical moment calculation
fun = @(x,y)(1-x.^2-y.^2);
R=1; %radius
[r,phi]=meshgrid(linspace(-1,1,100),linspace(2*pi/90,2*pi,90));
X=r.*cos(phi);
Y=r.*sin(phi);
% figure (1)
% plot(X(:),Y(:),'.','markersize', 0.5);
% title('Grid points inside the given circle')
% xlabel('x');ylabel('y')
% % %
figure (2)
subplot(1,2,1)
surf(X,Y,fun(X,Y));
title('Given Function(1-x^2-y^2)')
xlabel('x');ylabel('y');zlabel('f')
%Compute the grid points inside the circle r=1
xx=[-1:0.1:1];
yy=[-1:0.1:1];
X=sort(repmat(xx,[1 length(xx)])');
Y=(repmat(yy,[1 length(yy)])');
zloc = 1-X.^2 - Y.^2;
A=[X Y zloc];
condition=A(:,3)<0;
A(condition,:)=[];
n=length(A);
a=A(:,1); b=A(:,2);
figure(1)
plot(a,b,'.')
%area of domain
area=(pi*R^2);
%%%%%%%%%%%
%grid search
cc=[0;0];
d=1;
max=cc + d;
min=cc - d;
dx=0.1; %unit distance
n=ceil((max-min)/dx) + 1;
% nn=length(A);
f02d=0;
f1x2d=0;
f1y2d=0;
f2x2d=0;
f2y2d=0;
f2xy2d=0;
k=0;
for i1=1:n(1)
m(1,1)=min(1)+dx*((i1)-1);
for i2=1:n(2)
m(2,1)=min(2)+dx*((i2)-1);
if sqrt(1-m(1,1)^2-m(2,1)^2) > 0
x=m(1,1) ;
y=m(2,1);
k=k+1;
f02d=f02d+(fun(x,y));
f1x2d=f1x2d+x*(fun(x,y));
f1y2d=f1y2d+y*(fun(x,y));
f2x2d=f2x2d+x^2*(fun(x,y));
f2y2d=f2y2d+y^2*(fun(x,y));
f2xy2d=f2xy2d+x*y*(fun(x,y));
% for i=1:k
% g(i)=exp((-0.5)*(m'*inv(cov)*m))*(1/(det(cov)*(2*pi)));
% M=zeros(k,3);
% end
end
end
end
% nn=100;
nn=k;
ff0=area*f02d/nn;
ff1x=area*f1x2d/(nn*ff0);
ff1y=area*f1y2d/(nn*ff0);
ff2x=area*f2x2d/(nn);
ff2y=area*f2y2d/(nn);
ff2xy=area*f2xy2d/(nn);
cov=[ff2x ff2xy;ff2xy ff2y];
I appreciate any help.
Thank you.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!