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

6 次查看(过去 30 天)
%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;

回答(1 个)

Alan Stevens
Alan Stevens 2021-10-6
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 个评论
Mathieu NOE
Mathieu NOE 2021-10-6
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by