Greetings and welcome to Bern’s section on the basic computation programming for engineers. In this first article that I have wrote for this special section on my blog, I would like to talk about constructing a simple computer language code using the Matlab for a common mathematical question. If you are my JKKP classmates, I am sure that you have the question for the assignment given by Dr Meor with you. If you are not, here is the question stipulated:

__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

3. First step; it is advised that you start your fresh code with the keyword

*clear*(Figure 1.1).**
**

**'Clear' erases any existing variables and helps to reduce risk of errors that may arise.**

*TIPS:*
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;*

**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.**

*TIPS:*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));*

**Figure 1.2 below shows the syntax for some basic mathematical operations:**

*TIPS:*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 "

**". 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).**

*insert an integer for a*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.

**Article Source : bernard-greentea.blog**

## 3 comments:

hello sir

send me matlab code for collision detection using Neural network

arvindwankhede111@gmail.com

Hi

Need Mat Lab codes for "Unit Commitment Using PSO"

Pl send me the codes

jayashreebhogendrakumar@gmail.com

I working on a matlab code for a function that will accept a vector T, and a scalar zeta, and return a vector y whose values are defined as ...

y=exp(-zeta*T)*(2*zeta*cos(Wd*T)+

((We/Wd)*sin(Wd*T)));

I made the code given below, I keep getting undefined variable error for V2 and T along with error for improper input of *

If anyone can guide me in the right direction I would be greatful! Thank you!

clear

prompt='T zeta';

T=v1;

zeta=v2, i.e. [0,4*pi];

zeta=input(v2);

result=input(zeta);

v1-v2;

We=(1-(2*zeta.^2));

Wd=sqrt(1-zeta.^2);

x=-4:.001:4;

y=exp(-zeta.*T).*(2*zeta.*cos(Wd.*T)+((We./Wd).*sin(Wd.*T)));

## Post a Comment