LAB 3 - Yola
LAB # 3
SCRIPT AND FUNCTION M-FILE
Objective
• To create function M file.
• To create script M file and calling another function M file.
Theory
In Matlab, executable files or exe files have extension .m and hence are called M-files. MATLAB allows you to place MATLAB commands in a simple text file and then tell MATLAB to open the file and evaluate the commands exactly as it would have if you had typed the commands at the MATLAB prompt. These files are called Script files or M-files.
The term “script” signifies that MATLAB simply reads from the script found in the file. The term M-file means that script filenames must end with the extension ‘.m’ as in, for example, example1.m
Matlab commands are actually built-in Functions. Function m-file contains word function in its first line and the comments in the next line (using % sign) are displayed when help of that function m-file is called. Arguments can be passed to a function file from another m-file (script).
Function file does not make use of workspace data globally; all the variables generated, except those passed out, remain local and get destroyed at the end of execution.
Script m-file makes use of workspace data globally; all the variables generated reside in workspace and are globally available. Script file can be executed from command window by just typing its name and pressing enter key.
TABLE 3.1 : FUNCTIONS AND THEIR DESCRIPTION
|FUNCTION |DESCRIPTION |
|Disp(variable name) |Displays results without identifying variable names |
|Echo |Controls Command window echoing of script file contents as they are executed. |
|Input |Prompts user for input |
|Keyboard |Temporarily gives controls to keyboard. (type return to return control to the executing |
| |script M-file) |
|Pause or pause(n) |Pause until user presses any keyboard key, or pause for n seconds and then continues |
|Waitforbuttonpress |Pauses until user presses mouse button or any keyboard key. |
3.1 CREATING A FUNCTION M-FILE
function y = myavg(z) % computes average of sequence z
L = length(z);
y = (sum(z)/L);
Note: Save the function file – myavg.m
Above program is typed in Matlab editor and is saved as ‘myavg.m’ in folder. If you type in command window help myavg comment line of this file (computes average of sequence z) is displayed. This function can also be called by another m-file (script).
3.2 CREATING A SCRIPT M-FILE AND CALLING FUNCTION M-FILE
% Script m-file mydata.m
p = [1:20];
q = [21:40];
disp (‘Average of p is’); myavg(p)
disp (‘Average of q is’); myavg(q) % calculates and displays average of p and q.
Note: Save the function file – mydata.m
Above program is typed in Matlab editor and is saved as ‘mydata.m’ in folder. Now if you type in command window mydata, it calls the function myavg two times to calculate average of p and q, and display the result.
[pic]
CLASS ACTIVITY
Show the output of all the given examples:
Example 1
% script M-file example1.m
erasers = 4 ; % number of each item
pads = 6;
tape = 2;
Items = erasers + pads+tape
Cost = erasers * 25 + pads * 52 + tape * 99
Avg_cost = Items / Cost
Note: Save the script file – example1.m
Example 2
% script M-file example2.m
erasers = 4 ; % number of each item
pads = 6;
tape = input('Enter the number of rolls of tape purchased > ');
Items = erasers + pads + tape
Cost = erasers * 25 + pads * 52 + tape * 99
Avg_cost = Items / Cost
Note: Save the script file – example2.m
Example 3
To see the effect of the echo command, add it to the script M-file and execute it
%script M-file example3.m
echo on
erasers = 4 ; % number of each item
pads = 6;
tape = input('Enter the number of rolls of tape purchased > ');
Items = erasers + pads + tape
Cost = erasers * 25 + pads * 52 + tape * 99
Avg_cost = Items / Cost
echo off
Note: Save the script file – example3.m
Example 4
%FINISH Confirm Desire for Quitting MATLAB
question = 'Are you Sure you want to Quit?';
button = questdlg(question,'Exit Request','Yes','No','No');
switch button
case 'No'
quit cancel; % how to cancel quitting!
case 'Yes'
disp('Exiting MATLAB');
save
quit force; % 'Yes' lets script and MATLAB end.
End
Note: Save the script file – finish.m
1. Write function M-file “mysum.m” to add two numbers. The syntax should be like:
y = mysum(c,d);
Answer:
c=input('Enter the number');
d=input('Enter the number');
y=m(c,d)
function y = m(z,x) % computes average of sequence z
y = z+x;
2. Write function M-file “mysqrt.m” and take the sum. The syntax should be like:
y = mysqrt(c,d);
Answer:
function y = mysqrt(c,d);
e=sqrt(c)
f=sqrt(d)
sum=e+f
HOME TASK
3. Write function M-file “cart2plr.m” to convert the Cartesian coordinates into Polar coordinates. The syntax should be like:
[r,t] = cart2plr(x,y);
[pic]
4. Write function M-file “quadeq.m” to solve the quadratic equation. The syntax should be like:
[r1,r2] = quadeq(a,b,c);
function myquadeq(a,b,c);
disp('The positive answer of quadratic eq');
d=-b
e=b^2
f=4*a*c
g=e-f
h=sqrt(g)
i=2*a
j=(d+h)
Answer=j/i
disp('The negative answer of quadratic eq');
l=-b
m=b^2
n=4*a*c
o=e-f
p=sqrt(g)
q=2*a
r=(d-h)
Answer2=j/i
[pic][pic]
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- activity 1 3 3 thermodynamics answer key
- activity 1.3.3 thermodynamics answer key
- act 1 3 3 thermodynamics answer key
- 3 3 t score bone density
- 3 by 3 linear system calculator
- 3 by 3 matrix solver
- 3 by 3 system solver
- 3 by 3 system calculator
- 3 equation 3 unknown solver
- 3 x 3 equation solver
- solving 3 x 3 linear systems calculator
- 3 3 repeating as a fraction