Euler's Method using a for loop

22 次查看(过去 30 天)
I am trying to produce a graph of displacement vs. velocity of a falling parachuter and produce tabulated values. I have been given the function--which I have attached a screenshot of. My code is currently producing the error: "Array indices must be positive integers or logical values.
My Code:
clear all
%initial conditions
g0 = 9.81; %m/s^2
R = 6.37e6; %m
h = 10000; %Step Size in m
x = 0 : h : 100000; % Range of X values
v = zeros(size(x));
vi = 1400; %m/s Initial velocity
n = numel(v); % Number of values for velocity
for i=1:n-1
v(x(i+1)) = v(x(i)) + ((g0/v(x(i))) * (R^2/ ((R + x(i))^2))) * (x(i+1) - x(i));
end
plot (x(i),v(i))

采纳的回答

Les Beckham
Les Beckham 2024-5-21
I'm not sure I believe that the equation given in your attached image is correct. However, the changes below allow the code to run so that you can work out the details.
%initial conditions
g0 = 9.81; %m/s^2
R = 6.37e6; %m
h = 10000; %Step Size in m
x = 0 : h : 100000; % Range of X values
v = zeros(size(x));
v(1) = 1400; %m/s Initial velocity <<<<< initialize the first element of v
n = numel(v); % Number of values for velocity
for i=1:(n-1)
v(i+1) = v(i) + ((g0/v(i)) * (R^2/ ((R + x(i))^2))) * (x(i+1) - x(i));
% ^^-----^^----------^^------index using i rather than x(i)
end
plot (x,v) % << change to plot the entire v vector against the entire x vector
grid on
  2 个评论
Peter Bohlen
Peter Bohlen 2024-5-21
Thank you very much for your help!!!
Les Beckham
Les Beckham 2024-5-21
You are quite welcome.
Also, if you are just getting started with Matlab, I would highly recommend that you take a couple of hours to go through the free online tutorial: Matlab Onramp

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by