Making a Euler Method script to solve for velocity but am encountering "Array indices must be positive integers or logical values" Error

4 views (last 30 days)
Jeremy Markowski
Jeremy Markowski on 6 Oct 2021
Commented: Mathieu NOE on 6 Oct 2021
%Script for Euler Method to numerically solve the
%velocity of a skydiver modeled as a smooth sphere
clc;
close all;
clear;
m = 70.0; % Mass in kg
D = 0.47; % Drag Coefficent
R = 0.5; % Radius of Sphere in m
p = 1.225; % Density of air in kg/m^3
A = 0.79; % Fronal area in direction of travel in m^2
g = 9.81; % Gravity in m/s^2
h = 3; % Time step Size
t = 0:h:9; % the range of t
v = zeros(size(t)); % allocate the result v
v(1)=0; % Inital condition of velocity
for t=0:3:9
f = (g - (D * p * A * v.^2)/(2*m));
v(t+1) = v(t) + h * f;
end
Array indices must be positive integers or logical values.
Error in Untitled3 (line 22)
v(t+1) = v(t) + h * f;

Answers (1)

Alan Stevens
Alan Stevens on 6 Oct 2021
Try changing your loop to
for i=1:numel(t) %%% i loop not t
f = (g - (D * p * A * v(i).^2)/(2*m)); %%% v(i)
v(i+1) = v(i) + h * f; % i not t
end
  1 Comment
Mathieu NOE
Mathieu NOE on 6 Oct 2021
almost there !
the for loop must be stopped 1 index earlier to have t and v the same length
clc;
close all;
clear;
m = 70.0; % Mass in kg
D = 0.47; % Drag Coefficent
R = 0.5; % Radius of Sphere in m
p = 1.225; % Density of air in kg/m^3
A = 0.79; % Fronal area in direction of travel in m^2
g = 9.81; % Gravity in m/s^2
h = 1; % Time step Size
t = 0:h:9; % the range of t
v = zeros(size(t)); % allocate the result v
v(1)=0; % Inital condition of velocity
for i=1:numel(t)-1 %%% i loop not t
f = (g - (D * p * A * v(i).^2)/(2*m)); %%% v(i)
v(i+1) = v(i) + h * f; % i not t
end
plot(t,v);

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by