Euler's Method using a for loop

22 visualizaciones (últimos 30 días)
Peter Bohlen
Peter Bohlen el 21 de Mayo de 2024
Comentada: Les Beckham el 21 de Mayo de 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))

Respuesta aceptada

Les Beckham
Les Beckham el 21 de Mayo de 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 comentarios
Peter Bohlen
Peter Bohlen el 21 de Mayo de 2024
Thank you very much for your help!!!
Les Beckham
Les Beckham el 21 de Mayo de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by