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

3 ビュー (過去 30 日間)
Jeremy Markowski 2021 年 10 月 6 日
コメント済み: Mathieu NOE 2021 年 10 月 6 日
%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 2021 年 10 月 6 日
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 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);

サインインしてコメントする。

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by