Trying to create a vector in steps of 0.5

269 次查看(过去 30 天)
I'm trying to get the vector to go up in steps of 0.5.
v_numerical(0.5)= g.*0.5;
for i = 1:0.5:10
v_numerical(i) = v_numerical(i-0.5)+(g-((c.*v_numerical(i-0.5))./m).*(0.5));
end
However whenever i try running it I get the error:
Attempted to access v_numerical(0.5); index must be a positive integer or logical.
Any idea how I can solve this problem? Many thanks in advance!

采纳的回答

Stephen23
Stephen23 2016-1-24
编辑:Stephen23 2016-1-24
MATLAB indices start at one, and must be whole numbers (i.e. no fraction allowed). With your code you are trying to index with 0.5 and lots of other fractions, which will always be an error, like this:
>> A(0.5) = 3 % fraction index is an error
??? Attempted to access (0.5); index must be a positive integer or logical.
>> A(1) = 3 % whole number index is okay
A =
3
You can easily create a vector with 0.5 spacing:
V = 1:0.5:10;
but when I tried to fix your code it made very little sense to me what you are trying to achieve. Can you please explain what you are trying to do with this code: what should the input and output be?
  2 个评论
eoin247
eoin247 2016-1-24
I'm trying to plot a graph of velocity against time in steps of 0.5 and compare the analytic and numerical approaches. Here is the whole code:
close all; clear;
%defining known variables
g = 9.81; c = 12.5; m = 70;
%defining timestep
tstep = 0.5 ;
%defining analytic time vector
t = (0:tstep:10);
%get analytic velocity
v_analytic =(g.*m./c).*(1-(exp((-c.*t)./m)));
%first numerical velocity point
v_numerical(0.5)= g.*0.5;
%setting up plot and plotting analytic velocity
figure(1); hold on; grid on; box on;
plot(t,v_analytic,'b*-')
title('parachutist');
xlabel('time (s)');
ylabel('velocity (m/s)');
%numerical calculations using a 'for' loop
for i = 1:0.5:10
v_numerical(i) = v_numerical(i-0.5)+(g-((c.*v_numerical(i-0.5))./m).*(0.5));
end
%numerical results and plot
plot(t,v_numerical,'ro-')
legend('analytical','numerical')
eoin247
eoin247 2016-1-24
Actualy i think I just realised that your original answer solved my problem. I needed to define the vector in Ste[s of 0.5 before hand which i had not done.
Thank you for the answer!

请先登录,再进行评论。

更多回答(1 个)

Nemanja
Nemanja 2023-6-9
编辑:Nemanja 2023-6-9
g = linspace(1:10:20) %goes from 1 to 10 and has 20 elements because 10/20 is 0.5 so it will go up from 1 to 10 with 0.5 spacing%
  1 个评论
Stephen23
Stephen23 2023-6-9
"goes from 1 to 10 and has 20 elements because 10/20 is 0.5 so it will go up from 1 to 10 with 0.5 spacing"
No, you are making this error:
It is easy to check that your statement is incorrect: did you try running the code yourself before posting here?
g = linspace(1,10,20) % fixed incorrect syntax
g = 1×20
1.0000 1.4737 1.9474 2.4211 2.8947 3.3684 3.8421 4.3158 4.7895 5.2632 5.7368 6.2105 6.6842 7.1579 7.6316 8.1053 8.5789 9.0526 9.5263 10.0000
Tip: to split the range 1..10 into steps of 0.5 then you need 19 elements. Lets try it now:
g = linspace(1,10,19)
g = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by