replace diff function with for loop and if structures

1 visualización (últimos 30 días)
Jack
Jack el 23 de Sept. de 2022
Comentada: Walter Roberson el 29 de Sept. de 2022
Time, seconds Altitude, meters
0 0
1.00 107.37
2.00 210.00
3.00 307.63
4.00 400.00
5.00 484.60
6.00 550.00
7.00 583.97
8.00 580.00
9.00 549.53
10.00 570.00
Determine the velocity and acceleration by using FOR loop, nested or not, and two nested IF branching structures.
  4 comentarios
anthony sithembiso
anthony sithembiso el 29 de Sept. de 2022
clear memory
clear all
clc
rocket_launch=readmatrix('rocket_launch.xlsx');
time=[rocket_launch(:,1)]';
distance=[rocket_launch(:,2)]';
v=0;
for k=1:length(time)
for j=1:length(distance)
end
end
if i want to use equation of motion, how can i write the equation on matlab using the nested if branching structure inside the for loop structure ?
Walter Roberson
Walter Roberson el 29 de Sept. de 2022
What would you be testing inside the loop? What condition needs to be distinguished and which varies for different values or varies in accumulated effect ?
For example are you requested to model until the rocket hits the ground? Testing for altitude 0 would be reasonable inside a loop, but as I described in my Answer, determining the velocity and acceleration does not need tests inside the loop

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 23 de Sept. de 2022
Using if structures is pretty useless there. The calculation is very straight forward, and the only relevant questions are:
  • do you have at least two data points? If not you cannot determine velocity
  • do you have at least three data points? If not you cannot determine acceleration
  • are the points sorted in ascending time? If not then you need to sort them (which is where if statements might be useful)
  • are there any duplicate times? If so then you need to decide what to do for that case
But we can see in the data that none of those problem conditions hold, so if the task is to process this particular dataset then the tests are make-work . If the code has to be generalized to handle additional datasets then it needs to be specified ahead of time what should be done if the conditions fail -- and if it needs to be able to handle all of the conditions I list above then you might need more loops or more levels of if than the question requires you to use.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by