Functions:-
The functions can be defined as the set of statement or script which can be executed and run discretely. The functions can perform a task with other programs combined. Functions should be defined n a different file.
The name of the MATLAB function file name should be the same as the function name.
Syntax:-
function [out_arg1,out_arg2,.......,out_argN] = foldername(in_arg1, in_arg2,.......,in_argN)
out_arg = Output Argument
in_arg = Input Argument
Function Operation On Variable:-
It is of 2 types.
1. Local Workspace:- When a function is operating on variables in its own space is known as Local Workspace.
2. Base Workspace:- When a function is operating on variables in Command Prompt is known as Base Workspace.
How to write a Function File:-
Step: 1
Create a file with the .m extension.
Step: 2
The name of the file and function name should be the same and it is important.
Step: 3
The first line of a function with the keyword function because it arranges the argument order.
Step: 4
Then use the function name directly in the command window or use the function in a script than execute it.
For Example:
WAP to find the power of a number using function.
%first create a file
named 'findsum'
function s = findsum(a,b,c)
s=a+b+c;
end
|
After writing a program in the editor save the function file. Then create a new file name it myTest.m and in the editor write following lines,
a=input('Enter value of a = ');
b=input('Enter value of b = ');
c=input('Enter value of c = ');
findsum(a,b,c)
|
>> myTest
Enter value of a = 8
Enter value of b = 5
Enter value of c = 2
ans =
15
>>
|
According to the writing style of a function, it can be classified into 3 types. Such as:
- Anonymous Functions
- Primary & Sub Functions
- Nested Functions
1.Anonymous Function:-
The Anonymous Function is an inline function i.e. the functions are of this category are defined in a single MATLAB statement which is consist of a single expression.
An Anonymous Function can be called directly in the command window with a function / Script.
The main benefit of this function is you don't have to create any specific files for these functions.
Syntax:-
function_name = @(arg_list)expression
arg_list = List of Argument
For Example:
WAP to create an anonymous function named power and provide two variables input and return
clc;
close all;
clear all;
power = @ (x, n) x.^n;
result1 = power(2, 3)
result2 = power(25, 0.5)
result3 = power(100, -10)
result4 = power (4, 1)
|
Save the file and click on run.
8
result2 =
5
result3 =
1.0000e-20
result4 =
4
>>
|
2.Primary & Sub Function:-
The Primary Function can be called from outside of the file which defines them, either from the command line or from other function.
The Sub Function can only visible to the primary function and other sub-functions of the function files that can define them. And Sub Function can not be called from outside of the file which defines them, either from the command line or from other function
For Example:
WAP to Find Roots using Primary & Sub-function.
Let create a function file named findroots.m to calculate a quadratic equation.
findroots.m is a Primary Function and disc.m is sub-function to find discriminate.
%Sub-Function
function dis = disc(a,b,c)
dis = sqrt(b^2 - 4* a* c);
end
|
After disc.m sub-function file successfully create a primary time named findroots.m and use the sub-function in it.
%primary
function
function [x1,x2] = findroots(a,b,c)
d =
disc(a,b,c);
x1 = (-b + d)
/ (2* a);
x2 = (-b - d)
/ (2* a);
end
|
Save it and in the command window type findroots(2,4,-4) and give enter. after successfully executing, the result is as follows:
>> findroots(2,4,-4)
ans =
0.7321
>>
|
In the nested function, it allows to write a function in another function.
For Example:
WAP to Find Roots using Nested function.
function [x1,x2] =
findroots2(a,b,c)
function disc
d = sqrt(b^2 - 4* a* c);
end
disc;
x1 = (-b + d)
/ (2* a);
x2 = (-b - d)
/ (2* a);
end
|
Save the file and use disc.m here and go to the command window and type findroots2(2,4,-4) and give enter. after successfully executing, the result is as follows:
>> findroots2(2,4,-4)
ans =
0.7321
>>
|
According to the declaration type of a function, it can be classified into 2 types. Such as:
- Private Function
- Global Variables
1.Private Function:-
From the name itself can be known as that while writing a function program sub-function program files can be treated as privet function.
To declare a function file we have to create subfolder and store those files in it. They are only visible to the parent only.
For Example:
WAP to Find Roots using Private Function.
First create a findroots.m file and save it.
function [x1,x2] =
findroots3(a,b,c)
d =
disc(a,b,c);
x1 = (-b + d)
/ (2* a);
x2 = (-b - d)
/ (2* a);
end
|
Create a privet folder and copy the disc.m file in it.
%in the
Private file
function dis = disc(a,b,c)
dis = sqrt(b^2 - 4* a* c);
end
|
Go to the command window and type findroots3(2,4,-4) and give enter. after successfully executing, the result is as follows:
>> findroots3(2,4,-4)
ans =
0.7321
>>
|
2. Global Variables:-
The variables which are share in more than once function is known as Global Variables. Global variables can be used everywhere we just want to declare the variable global.
The global Variable must be a capital letter to distinguish easily.
For Example:
WAP to Find Average using Global Variable.
function avg =
average(nums)
global TOTAL
avg = sum (num
s)/TOTAL;
end
|
clc;
clear all;
close all;
global TOTAL;
TOTAL = 10;
n = [34, 45,
25, 45, 33, 19, 40, 34, 38, 42];
av =
average(n)
|
Save it and Execute it.
av =
35.5000
>>
|
Post a Comment
If you have any doubts regarding this topic...
feel free to ask...