Clear Filters
Clear Filters

Euler's Method using a for loop

7 views (last 30 days)
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))

Accepted Answer

Les Beckham
Les Beckham on 21 May 2024
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 Comments
Peter Bohlen
Peter Bohlen on 21 May 2024
Thank you very much for your help!!!
Les Beckham
Les Beckham on 21 May 2024
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!