How do I keep my array size consistent?
2 views (last 30 days)
Show older comments
I'm working on a school project where I need to calculate the time it takes to fall through the earth, by calculating it through iterations. So the first iteration would using the radius of the earth to find the final velocity at the center of the earth and then from there calculate the time it takes to get there and back. Then the next iteration would split the distance in half and then using half the radius and then half the acceleration for the second part. The problem Im having is that my array that saves the ,information from the calculations, keeps changing in size from the pre-allocation and in doing so prevents me from subtracting the arrays from each other. So if someone could take a look at my code and see what I'm doing wrong it would be greatly appreciated. Also if anyone could give me any tips for MATLAB that'd really help this is my first time using it.
prompt={'Enter the number of N'};
name = 'Number of Runs';
answer = inputdlg(prompt,name);
q = str2num(answer{1});
V_O = 0;
R = 6371000;
figure(1); hold on
valueofVF = zeros(1,q);
valueoft_t = zeros(1,q);
valueoft = zeros(1,q);
valueoft2 = zeros(1,q);
valueoft3 = zeros(1,q);
for n = 1:q
for i = 1:n
if i > 1
V_O = valueofVF(i-1);
end
V_f = sqrt(((9.81*(i/n))* 2 * (R/n)) + (V_O)^2);
valueofVF(i) = V_f;
for m = 1:n
t = V_f/(V_O + (9.81*(m/n)));
valueoft(n) = t;
end
valueoft2 = valueoft(1:2:end,1:2:end);
valueoft3 = valueoft(1:3:end,1:3:end);
valueoft_t(n) = (sum(valueoft) * 4)/60;
end
plot(i,valueoft_t(n),'.k','markersize', 10);
end
hold off
1 Comment
Walter Roberson
on 14 Feb 2018
I do not encounter a problem when I paste in your code and integers like 10 or 128 ?
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!