Output to an Array

2 views (last 30 days)
Lily Muller
Lily Muller on 4 Sep 2014
Answered: Iain on 4 Sep 2014
I am trying to input variables into the function gpoly which is designed to calculate the gravity (g) for a 2D rectangle defined by the X-co-ordinates (xcorn) and Z-co-ordinates (zcorn). I have written a for loop to do this over a series of rectangles adjacent to each other and the output should be the gravity (g) for each rectangle. There should be 100 rectangles. However the output array is 2001x1 and has only the value from the final rectangle which is repeated every 20th value of the array when it should be 100x1 with the g values for each of the rectangles. This process is repeated for different values of dz - this should produce a new array, though I'm not sure how to do this. Please help me with the outputs as I think I'm hopelessly wrong!
clear
cellcnt=100
xmax=2001
xmin=1
dx=(xmax-xmin)/cellcnt
z=30
g=zeros(1,100);
for dz=10:10:100,
for x=xmin:dx:xmax,
xcorn=[x, x+dx, x+dx, x];
zcorn=[z, z, z+dz, z+dz];
ncorn=4;
x0=x+(dx/2);
z0=0;
rho=3230; %kg/m3
g(x)=gpoly(x0,z0,xcorn,zcorn,ncorn,rho);
end
end
Thanks for the help.
Lily

Answers (1)

Iain
Iain on 4 Sep 2014
You're indexing like this:
for x = 1:20:2001
g(x) = blah;
end
You should be indexing like this:
for i = 1:1:100
x = x + dx;
g(i) = blah;
end
I'm not sure if you have another problem in the code - me no know what gpoly does!

Products

Community Treasure Hunt

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

Start Hunting!