"Index must be a positive integer or logical" error in my function to calculate velocity from acceleration

1 view (last 30 days)
When I try to run this function:
function [time,velocity] = velocity(rocket,chutea,chuteb)
[t,a,drag] = calculateAcceleration(rocket,chutea,chuteb,0.1);
v = 0;
for k = 2:0.1:100; % Defined range for time
v(k) = v(k-1) + (a(k-1)*0.1);
end
maximumveloc = max(v)
end
I get the following error message:
Attempted to access veloc(1.1); index must be a positive integer or logical.
Error in Q3_11013011 (line 13)
veloc(k) = veloc(k-1) +
(a(k-1)*0.1);
  4 Comments
Cedric
Cedric on 31 Jan 2013
Edited: Cedric on 31 Jan 2013
On a slightly different topic, I think that if people were reminded on a verified email that they have to come back to "close" their threads (or to actively indicate that they could still use help), more people would at least come back and see what was answered/commented. I am saying that, because I really suspect that many people actually find some way to do what they want after posting the question, and never come back to check their threads. All of you folks who have been active here for a long time, have roughly a 1/3 ratio of accepted answers, but when I look at the threads it seems that a significant amount of the answers that were not accepted are just a consequence of people not coming back..
Randy Souza
Randy Souza on 31 Jan 2013
Edited: Randy Souza on 31 Jan 2013
@Cedric: excellent idea; this is another enhancement that we are working on.
Please feel free to add this and any other suggestions to the MATLAB Answers wish list.
Thanks again!

Sign in to comment.

Accepted Answer

Cedric
Cedric on 28 Jan 2013
Edited: Cedric on 28 Jan 2013
The whole approach is not optimal, but to answer your question specifically, your cannot use k, whose values are floating point, as an index in v and a. One option could be to define an integer loop index..
delta_t = 0.1 ;
a = ... % Do you really get a vector of
% accelerations?
v = zeros(1, numel(a)) ;
for ii = 2 : numel(v)
v(ii) = v(ii-1) + a(ii-1)*delta_t ;
end
Now there are a few other things that won't work in your function. The function that computes the acceleration returns I guess a vector of times t and a vector of accelerations a(?).. but your function first output arg is named time and not t. You function 2nd output arg is named velocity but you define a vector named v. Finally, you compute max velocity, but you don't return it.
For computing velocities, you want to integrate the acceleration. You are performing this integration "by hand" here. If you want to optimize it a little (without talking about integration, play a bit with the following to understand how you could use it in your case:
>> a = [1 3 2 -1 0 1] ;
>> dt = 4 ;
>> dv = a .* dt ;
>> v = [0, cumsum(dv)]
v =
0 4 16 24 20 20 24
>> diff(v) ./ dt % Check
ans =
1 3 2 -1 0 1 % Looks like a!
  5 Comments
Randy Souza
Randy Souza on 31 Jan 2013
Cedric, thanks for taking the time to provide a detailed answer; I'm sorry for the original asker's bad behavior. We are working on ways to prevent and/or detect this behavior, but, as Jan points out, it's a tricky problem.
You are welcome to capture PDFs of the questions that you answer so that you can restore the question if needed, but it might be easer for you to simply flag it. I'll see the flag and restore the original text. As I'm about to do for this question...

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!