You are on page 1of 7

> help function

FUNCTION Add new function.


New functions may be added to MATLAB's vocabulary if they
are expressed in terms of other existing functions. The
commands and functions that comprise the new function must
be put in a file whose name defines the name of the new
function, with a filename extension of '.m'. At the top of
the file must be a line that contains the syntax definition
for the new function. For example, the existence of a file
on disk called STAT.M with:
function [mean,stdev] = stat(x)
%STAT Interesting statistics.
n = length(x);
mean = sum(x) / n;
stdev = sqrt(sum((x - mean).^2)/n);
defines a new function called STAT that calculates the
mean and standard deviation of a vector. The variables
within the body of the function are all local variables.
See SCRIPT for procedures that work globally on the work-
space.
A subfunction that is visible to the other functions in the
same file is created by defining a new function with the FUNCTION
keyword after the body of the preceding function or subfunction.
For example, avg is a subfunction within the file STAT.M:
function [mean,stdev] = stat(x)
%STAT Interesting statistics.
n = length(x);
mean = avg(x,n);
stdev = sqrt(sum((x-avg(x,n)).^2)/n);
%-------------------------
function mean = avg(x,n)
%MEAN subfunction
mean = sum(x)/n;
Subfunctions are not visible outside the file where they are defined.
Normally functions return when the end of the function is reached.
A RETURN statement can be used to force an early return.
See also SCRIPT, RETURN, VARARGIN, VARARGOUT, NARGIN, NARGOUT,
INPUTNAME, MFILENAME.
>> sol=solve('s^3 + 2*s^2 + 16*s = 0')
sol =
[ 0]
[ -1+i*15^(1/2)]
[ -1-i*15^(1/2)]
>> help solve
SOLVE Symbolic solution of algebraic equations.
SOLVE('eqn1','eqn2',...,'eqnN')
SOLVE('eqn1','eqn2',...,'eqnN','var1,var2,...,varN')
SOLVE('eqn1','eqn2',...,'eqnN','var1','var2',...'varN')
The eqns are symbolic expressions or strings specifying equations. The
vars are symbolic variables or strings specifying the unknown variables.
SOLVE seeks zeros of the expressions or solutions of the equations.
If not specified, the unknowns in the system are determined by FINDSYM.
If no analytical solution is found and the number of equations equals
the number of dependent variables, a numeric solution is attempted.
Three different types of output are possible. For one equation and one
output, the resulting solution is returned, with multiple solutions to
a nonlinear equation in a symbolic vector. For several equations and
an equal number of outputs, the results are sorted in lexicographic
order and assigned to the outputs. For several equations and a single
output, a structure containing the solutions is returned.
Examples:
solve('p*sin(x) = r') chooses 'x' as the unknown and returns
ans =
asin(r/p)

