0% found this document useful (0 votes)
17 views2 pages

MATLAB Functions: Built-in & User Defined

Uploaded by

Shivam Pal
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)
17 views2 pages

MATLAB Functions: Built-in & User Defined

Uploaded by

Shivam Pal
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

Tutorial 1. 2025-26, Ist Sem. ME 502 Engg. Compt. Lab.

DEPARTMENT OF MECHANICAL ENGINEERING

INDIAN INSTITUTE OF TECHNOLOGY - GUWAHATI

Hands on MATLAB® : Builtin and User Defined Functions

A. MATLAB built-in functions. MATLAB has many built-in mathematical functions, includ-
ing trigonometric, logarithmic, and hyperbolic functions, as well as functions for processing
arrays. Following are the some of the common mathematical functions

• Exponential: x
√ exp(x) - For computing the exponential e ; sqrt(x) - For computing
square root x. Question: Find square root of 5, 7, 15, 25. Also, find e2 .
• Logarithmic: log(x) - Natural logarithm; ln x. log10(x) - Common (base-10)
logarithm; logx = log10 x. Question: For several values of x and y, confirm that ln(xy)
= ln x + ln y.
• Complex: abs(x) - Absolute value; x. angle(x) - Angle of a complex number x.
conj(x) - Complex conjugate. imag(x) - Imaginary part of a complex number x.
real(x) - Real part of a complex number x. √ Question: Find the magnitude, angle,
real part, and imaginary part of the number 2 + 6i . (Answers: magnitude = 2.5149,
angle = 0.6245 rad, real part = 2.0402, imaginary part = 1.4705).
• Numeric: ceil(x) - Round to the nearest integer toward ∞. fix(x) - Round to
the nearest integer toward zero. floor(x) - Round to the nearest integer toward -∞.
round(x) - Round toward the nearest integer. sign(x) - Signum function: +1 if
x > 0; 0 if x = 0; -1 if x < 0.
• Trigonometric: cos(x) - Cosine; cos x. cot(x) - Cotangent; cot x. csc(x) -
Cosecant; csc x. sec(x) - Secant; sec x. sin(x) - Sine; sin x. tan(x) - Tangent; tan
x. (NOTE: x should be in radians.) Question : For several values of x in the range 0
≤ x ≤ 2 , confirm that sin2 x + cos2 x = 1.
• Inverse trigonometric - acos(x) - Inverse cosine; arccos x = cos−1 x. acot(x) -
Inverse cotangent; arccot x = cot−1 x. acsc(x) - Inverse cosecant; arccsc x = csc−1 x.
asec(x) - Inverse secant; arcsec x = sec−1 x. asin(x) Inverse sine; arcsin x = sin−1 x.
atan(x) - Inverse tangent; arctan x = tan−1 x. (NOTE: x should be in radians.)
Question : For several values of x in the range 0 ≤ x ≤ 2 , confirm that sin−1 x +
π
cos−1 x = .
2
B. User Defined Functions MATLAB also provides option to have user defined functions -
functions to perform tasks which are not available as built-in [Link] a script file,
all the variables in a function file are local variables, which means their values are available
only within the function. Function files are useful when you need to repeat a set of commands
several times. They are the building blocks of larger programs.
To create a function file, open the Editor /Debugger. The first line in a function must begin
with a function definition line that has a list of inputs and outputs. This line distinguishes
a function file from a script file. Its syntax is as follows:
function [out1, out2,.., outN] = function name(inp1, inp2,.., inpM)
The output variables are those variables whose values are computed by the function, using
the given values of the input variables. Note that the output variables are enclosed in square
brackets (which are optional if there is only one output), while the input variables must be
enclosed with parentheses. The function name should be the same as the file name in
which it is saved (with the .m extension). The function is ”called” by typing its name at
the command line. The word function in the function definition line must be lowercase.
Before naming a function, you can use the exist function to see if another function has the
same name.

Introduction to MatLab® Page 1 of 2


Tutorial 1. 2025-26, Ist Sem. ME 502 Engg. Compt. Lab.

Question: (a) Write a function to compute the u = 3 x and v = sin(x) + y 2 , and w =


x + y. The input to the function are x and y. The output are u, v, w. (b) Write a function
to compute the area of a planar triangle given the x and y coordinates of its three vertices.
(c) Using a function find the first n numbers in Fibonacci series.

Introduction to MatLab® Page 2 of 2

You might also like