Saturday, March 10, 2012

LESSON 2: LET'S CODE MORE WITH TABLING THE OUTPUTS


Recall that from the first post, we have already:
  • Algorithm for any program should consists of three main sections - input, computation and output
  • Basic syntax for mathematical operation
  • Solving the assignment given
For this post, we will discuss on more coding.
P/S: Before I proceed, I would like to make some correction. From Figure 1.4, the semicolon (;) at the end of the last code line is preferably dropped to result in automation of output display when running the program.

Here is the new assignment given this week:

QUESTION: 
Write a Matlab program, using a script M-file, that calculates the velocity and acceleration of an aircraft at time 0 to 120 seconds with increments of 10 seconds. Display the results in a table of time, velocity and acceleration.

The following are the equations to estimate the velocity and the acceleration of the aircraft:
velocity=0.00001*time.^3−0.00488*time.^2+0.75795*time+181.3566
acceleration=3−0.000062velocity.^2

NOTE: I will leave it to you to solve the algorithm and the flowchart before doing the Matlab code.

Here's how to do your simple Matlab code:

1. It is advised that you start your fresh code with the keyword clear as has been explained in the first post (Figure 2.1).

2. You are now required to define the input as t (time). #Suggested code can be obtained in the Figure 2.1. You can use t = input ('Insert range of time in s: '); as well although that would require you to key in

3. You are now required to compute the variable V (velocity) and A (acceleration) using the formula given in the question and with the variables t that you have defined as the input in step (2) (Figure 2.1). The code is as follows:

V = 0.00001*t.^3 - 0.00488*t.^2 + 0.75795*t + 181.3566;
A = 3 - 0.000062*V.^2;

Figure 2.1

TIPS: For computation of power, please make sure that you inserted .^ instead of ^ only. I am not sure what is the explanation but it seems that the latter won't work when running the program.

4. You need to define your output (or display or print). The question requires you to compute Velocity and Acceleration as the result, then display them in the table form (Figure 2.2). Therefore, the code is as follows:

table = [t' V' A'];

5. To display the output in the form of table with headings on top of it, use the following additional codes (Figure 2.2):

disp (' Time      Velocity    Acceleration '); 
disp (table);

TIPS: The difference between step (4) and the additional step (5) is that step (4) results in tabulation of matrix that does not have the headings 'time', 'velocity' and 'acceleration' above the data when running the code.

Figure 2.2

6. Save your code and run it on the command window.

GOOD LUCK IN TRYING. Thank you for staying till the end of this article. Any improvements on the Matlab code can be submitted to the comment box below.