AIM: Study and Implementation of
user defined function.
THEORY:
MATLAB
function is a special type of Mfile that runs
in its own independent workspace. It receives input data through an input argument list, and returns results to the caller through an output argument list.
Syntax of function:
function [output parameters] = function_name (Input parameters)
Statements;
Statements;
Statements;
Statements;
End

The function statement
marks the beginning of the function. It specifies the name of the function and
the input and output argument lists. The input argument list appears in
parentheses after the function name, and the output argument list appears in
brackets to the left of the equal sign. (If there is only one output argument,
the brackets can be dropped.) Each ordinary MATLAB function should be placed in
a file with the same name (including capitalization) as the function, and the
file extent “.m”. For example, if a function is named My_fun, then that
function should be placed in a file named My_fun.m.
PROCEDURE:
1. Program for finding Area
and Circumference of the circle:
Area of the circle having formula Area = pi*r^2;
Circumference of the circle is = 2*pi*r;
Create a function having two outputs. One is Area and other
is circumference of the circle with only one input radius.
2. Program for finding
distance between two Cartesian coordinates:
Point p1(x1,y1) and point p2(x2,y2)
these are two points used to find distance. Write a function having only one
output that is distance and having four input values x1, x2, y1 and y2. With
the help of above equation just implement function which calculate distance between
two points.
1. Program for finding AREA and CIRCUMFERENCE of circle
using function:
Function:
function [area circum]=AREA(r)
%
output Parameters:
%
area = Area of Circle
%
Circum = circumference of circle
%
Input parameters:
%
r = Radius of circle
area= pi*r^2;
circum=
2* pi* r;

Application code:
%
Test Application of Area function
r=input('Enter
Radius for circle = ');
[area
circum]=AREA(r);
disp('Area
of circle is');area
disp('Circumference
of circle is');circum

Output:
Enter
Radius for circle = 6
Area of
circle is
area =
113.0973
Circumference
of circle is
circum =
37.6991

2. Program for finding distance between two Cartesian
coordinates:
Function:
function [dis]=dist(x1,x2,y1,y2)
%
Output:
%
dis = distance between two cartesian coordinates
%
Input:
%
x1 = x coordinate value of 1st point
%
x2 = x coordinate value of 2nd point
%
y1 = y coordinate value of 1st point
%
y2 = y coordinate value of 2nd point
dis=sqrt((x2x1)^2+(y2y1)^2);

Application code:
%
Application for dist function
x1=input('Enter x coordinate value for
1st point = ');
x2=input('Enter x coordinate value for
2nd point = ');
y1=input('Enter y coordinate value for
1st point = ');
y2=input('Enter y coordinate value for
2nd point = ');
dis=dist(x1,x2,y1,y2);
disp('Distance between two
cartesian coordinate points is');dis

Output:
Enter x
coordinate value for 1st point = 1
Enter x
coordinate value for 2nd point = 5
Enter y
coordinate value for 1st point = 1
Enter y
coordinate value for 2nd point = 4
Distance
between two Cartesian coordinate points is
dis = 5

No comments:
Post a Comment