HOW TO WRITE MAIN FUNCTION
29 views (last 30 days)
Show older comments
Getting down to bussiness;
I have written "TWO separate" functons on matlab that calculates: 1) the area of triangle 2) the area of a parallelogram
But I want to create an M-function that will calculate the area of a triangle or a parallelogram by asking the user to make a choice between Triangle and Parallelogram and then compute the area by calling the right funtion.
%-------------AREA OF TRIANGLE FUNCTION--------------------------------------------------------%;
- function [ points ] = coordinateS( x_1,y_1,x_2,y_2,x_3,y_3 )
*x_1=input('Enter the coordinates (x_1), :' ) *y_1=input('Enter the coordinates (y_1), :' )
*x_2=input('Enter the coordinates (x_2), :' ) *y_2=input('Enter the coordinates (y_2), :' )
*x_3=input('Enter the coordinates (x_3), :' ) *y_3=input('Enter the coordinates (y_3), :' )
*M = [x_1 y_1 1; x_2 y_2 1; x_3 y_3 1]
*% Calculate Area of Triangle (areaT) *Area = [(0.5)*abs(det(M))]
%-------------AREA OF PARALLELOGRAM FUNCTION-------------------------------------------%
- function [ points ] = coordinateS( x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4 )
*x_1=input('Enter the coordinates (x_1), :' ) *y_1=input('Enter the coordinates (y_1), :' )
*x_2=input('Enter the coordinates (x_2), :' ) *y_2=input('Enter the coordinates (y_2), :' )
*x_3=input('Enter the coordinates (x_3), :' ) *y_3=input('Enter the coordinates (y_3), :' )
*x_4=input('Enter the coordinates (x_4), :' ) *y_4=input('Enter the coordinates (y_4), :' )
*M = [x_1 y_1 1; x_2 y_2 1; x_3 y_3 1]
*% Calculate Area of PARALLELOGRAM (areaP) *Area = [abs(det(M))]
%--------------------------------------------------------------------------------------------------------------%
How do i write another funtion that will take user input (from the keyboard). That will calculate the area of a triangle or a parallelogram by asking the user to make a choice between Triangle and Parallelogram and then compute the area by calling the right funtion.
0 Comments
Answers (1)
Star Strider
on 21 Apr 2015
The ‘function’ you want to write is called a ‘script’ in MATLAB. See Programming Scripts and Functions for details.
2 Comments
Star Strider
on 21 Apr 2015
My pleasure.
I believe so. If I understand you correctly, you want to write a script, and that link should define the difference between scripts and functions.
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!