clear; clc; close ; format ; all

advertisement
clear; clc; close all; format shortg;
%Today is all about defining your own functions
%We have two different ways to define new functions
%1 - Anonymous function
%2 - Write a new function file
%1 - Anonymous functions:
%You can use this command in your normal script file
%Example: Convert Meters to inches
%Format: NAME = @(INPUT VARIABLE(S)) FORMULA
MetToInch = @(x) x*100/2.54; %converts to cm then to inches
x = MetToInch(1);
fprintf('1 meter = %f inches\n',x);
%Example: Convert polar coordinates to rectangular coordinates
xcord = @(r,theta) r*cos(theta);
ycord = @(r,theta) r*sin(theta);
r = sqrt(2); theta = pi/4;
P = [xcord(r,theta) ycord(r,theta)]
%Anonymous functions work well, when you have a simple calculation(s)
%Notice that they cannot output two different variables.
%We needed two different Anonymous Functions to do the above conversion
%Let's switch to Function Files
%Function File is a separate m-file
%This means you'll need to turn in both your problem file
%and your function file
%Go to your home tab and click the arrow under the new button
%Select "New Function"
%This will open an m-file that is already formatted for you
%See bottom of page for all the function files
%Example: Convert Polar coordinates to rectangular coordinates
[x,y] = PolToRec(r,theta);
P = [x,y]
%Here note that if you want to save all outputs from the function you need
%to define all outputs to start with.
%Example: Distance between two points in 3-D
A = [1,2,3]; B = [0,0,5];
d = Dist(A,B);
fprintf('The distance between A and B is %g',d)
%Example: Plotting a circle
xc = 1; yc = -1; r = 2;
PlotCircle(xc,yc,r);
1
%Hints and Other Notes:
%Look at the example on page 229
%Problem 2 - You can use an Anonymous Function or a Function File
%When plotting you can put the two different plots on the same graph
%or on separate graphs
%Use the command, figure, to open a new plot window if plotting on
%separate graphs
%Problems 4, 12, 20, and 21 you must a Function File
%All graphs must have titles and must label each axis
%Refer to previous assignments or pg: 145-148 on how to format the plot
%Refer to previous assignments or Pg: 156-157 on how to plot polar
%For problem 12 we did a problem in previous assignments that caclulated
%the angle they're asking for
%Refer to that problem if you forgot the steps for that calculation
%For problem 20 try writing x and y as parametric equations
%See my above example for an idea
%Problem 21: See sec 13.4 in your calc book for the formula
%to convert polar to rectangular (Cartesian) coordinates
%When turning in your function files, you will get an error message when
%publishing. This is ok. Turn in with that error message
1 meter = 39.370079 inches
P =
1
1
1
1
P =
The distance between A and B is 3
2
Published with MATLAB® R2014a
3
function [x,y] = PolToRec(r,theta)
%UNTITLED12 Summary of this function goes here
%
Detailed explanation goes here
x = r*cos(theta);
y = r*sin(theta);
end
Error using PolToRec (line 4)
Not enough input arguments.
Published with MATLAB® R2014a
1
function [d] = Dist(A,B)
%UNTITLED14 Summary of this function goes here
%
Detailed explanation goes here
delx = A(1)-B(1);
dely = A(2)-B(2);
delz = A(3)-B(3);
d = sqrt(delx^2+dely^2+delz^2);
end
Error using Dist (line 4)
Not enough input arguments.
Published with MATLAB® R2014a
1
function PlotCircle(xc,yc,r)
%UNTITLED15 Summary of this function goes here
%
Detailed explanation goes here
t = [0:.01:2*pi];
x = xc+r*cos(t); y = yc+r*sin(t);
plot(x,y)
end
Error using PlotCircle (line 5)
Not enough input arguments.
Published with MATLAB® R2014a
1
Download