Draw Circle Using Rectangle Matlab

Area of rectangle, square, circumvolve and triangle using Matlab

Problem

Ask the user to give input to select the rectangle or square or circumvolve 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 besides be called as a polygon with infinite sides) is as follows:

1)Area of  Rectangle:

Area of Rectangle - LxB
Area of Rectangle Formula

Where L is the length and B is the latitude of the rectangle

Area of square - a2 - axa
Area of square Formula

Where a is the length of the side of a square

Area of circle Formula - pi*r*r - pi*r^2
Area of a circle

Where r is the radius of the circumvolve

four) Area of triangle:

Where b is the length of the base of triangle and h is the superlative of the triangle.

If you want to more than nearly the geometric polygons and then you lot tin see:

Solution

The solution is very simple if you lot know what the above figures mean. But if you don't recollect the figure then you tin run into the images beneath to get a castor upwards of the polygons.

Rectangle
Rectangle
Square
Square
circle
Circumvolve
Triangle
Triangle

That's it if you know virtually these figures then it is very like shooting fish in a barrel to solve the problem in Matlab. It is similar to the Display electric current date and fourth dimension in Matlab poblem which we have solved earlier. Don't worry if you have not solved that problem, I am assuming that you are starting from scratch later on 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 will tell you how to solve the problem in both the ways only I will follow the showtime style because information technology will be helpful for everyone fifty-fifty if they are beginners in Matlab.

Method i

We will take the input of the user and then nosotros volition apply the if else argument to solve the trouble accordingly. I am non explaining much here almost this method because, we are anyways going to solve this problem and y'all can see more in the explanation section below the program section.

Method 2

First nosotros will write scripts for the areas of the rectangles, circles, squares and triangles and then we we will finally write a script which will enquire 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 methods on how to solve the areas of circle, triangle, rectangle and squares
Tree diagram representing the solution for solving the above trouble

Nosotros can see from the tree diagram that using method one nosotros can solve in two ways: We can create functions for each of the areas and then solve it or we can straight solve it nether simple mode so that fifty-fifty the beginners will understand what is happening in the code. We are going to use elementary approach to solve the problem.

Don't worry if y'all don't empathise any of the in a higher place concepts or diagrams, skip them and y'all volition understand the residuum fifty-fifty if you don't sympathise them. But trying to understand them is skillful equally you volition be using the to a higher place concepts in hereafter.

I suggest that you first refer to the programme section and then if y'all don't empathise the program then refer to the caption section to get a detailed explanation nearly the plan. Some of the concepts used in this mail service are as follows:
1) Matlab Variables
two) input function in matlab
three) fprintf function in matalab
4) if argument in matlab
5) elseif statement in matalb
6) else statement in matlab

Program


                        1  2  three  4  5  vi  vii  eight  9 10 11 12 xiii 14 15 16 17 18 19 twenty 21 22 23 24 25 26 27 28
fprintf('Select your choice and enter the option number!\n') fprintf('i) Area of Rectangle\n') fprintf('2) Area of square\n') fprintf('3) Area of circumvolve\n') fprintf('4) Area of triangle\northward') user_input = input('You lot 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 exist written as side^ii                        elseif                        user_input                        ==                        3                        radius = input('Enter the length of the radius of circle: ');     a =                        pi                        *(radius^2);%This can also exist written every bit pi*radius*radius                        elseif                        user_input                        ==                        4                        superlative = input('Enter the height of the triangle: ');     base = input('Enter the base length of the triangle: ');     a =                        0.v                        *height*base;                        else                        fprintf('The input y'all have entered is wrong\n');     fprintf('Delight enter only numbers present in the selection');     fprintf('Try Again!!!');                        end                        fprintf('Area = %.2f\n',a);                      

I have saved the matlab programme as area.grand on my local figurer.

Explanation

In lines 1-five I have just added the fprintf statements to display the available choices to the user.

In line 6 I used the input part to take the user input and have stored the value in user_input variable.

From line 8 I have started the if argument with a conditional to check if the user has given the input as ane, if the user has given the input 1 then the user is asked to input length and breadth of the rectangle to calculate the surface area. Afterward the user has given the input and so the surface area has been found out using the relation given in the question.(LxB).

Then on line 12 I have continued the if argument with the elseif argument and and so checked if the user has entered the input of two and if the input is 2 so 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 piece of work for the user input respective to circumvolve and triangle with 3 and four respectively.

The terminal else statement is used to cheque 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 and then else gets executed and the fprintf functions beneath it gets executed.

At the end of the program nosotros have a fprintf office to brandish the surface area we have calculated in a neat format so that the user tin can just use the value for other computations if he/she requires.

Remember not to name the variable same equally the script proper name because it volition generate a matlab error because Matlab assumes that you are using the script name equally the variable. That is the reason why I have named my area variable every bit a instead of expanse.

Input/Output

area of rectangle, circle, triangle, square using matlab, area.m file
Trial run in matlab for surface area.thou script

Endeavor it yourself

1) Endeavor to write programs for method two and comment in the annotate box below

ii) Try to write program using function approach in the method 1 and comment in the comment box beneath

Final note

I have tried to explain all the contents in the post in a easy to sympathize format. If yous take not understood anything or accept any incertitude then please do contact me or comment in the comment box below. You lot tin can contact me from here: Contact me

The to a higher place program was high lighted using hilite.me we app.

Keywords: area, rectangle, circle, triangle, foursquare, methods, matlab

ranktheromid.blogspot.com

Source: https://radiusofcircle.blogspot.com/2015/12/area-of-rectangle-square-circle-and-triangle-using-matlab.html

0 Response to "Draw Circle Using Rectangle Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel