Dr. Young's Function m-file Tutorial
Write a function m-file that calculates kinetic energy. Name it Ek.m on your disk. I have copied my function m-file here.
function y = Ek(m,v)
% This function calculates the kinetic energy of a system, given its
% mass and velocity. For flowing streams, it calculates the kinetic
% energy transported by the flowing stream given the mass flow rate
% and the velocity. If m and v are arrays, then value returned will
% also be an array.
y = 0.5*m.*v.*v; % note use of array operators
Now write an m-file that uses this function to calculate the kinetic energy of a flowing stream. Name it pipeflow.m on your disk. I have copied my m-file here.
% m-file pipeflow.m
clear; % clear workspace
format compact; % single space
% This program calculates the kinetic energy transported by
% 2 kg/s of water flowing at 3 m/s.
mass_flow = 2.0; % kg/s
velocity = 3.0; % m/s
kinetic = Ek(mass_flow,velocity); % calls function m-file Ek
% Print it nicely
fprintf('\n')
fprintf('The kinetic energy transported is %g J/s.\n',kinetic)
When you type pipeflow in the Matlab command window, it should look like this:
» pipeflow
The kinetic energy transported is 9 J/s.
»
You can also use the function directly from the command window, without writing an m-file. Try this.
» Ek(2,3)
ans =
9
»
(Last modified on 4/15/98)