Problem
Ask the user to give input to select the rectangle or square or circle or triangle and then ask the user necessary input. Finally calculate the area of the selected geometrical figure using Matlab. The areas of the polygons(circle can also be called as a polygon with infinite sides) is as follows:
1)Area of Rectangle:
Where L is the length and B is the breadth of the rectangle
Area of Rectangle Formula |
2) Area of square:
Area of square Formula |
3) Area of circle:
Area of a circle |
4) Area of triangle:
Where b is the length of the base of triangle and h is the height of the triangle.
Where b is the length of the base of triangle and h is the height of the triangle.
If you want to more about the geometric polygons then you can see:
1) Rectangle
2) Square
3) Circle
4) Triangle
Solution
The solution is very simple if you know what the above figures mean. But if you don't remember the figure then you can see the images below to get a brush up of the polygons.
Rectangle |
Square |
Circle |
Triangle |
According to me there are two major ways to write this program. They both are indirectly same. I will tell you how to solve the problem in both the ways but I will follow the first way because it will be helpful for everyone even if they are beginners in Matlab.
Method 1
We will take the input of the user and then we will use the if else statement to solve the problem accordingly. I am not explaining much here about this method because, we are anyways going to solve this problem and you can see more in the explanation section below the program section.
Method 2
First we will write scripts for the areas of the rectangles, circles, squares and triangles and then we we will finally write a script which will ask the user for the input and use the if else statements which contains direct calling of the scripts. This is similar to the functional approach. I have made a tree diagram of the two methods.
Tree diagram representing the solution for solving the above problem |
Don't worry if you don't understand any of the above concepts or diagrams, skip them and you will understand the rest even if you don't understand them. But trying to understand them is good as you will be using the above concepts in future.
I suggest that you first refer to the program section and then if you don't understand the program then refer to the explanation section to get a detailed explanation about the program. Some of the concepts used in this post are as follows:
1) Matlab Variables
2) input function in matlab
3) fprintf function in matalab
4) if statement in matlab
5) elseif statement in matalb
6) else statement in matlab
I have saved the matlab program as area.m on my local computer.
1) Matlab Variables
2) input function in matlab
3) fprintf function in matalab
4) if statement in matlab
5) elseif statement in matalb
6) else statement in matlab
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | fprintf('Select your choice and enter the option number!\n') fprintf('1) Area of Rectangle\n') fprintf('2) Area of square\n') fprintf('3) Area of circle\n') fprintf('4) Area of triangle\n') user_input = input('You Choice? : '); if user_input == 1 length = input('Enter the length of the rectangle: '); breadth = input('Enter the breadth of the rectangle: '); a = length*breadth; elseif user_input == 2 side = input('Enter the length of the side of the square: '); a = side*side; %This can also be written as side^2 elseif user_input == 3 radius = input('Enter the length of the radius of circle: '); a = pi*(radius^2);%This can also be written as pi*radius*radius elseif user_input == 4 height = input('Enter the height of the triangle: '); base = input('Enter the base length of the triangle: '); a = 0.5*height*base; else fprintf('The input you have entered is wrong\n'); fprintf('Please enter only numbers present in the choice'); fprintf('Try Again!!!'); end fprintf('Area = %.2f\n',a); |
Explanation
In lines 1-5 I have just added the fprintf statements to display the available choices to the user.
In line 6 I used the input function to take the user input and have stored the value in user_input variable.
From line 8 I have started the if statement with a conditional to check if the user has given the input as 1, if the user has given the input 1 then the user is asked to input length and breadth of the rectangle to calculate the area. After the user has given the input then the area has been found out using the relation given in the question.(LxB).
Then on line 12 I have continued the if statement with the elseif statement and then checked if the user has entered the input of 2 and if the input is 2 then the user is asked to enter the length of the side of the square. Finally the area is calculated using the given relation.
Similarly the elseif statements work for the user input corresponding to circle and triangle with 3 and 4 respectively.
The last else statement is used to check if the user has given input other than any of the values given in the options. If the user gives the input of other choices then else gets executed and the fprintf functions below it gets executed.
At the end of the program we have a fprintf function to display the area we have calculated in a neat format so that the user can simply use the value for other computations if he/she requires.
Remember not to name the variable same as the script name because it will generate a matlab error because Matlab assumes that you are using the script name as the variable. That is the reason why I have named my area variable as a instead of area.
Input/Output
Trial run in matlab for area.m script |
Try it yourself
1) Try to write programs for method 2 and comment in the comment box below
2) Try to write program using function approach in the method 1 and comment in the comment box below
Final note
I have tried to explain all the contents in the post in a easy to understand format. If you have not understood anything or have any doubt then please do contact me or comment in the comment box below. You can contact me from here: Contact me
The above program was high lighted using hilite.me we app.
Keywords: area, rectangle, circle, triangle, square, methods, matlab