exp(x)
Let's see an example:
function output = poly(x)
% This function calculates the value of a third-order polynomial
output=3*x.^3+5*x.^2-2*x+1
end
function name(input)
: Calculates the result of a user-defined function, using the same syntax rules that apply to the built-in functions.Note: A user-defined function can be called if it is in the current folder!
x=3;
y=poly(x);
y =
121
function output = g(x,y)
% This function multiplies x and y together
% x and y must be the same size matrices
a = x.*y
output = a
end
g
x
and y
output
function [dist, vel, accel] = motion(t)
% This function calculates the distance velocity, and acceleration of a particular car for ...
given value of t assuming all 3 parameters are initially 0.
accel = 0.5.*t
vel = t.^2/4
dist = t.^3/12
end
motion
t
accel
, vel
, and dist
function result = distance(t)
% This function calculates the distance a falling object travels due to gravity
g = 9.8 % meter per second squared
result = 1/2*g*t.^2
end
t
, g
and result are local variablesglobal G; G=9.8;
function result = distance(t)
% This function calculates the distance a falling object travels due to gravity
global G
result = 1/2*G*t.^2
end
G
is a global variablenargin('function'): Determines the number of input arguments in either a user-defined function or a built-in function
nargin('exp')
ans = 1
nargin('g')
ans = 2
nargout('function'): Determines the number of outputs from the function
nargin('sin')
ans = 1
nargin('motion')
ans = 3
function_handle
ln = @(x)log(x)
@
symbol alerts MATLAB that ln
is a function@
symbol, the input to the function is listed in paranthesesa = 1.3;
b = 0.2;
c = 30;
parabola = @(x) a*x.^2+b*x+c;
parabola(2)
c = @(a,b) sin(a*b);
c(1,3)
c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi)
Two outputs, x
and y
, are obtained as a result of this function.
g = @(c) (integral(@(x)(x.^2 + c*x + 1),0,1));
g(2)
One example of a MATLAB built-in function function is the function plot: fplot
This function requires two inputs: a function or a function handle, and a range over which to plot.
poly5 = @(x) - 5*x.^5 + 400*x.^4 + 3*x.^3 + 20*x.^2 - x + 5;
fplot(poly5, [-30,90])
fzero
. It tries to find a point x
where $function(x) = 0$. This function requires a non-linear function, and an initial point.fzero(poly5, 75)
function [addition result, subtraction result] = subfunction demo(x, y)
% This function both adds and subtracts the elements stored in two arrays
addition result = add(x,y);
subtraction result = subtract(x,y);
function result = add(x,y) % subfunction plus
result = x + y;
end
function output = subtract(x,y) %subfunction minus
output = x - y;
end
end
disp
: Displays the result of an expression or a stringdisp('Hello')
disp(exp(2))
x=12;
y=-3;
disp(x+y)
fprintf
: Prints formatted output to the screencows = 5;
fprintf('There are %f cows in the pasture \n', cows)
fprintf('There are %d cows in the pasture \n', cows)
fprintf('The value is %d, for sure! \n', cows)
fprintf('%10.3f\n', pi)
fprintf('%10.4e\n', pi)
fprintf('%-5.0f\n%5.0f', 9, 103)
fprintf
command until it uses all the values in the matrix.x=1:5;
fprintf('%10.2f\n', x);
feet = 1:3;
inches = feet.*12;
table = [feet;inches]
fprintf('%7.0f %15.3f \n', table);
Code | Format |
---|---|
%f | Fixed-point notation |
%e | Exponential notation |
%d | Decimal notation - does not include trailing zeros if the value displayed is an integer. If the number includes a fractional component, it is displayed using exponential notation. |
%g | Whichever is shorter, %f or %e |
%c | Character information (displays one character at a time) |
%s | String of characters (displays the entire string) |
sprintf
function is similar to fprintf
, but instead of just sending the result of the formatted string to the command window, sprintf
assigns it a name and sends it to the command window.fprintf('Some example output is %10.2f \n', pi*1000)
sprintf('Some example output is %10.2f \n', pi*1000)
MATLAB offers a technique for entering ordered pairs of x
and y
values graphically.
The ginput
command allows the user to select points from a figure window and converts the points into the appropriate x and y coordinates.
[x,y] = ginput(n)
: Requests the user to select n
points from the figure windows.[x,y] = ginput
: Accepts points until the return key is pressed.input('text-string')
: Allows the user to input values of a matrix from the keyboard while the program is running.It displays a text string in the command window and then waits for the user to provide the requested input.
x = input('Enter a value')
If a character or string input is desired, 's'
must be added after the prompt:
y = input('Enter your name', 's')
The Import Wizard determines what kind of data is in the file and suggests ways to represent the data in MATLAB.
xlsread('filename.xls')
: Imports data from Excel
xlswrite('filename.xls', M)
: Exports data to Excel