Trying to create a vector in steps of 0.5

264 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Stephen23
Stephen23 il 24 Gen 2016
Modificato: Stephen23 il 24 Gen 2016
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 Commenti
eoin247
eoin247 il 24 Gen 2016
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 il 24 Gen 2016
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!

Accedi per commentare.

Più risposte (1)

Nemanja
Nemanja il 9 Giu 2023
Modificato: Nemanja il 9 Giu 2023
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 Commento
Stephen23
Stephen23 il 9 Giu 2023
"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

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by