HOW TO WRITE MAIN FUNCTION

38 views (last 30 days)
ASIM
ASIM on 21 Apr 2015
Commented: Star Strider on 21 Apr 2015
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.

Answers (1)

Star Strider
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
ASIM
ASIM on 21 Apr 2015
thanks, I will look into it.
My question is understandable, right? I mean the above one.
Star Strider
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.

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices 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!