How To Draw A Circle In Matlab
Area of rectangle, foursquare, circle and triangle using Matlab
Trouble
Ask the user to give input to select the rectangle or foursquare or circle or triangle and and then ask the user necessary input. Finally summate the area of the selected geometrical figure using Matlab. The areas of the polygons(circumvolve tin also exist chosen as a polygon with infinite sides) is as follows:
1)Area of Rectangle:
|
Area of Rectangle Formula |
Where L is the length and B is the breadth of the rectangle
|
Area of square Formula |
Where a is the length of the side of a square
|
Area of a circle |
Where r is the radius of the circle
4) Area of triangle:
Where b is the length of the base of operations of triangle and h is the height of the triangle.
If you desire to more well-nigh the geometric polygons and so you lot can run into:
Solution
The solution is very simple if y'all know what the to a higher place figures mean. But if you don't remember the figure so you can come across the images below to go a brush up of the polygons.
|
Rectangle |
|
Square |
|
Circle |
|
Triangle |
That's information technology if you know about these figures then information technology is very piece of cake to solve the problem in Matlab. It is similar to the Display current engagement and fourth dimension in Matlab poblem which we have solved earlier. Don't worry if yous take not solved that problem, I am assuming that you are starting from scratch after reading a bit of Matlab programming. Okay lets become started.
According to me there are two major ways to write this program. They both are indirectly same. I volition tell yous how to solve the problem in both the ways merely I volition follow the first way considering it will be helpful for anybody even if they are beginners in Matlab.
Method 1
We volition accept the input of the user so we will use the if else argument to solve the trouble accordingly. I am not explaining much hither about this method because, we are anyways going to solve this problem and yous can run into more in the caption section below the program section.
Method 2
Commencement nosotros will write scripts for the areas of the rectangles, circles, squares and triangles so we we volition finally write a script which volition ask the user for the input and apply the if else statements which contains directly calling of the scripts. This is similar to the functional approach. I have fabricated a tree diagram of the two methods.
|
Tree diagram representing the solution for solving the above problem |
We tin can see from the tree diagram that using method 1 we tin solve in two ways: We tin create functions for each of the areas and then solve information technology or we can straight solve it under simple manner so that fifty-fifty the beginners volition sympathize what is happening in the code. We are going to use simple arroyo to solve the problem.
Don't worry if you don't sympathize whatever of the above concepts or diagrams, skip them and you volition sympathize 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 propose that y'all get-go refer to the program section and then if you don't understand the plan then refer to the explanation section to become a detailed explanation about the programme. Some of the concepts used in this mail are equally follows:
i) Matlab Variables
ii) input function in matlab
3) fprintf part in matalab
4) if statement in matlab
v) elseif statement in matalb
6) else statement in matlab
Program
1 two three 4 5 half-dozen 7 8 9 10 11 12 13 14 15 16 17 18 19 xx 21 22 23 24 25 26 27 28 | fprintf('Select your selection and enter the option number!\n') fprintf('1) Area of Rectangle\n') fprintf('2) Area of square\n') fprintf('3) Area of circumvolve\n') fprintf('4) Expanse of triangle\n') user_input = input('You Choice? : '); if user_input == ane length = input('Enter the length of the rectangle: '); latitude = input('Enter the latitude of the rectangle: '); a = length *breadth; elseif user_input == two 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 besides exist written as pi*radius*radius elseif user_input == four height = input('Enter the top of the triangle: '); base = input('Enter the base length of the triangle: '); a = 0.5 *top*base; else fprintf('The input you have entered is wrong\n'); fprintf('Please enter only numbers nowadays in the choice'); fprintf('Endeavour Again!!!'); end fprintf('Area = %.2f\n',a); |
I take saved the matlab program every bit area.1000 on my local estimator.
Explanation
In lines ane-v I have just added the fprintf statements to display the available choices to the user.
In line half dozen I used the input function to take the user input and take 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 ane so the user is asked to input length and latitude of the rectangle to calculate the area. After the user has given the input then the expanse has been found out using the relation given in the question.(LxB).
And 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 surface 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 terminal else statement is used to check if the user has given input other than whatever of the values given in the options. If the user gives the input of other choices so else gets executed and the fprintf functions below it gets executed.
At the finish of the program we have a fprintf function to display the area nosotros take calculated in a neat format so that the user can merely apply the value for other computations if he/she requires.
Call up non to name the variable aforementioned as the script name because information technology will generate a matlab fault because Matlab assumes that you lot are using the script proper noun as the variable. That is the reason why I accept named my area variable as a instead of area.
Input/Output
|
Trial run in matlab for area.m script |
Attempt it yourself
1) Effort to write programs for method ii and comment in the comment box below
ii) Endeavor to write program using role approach in the method 1 and comment in the annotate box below
Final notation
I have tried to explicate all the contents in the postal service in a easy to sympathize format. If you have not understood anything or have whatsoever incertitude 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 nosotros app.
Keywords: area, rectangle, circle, triangle, foursquare, methods, matlab
Source: https://radiusofcircle.blogspot.com/2015/12/area-of-rectangle-square-circle-and-triangle-using-matlab.html
Posted by: alanizthates.blogspot.com
0 Response to "How To Draw A Circle In Matlab"
Post a Comment