QUESTION:
The area of a triangle having side lengths a, b and c is:
Area = √(s(s-a)(s-b)(s-c)) - where; s = (a+b+c)/2
Write a computer code (Matlab) to prompt user for the three sides of a triangle and then compute and print the area using the relationship above. You are expected to submit the algorithm, flowchart and the computer (Matlab) code.
NOTE: I will leave it to you to solve the algorithm and the flowchart.
Here's how to do your simple Matlab code:
1. Open your Matlab software. Now, you are at the command window.
2. Type edit and enter. Now you have open the editor window.
Please note that the editor is the place you do the coding. The command window can be used to do so too, as well as served as a place for checking and experimenting. Analyze the question and do the proper algorithm before you start the coding. The algorithm should have the following:
- Input - you are required to name the input A, B and C; as well as to define the S (I'm using capital letter here)
- Computation - you are required to compute AREA
- Output - you are required to determine the output, in this case is the AREA
TIPS: 'Clear' erases any existing variables and helps to reduce risk of errors that may arise.
4. Second step; you are now required to define the inputs. We have earlier, determined that we need to put three different inputs of a, b and c to initiate computation. Suggested codes are displayed in Figure 1.1 below:
Figure 1.1 |
The codes are divided into two distinguished commands here as in black and purple-colored text. The black-colored code defines the variables A, B and C or the side lengths of the triangle as the input. The purple-colored code, meanwhile is a description of the input variables that you can fill in when you run the code on the command window (we will get back to this later).
5. Third step; you will need to define and compute the variable S using the formula given in the question and with the variables A, B and C that you have defined as the input in step (4). The code is as follows:
S = (A+B+C)/2;
TIPS: Semi-colon (;) is inserted at the end of each code line (if necessary only) to stop the program from auto-computing that particular formula on that line. It may not be critical in the editor window but it will be in the command window.
6. Fourth step; you are now required to insert the computation code to compute area of the triangle based on the variables A, B, C and S. Use variable AREA to represent area. The code is as follows:
AREA = sqrt (S*(S-A)*(S-B)*(S-C));
TIPS: Figure 1.2 below shows the syntax for some basic mathematical operations:
Figure 1.2 |
7. The fifth step will be that you need to define your output (or display or print). The question requires you to compute AREA as the result. Therefore, the code is as follows:
Output = AREA;
8. There is no exact syntax code for stop here. Leaving it as it is should be subtle. Please note that adding stop or end at the end of the whole coding may causes error. End syntax should only used to close a loop or repetitive conditioning. After doing steps (1) to (7), your code should look something like the one displayed in Figure 1.3.
Figure 1.3 |
9. Save this file to your proper directory on the computer. For mine, I will save it as "AreaTriangle2" and will be saved to the Document directory. On the top of the editor window, Matlab will usually show the location of your saved file in within the directory of files (Figure 1.4).
Figure 1.4 |
10. Now, back to your command window. Type the name of the code file you have just saved. If you have type the name of the file correctly, you will get something as in Figure 1.5. If not, change the director or current folder.
Figure 1.5 |
11. Recall from step (4) about the purple-colored text. On the command window, you will know see the instruction "insert an integer for a". Type the value of side length a as the input for A then press enter. Do the same for B and C. For this example, I will insert A = 20, B = 10 and C = 15 (Figure 1.6).
Figure 1.6 |
12. If you have get your code correct, you should be able to get the AREA correctly. Type output and press enter. The output is the computed area of the triangle. You can always hit your calculator to check the answer (Figure 1.7). You can try with different values for A, B and C.
Figure 1.7 |
13. You have completed the assignment. Congratulation.
GOOD LUCK IN TRYING. Thank you for staying till the end of this article.