HOW TO WRITE MAIN FUNCTION

조회 수: 15 (최근 30일)
ASIM
ASIM 2015년 4월 21일
댓글: Star Strider 2015년 4월 21일
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.

답변 (1개)

Star Strider
Star Strider 2015년 4월 21일
The ‘function’ you want to write is called a ‘script’ in MATLAB. See Programming Scripts and Functions for details.
  댓글 수: 2
ASIM
ASIM 2015년 4월 21일
thanks, I will look into it.
My question is understandable, right? I mean the above one.
Star Strider
Star Strider 2015년 4월 21일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by