More Function Examples
·
Here
is a function you will use! It
converts an angle in degrees to radians.
Copy-and-paste this into a new m-file (deg2rad.m). Be sure to remove those unwanted spaces
between the comment lines on top in your m-file.
function rad=deg2rad(deg)
%
% rad=deg2rad(deg)
%
% Converts an angle in degrees to
radians
% deg – input angle in degrees
% rad – angle converted to radians
%
% Author: …
% Date: …
% 360 degrees = 2 pi radians
rad=(deg/360)*2*pi;
·
Test
it at the command prompt like so...
>> r1=deg2rad(180)
>> r2=deg2rad(360)
>> r3=deg2rad(0)
·
Now,
write a new function that converts the other way called rad2deg( ). This should be
in a new m-file called rad2deg.m. Model it after the function code above (copy
this code into a new m-file). Be sure,
however, to modify the function line, comment lines, and the actual
mathematical expression inside the function.
·
Test
it like so at the command prompt…
>> rad2deg(deg2rad(45))
·
What
should the output be?
·
The
following function computes some parameters for a circle, given its radius
function [a,c,d]=circle(r)
%
% [a,c,d]=circle(r)
%
% Computes the area, circumference, and
diameter of
% a circle with radius r.
%
% a – output area of circle
% c – output circumference of circle
% d – output diameter of circle
% r – input radius of circle
%
% Author: …
% Date: …
a=pi*r^2;
c=2*pi*r;
d=r*2;
·
Modify
this scalar function so that it will work with an array input r then try this at the command prompt…
>> r=linspace(0,10,1000);
>> [a,c,d]=circle(r);
>> plot(r,a,r,c,r,d)
>> legend('Area', 'Circumference', 'Diameter');
>> xlabel('Radius');
·
This
function compute the distance and velocity of a dropped object
function [dist,vel]=drop(g,v0,t)
%
% function [dist,vel]=drop(g,v0,t)
%
% Computes the distance and velocity of
a dropped object
% given the acceleration due to gravity,
initial velocity, and time after drop.
% Neglects air resistance!
%
% dist – output distance traveled after
drop
% vel – output velocity of object at
time t
% g – input acceleration due to gravity:
g=32 ft/s^2 or g=9.8 m/s^2.
% v0 – input initial velocity
% t – input time after drop (can be
scalar or vector).
%
% Author: …
% Date: …
% velocity is integral (with respect to t) of acceleration + initial velocity
vel=g*t+v0;
% distance is integral of velocity (with
respect to t)
dist=.5*g*t.^2+v0*t;
·
Let’s
take our function for a spin… If you drop a coin off a building and it takes
3.2 seconds to hit the ground, how tall was the building? How fast was it going when it hit the
ground?
>> [height,velocity]=drop(32,0,3.2)
·
Let’s
go farther…
>> t=linspace(0,10,1000);
>> [d,v]=drop(32,0,t);
>> figure(1)
>> plot(t,v)
>> xlabel('Time (s) '); ylabel('Velocity (ft/s) ');
>> figure(2)
>> plot(t,d)
>> xlabel('Time (s) '); ylabel('Distance (ft) ');
·
The
variables you use inside a function are local to that function and are not “known”
to the calling m-file or workspace (if calling the function from the command
line). The calling m-file or workspace
will only “know” about the variables specifically declared as output variables
in the function statement.
·
When
you call a function, the output variables can be named anything, as can the
input variables. It is the order only
that matters.