0% found this document useful (0 votes)
9 views

functions_8

Matlap file

Uploaded by

saleh5hr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

functions_8

Matlap file

Uploaded by

saleh5hr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

User Defined Function

https://www.mathworks.com/help/matlab/ref/function.html

https://majackmer85.files.wordpress.com/2014/11/tercera-edtion-matlab.pdf , (CHAPTER 6)

Input Data => | Function | => Output Data

function [out1,out2,out3,..] = functionName(in1,in2,in3,..)


exms:

calc_average(2,4)

ans = 3

n=1:90;
vect_average(n)

ans = 45.5000

Function types
• Local Functions
• Nested Functions
• Private Functions
• Anonymous Functions

Define a function in a file named stat.m that returns the mean and
standard deviation of an input vector.
values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)

ave = 47.3400
stdev = 29.4124

Exr: write a function for the following equation

where

Script Files
https://www.mathworks.com/help/matlab/matlab_prog/create-scripts.html

1
Scripts vs. Functions
https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html

Create and Run Sections in Code


https://www.mathworks.com/help/matlab/matlab_prog/create-and-run-sections.html

function [av] = calc_average(x,y)


% calc_average calculates the avreage of two numbers
% x: first number
% y: second number
av = (x+y)/2;
end

function [avg] = vect_average(x)


%UNTITLED Summary of this function goes here
% Calculate the avreage of vector numbers
% x: vector number
avg = sum(x(:))/numel(x);
end

function [m,s] = stat(x)


n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end

You might also like