Experiment 2
Experiment 2
Experiment No. 2:
M-FILE, FUNCTIONS AND FUNCTION FILES
Introduction
MATLAB is a programming language, as well as an interactive computational environment. Files that include
code in the MATLAB language are called M-files.
There are two types of M-files: script files and function files.
A script file consists of a sequence of normal MATLAB statements. If the file has the filename, say, rotate.m,
then the MATLAB command rotate will cause the statements in the file to be executed. Variables in a script file
are global and will change the value of variables of the same name in the environment of the current MATLAB
session.
Function files provide extensibility to MATLAB. We can create new functions specific to your problem which will
then have the same status as other MATLAB functions. Variables in a function file are by default local. However,
we can declare a variable to be global if you wish.
Objective
1. To make,save and call M-file.
2. To define and call a function in M-file.
3. Learn to access local and global variables.
C=46;
[K] = CtoK(C)
K = 319
Issues
We did not face any issues during the experiment.
Conlcusion
• We can make functions in the M-file and use them in our code to by calling the functions.
• We can also make user defined functions to perform our respective tasks.
1
Applications
An m-file, or script file, is a simple text file where you can place MATLAB commands. When the file is
run, MATLAB reads the commands and executes them exactly as it would if you had typed each command
sequentially at the MATLAB prompt. All m-file names must end with the extension '.
r=30;
R=45;
V_silo=120000;
[h_silo,sa] = Silo(R,r,V_silo)
h_silo = 39.6885
sa = 9.6411e+03
Problem-2(a)
x=6;
[y] = problem1(x)
y = 4.5401
Problem-2(b)
x=1;
[y] = problem1(x)
y = 0.7071
x=3;
[y] = problem1(x)
y = 3.0307
x=5;
[y] = problem1(x)
y = 4.1347
x=7;
[y] = problem1(x)
y = 4.8971
x=9;
[y] = problem1(x)
2
y = 5.5197
x=11;
[y] = problem1(x)
y = 6.0638
Problem-3
Cel1 = 4.4444
Cel2 = 33.3333
del_L = 0.0030
Problem-4 (a)
cm = 172.7200
kg = 79.2750
Problem-4 (b)
in=71;
lb=200;
[cm,kg] = STtoSI(in,lb)
cm = 180.3400
kg = 90.6000