[x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 = 0') returns


x =
[ 1]
[ 3]
y =
[ 1]
[ -3/2]
S = solve('x^2*y^2 - 2*x - 1 = 0','x^2 - y^2 - 1 = 0') returns
the solutions in a structure.
S =
x: [8x1 sym]
y: [8x1 sym]
[u,v] = solve('a*u^2 + v^2 = 0','u - v = 1') regards 'a' as a
parameter and solves the two equations for u and v.
S = solve('a*u^2 + v^2','u - v = 1','a,u') regards 'v' as a
parameter, solves the two equations, and returns S.a and S.u.
[a,u,v] = solve('a*u^2 + v^2','u - v = 1','a^2 - 5*a + 6') solves
the three equations for a, u and v.
[x,y] = solve('sin(x+y)-exp(x)*y = 0','x^2-y = 2') cannot find
an analytic solution, so returns a numeric solution.
See also DSOLVE.
Overloaded methods
help cgvalue/solve.m
help cgsubexpr/solve.m
help cgfeature/solve.m
help cgexpr/solve.m
help cgdivexpr/solve.m
help sym/solve.m
>> S = solve('a*u^2 + v^2','u - v = 1','a,u')
S =
a: [1x1 sym]
u: [1x1 sym]
>> a
??? Undefined function or variable 'a'.
>> a=
??? a=
|
Error: Expected a variable, function, or constant, found "end of line".
>> s
??? Undefined function or variable 's'.
>> S
S =
a: [1x1 sym]
u: [1x1 sym]
>> poly(sol)
??? Error using ==> sym/maple
Error, (in linalg[charmat]) matrix must be square
Error in ==> C:\MATLAB6p5\toolbox\symbolic\@sym\poly.m
On line 29 ==> p = maple('charpoly',A,x);
>> r=sol
r =
[ 0]
[ -1+i*15^(1/2)]
[ -1-i*15^(1/2)]
>> r=[0;-1+i*15^(1/2);-1-i*15^(1/2)]
r =
0
-1.0000 + 3.8730i
-1.0000 - 3.8730i
>> poly(sol)
??? Error using ==> sym/maple
Error, (in linalg[charmat]) matrix must be square
Error in ==> C:\MATLAB6p5\toolbox\symbolic\@sym\poly.m
On line 29 ==> p = maple('charpoly',A,x);
>> t=[1;2;3]
t =
1
2
3
>> poly(t)
ans =
1 -6 11 -6
>> fx=tf(ans,1)
Transfer function:
s^3 - 6 s^2 + 11 s - 6
>> polyfit([-pi, -3, -2, -1, -0.5, -0.05, -0.00000001, 0, 0.05, 1, 2, 3, pi],[0,
0, 0, 0, 0, 0, 0, pi, pi, pi, pi, pi, pi],8)
ans =
Columns 1 through 7
0.0002 -0.0047 -0.0037 0.1056 0.0224 -0.7851 -0.0251
Columns 8 through 9
2.5052 1.5125
>> polyfit([-pi, -3, -2, -1, -0.5, -0.05, -0.00000001, 0, 0.05, 1, 2, 3, pi],[0,
0, 0, 0, 0, 0, 0, pi, pi, pi, pi, pi, pi],25)
Warning: Polynomial is not unique; degree >= number of data points.
(Type "warning off MATLAB:polyfit:PolyNotUnique" to suppress this warning.)
> In C:\MATLAB6p5\toolbox\matlab\polyfun\polyfit.m at line 68
ans =
Columns 1 through 7
0.0036 0.0005 -0.0687 -0.0087 0.3350 0.0412 0
Columns 8 through 14
0 0 0 0 0 -29.9685 0
Columns 15 through 21
0 0 0 -57.3789 0 0 0
Columns 22 through 26
0 0 57.4173 31.2694 1.4994
>> fx=tf(ans,1)
Transfer function:
0.003556 s^25 + 0.0004572 s^24 - 0.06872 s^23 - 0.00868 s^22 + 0.335 s^21
+ 0.04119 s^20 - 29.97 s^13 - 57.38 s^8 + 57.42 s^2 + 31.27 s
+ 1.499

>> plot(fx)
??? Error using ==> plot
Conversion to double from tf is not possible.
>> x=[-pi, -3, -2, -1, -0.5, -0.05, -0.00000001, 0, 0.05, 1, 2, 3, pi]
x =
Columns 1 through 7
-3.1416 -3.0000 -2.0000 -1.0000 -0.5000 -0.0500 -0.0000
Columns 8 through 13
0 0.0500 1.0000 2.0000 3.0000 3.1416
>> y=[0, 0, 0, 0, 0, 0, 0, pi, pi, pi, pi, pi, pi]
y =
Columns 1 through 7
0 0 0 0 0 0 0
Columns 8 through 13
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416
>> plot(x,y)
>> help forrie
forrie.m not found.
>> help fourier
--- help for sym/fourier.m ---
FOURIER Fourier integral transform.
F = FOURIER(f) is the Fourier transform of the sym scalar f
with default independent variable x. The default return is
a function of w.
If f = f(w), then FOURIER returns a function of t: F = F(t).
By definition, F(w) = int(f(x)*exp(-i*w*x),x,-inf,inf), where
the integration above proceeds with respect to x (the symbolic
variable in f as determined by FINDSYM).
F = FOURIER(f,v) makes F a function of the sym v instead of
the default w:
FOURIER(f,v) <=> F(v) = int(f(x)*exp(-i*v*x),x,-inf,inf).
FOURIER(f,u,v) makes f a function of u instead of the
default x. The integration is then with respect to u.
FOURIER(f,u,v) <=> F(v) = int(f(u)*exp(-i*v*u),u,-inf,inf).
Examples:
syms t v w x
fourier(1/t) returns i*pi*(Heaviside(-w)-Heaviside(w))
fourier(exp(-x^2),x,t) returns pi^(1/2)*exp(-1/4*t^2)
fourier(exp(-t)*sym('Heaviside(t)'),v) returns 1/(1+i*v)
fourier(diff(sym('F(x)')),x,w) returns i*w*fourier(F(x),x,w)
See also IFOURIER, LAPLACE, ZTRANS.
>> help IFOURIER
--- help for sym/ifourier.m ---
IFOURIER Inverse Fourier integral transform.
f = IFOURIER(F) is the inverse Fourier transform of the scalar sym F
with default independent variable w. The default return is a
function of x. The inverse Fourier transform is applied to a
function of w and returns a function of x: F = F(w) => f = f(x).
If F = F(x), then IFOURIER returns a function of t: f = f(t). By
definition, f(x) = 1/(2*pi) * int(F(w)*exp(i*w*x),w,-inf,inf) and the
integration is taken with respect to w.
f = IFOURIER(F,u) makes f a function of u instead of the default x:
IFOURIER(F,u) <=> f(u) = 1/(2*pi) * int(F(w)*exp(i*w*u,w,-inf,inf).
Here u is a scalar sym (integration with respect to w).
f = IFOURIER(F,v,u) takes F to be a function of v instead of the
default w: IFOURIER(F,v,u) <=>
f(u) = 1/(2*pi) * int(F(v)*exp(i*v*u,v,-inf,inf),
integration with respect to v.
Examples:
syms t u w x
ifourier(w*exp(-3*w)*sym('Heaviside(w)')) returns 1/2/pi/(3-i*t)^2
ifourier(1/(1 + w^2),u) returns
1/2*exp(-u)*Heaviside(u)+1/2*exp(u)*Heaviside(-u)
ifourier(v/(1 + w^2),v,u) returns i/(1+w^2)*Dirac(1,-u)
ifourier(sym('fourier(f(x),x,w)'),w,x) returns f(x)
See also FOURIER, ILAPLACE, IZTRANS.
>> help fourier
--- help for sym/fourier.m ---
FOURIER Fourier integral transform.
F = FOURIER(f) is the Fourier transform of the sym scalar f
with default independent variable x. The default return is
a function of w.
If f = f(w), then FOURIER returns a function of t: F = F(t).
By definition, F(w) = int(f(x)*exp(-i*w*x),x,-inf,inf), where
the integration above proceeds with respect to x (the symbolic
variable in f as determined by FINDSYM).
F = FOURIER(f,v) makes F a function of the sym v instead of
the default w:
FOURIER(f,v) <=> F(v) = int(f(x)*exp(-i*v*x),x,-inf,inf).
FOURIER(f,u,v) makes f a function of u instead of the
default x. The integration is then with respect to u.
FOURIER(f,u,v) <=> F(v) = int(f(u)*exp(-i*v*u),u,-inf,inf).
Examples:
syms t v w x
fourier(1/t) returns i*pi*(Heaviside(-w)-Heaviside(w))
fourier(exp(-x^2),x,t) returns pi^(1/2)*exp(-1/4*t^2)
fourier(exp(-t)*sym('Heaviside(t)'),v) returns 1/(1+i*v)
fourier(diff(sym('F(x)')),x,w) returns i*w*fourier(F(x),x,w)
See also IFOURIER, LAPLACE, ZTRANS.
>>

You might also like