Azzera filtri
Azzera filtri

Euler's Method using a for loop

7 visualizzazioni (ultimi 30 giorni)
Peter Bohlen
Peter Bohlen il 21 Mag 2024
Commentato: Les Beckham il 21 Mag 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))

Risposta accettata

Les Beckham
Les Beckham il 21 Mag 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 Commenti
Peter Bohlen
Peter Bohlen il 21 Mag 2024
Thank you very much for your help!!!
Les Beckham
Les Beckham il 21 Mag 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by