replace diff function with for loop and if structures

1 visualizzazione (ultimi 30 giorni)
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 Commenti
anthony sithembiso
anthony sithembiso il 29 Set 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 il 29 Set 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

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 23 Set 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.

Tag

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by