replace diff function with for loop and if structures

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

What have you done so far? What specific problems are you having with your code? What is the purpose of the IF branching structures? Are you supposed to assume constant acceleration between points? Or ...? You need to make an effort at writing code and give us more details about the assignment.
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 ?
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 9월 23일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2022년 9월 23일

댓글:

2022년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by