Filter löschen
Filter löschen

Euler's Method using a for loop

7 Ansichten (letzte 30 Tage)
Peter Bohlen
Peter Bohlen am 21 Mai 2024
Kommentiert: Les Beckham am 21 Mai 2024
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))

Akzeptierte Antwort

Les Beckham
Les Beckham am 21 Mai 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 Kommentare
Peter Bohlen
Peter Bohlen am 21 Mai 2024
Thank you very much for your help!!!
Les Beckham
Les Beckham am 21 Mai 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by