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

조회 수: 5(최근 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!