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

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

@author: Wow, I find it very annoying, if a question is deleted after a voluntary contributor spent time for posting a useful answer. Without the text of the question, the answer is useless for the community and the time and energy for creating the answer is stolen. This is massively unfriendly and it surely reduces the motivation to answer.
Here the author even changed the login name. I do not have any problems with solving questions apart from this forum. But then a fair payment with real money is obviously the correct strategy.
@Randy: I do not see any method to make such obfuscations impossible. But I do not think that a parasitic behaviour should be tolerated. Perhaps there is some space in the layout to include a mativating slogan like: "Think twice if you are acting like you want others to act in a public forum" or shorter: "pull yourself together".
I have restored the original text of this question.
1 1, this question has a clear subject and a thorough answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.
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..
@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

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

Seems that the OP had something to hide.. if anybody wants to restore the original question, you have my blessing!
No answer, no thanks, not vote, not accepted, question deleted, login name changed. The anonymity in the internet encourages people to demonstrate in public how less reliable they are in a collaboration with others. But if we use the TCP/IP to locate the real person and find out, that this low degree of cooperativity appears in the real life also, how could we use this information?!
Matt Fig told me in this forum: "Post answers which can be useful, the rest is fluff".
Thank you for your comment and the vote! I will consider printing threads as PDF from now on.. just to be able to re-post the original question when it is deleted. Sometimes just restoring the original question is enough for these people to be spotted by their prof.
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...
Thank you, Randy, for your support and for having restored the question!

Sign in to comment.

More Answers (0)

Categories

Asked:

1 1
on 28 Jan 2013

Community Treasure Hunt

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

Start Hunting!