HOW TO WRITE MAIN FUNCTION

15 次查看(过去 30 天)
ASIM
ASIM 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