Matlab 10 Slide
Matlab 10 Slide
The sym function can be used to create The syms function enables you to combine more
“symbolic objects” in MATLAB. than one such statement into a single statement.
If the input argument to sym is a string, the For example, typing >> syms x is equivalent to typing
result is a symbolic number or variable. If the >> x = sym(’x’), and typing >> syms x y u v
input argument is a numeric scalar or matrix, the creates the four symbolic variables x, y, u, and v.
result is a symbolic representation of the given You can use the sym function to create symbolic
constants by using a numerical value for the
numeric values.
argument. For example, typing
For example, typing >> x = sym(’x’) creates the >> pi = sym(’pi’)
symbolic variable with name x, and typing >> fraction = sym(’1/3’)
>> y = sym(’y’) creates a symbolic variable and
named y. Typing >> x = sym(’x’,’real’) tells >> sqroot2 = sym(’sqrt(2)’)
MATLAB to assume that x is real. Typing create symbolic constants that avoid the floating-
>> x = sym(’x’,’unreal’) tells MATLAB to point approximations inherent in the values of S, 1/3,
assume that x is not real. and 2.
Z.R.K Z.R.K
10-
10-3 10-
10-4
If you want to tell MATLAB that f is a function Use the subs and double functions to
of the variable t, type f = sym(’f(t)’).
Thereafter, f behaves like a function of t, and evaluate an expression numerically. Use
subs(E,old,new) to replace old with a
you can manipulate it with the toolbox
numeric value new in the expression E. The
commands. For example, to create a new
function g(t) f (t 2) f (t), the session is result is of class double. For example,
>> syms t >> syms x
>> f = sym(’f(t)’); >> E = x^2+6*x+7;
>> G = subs(E,x,2)
>> g = subs(f,t,t+2)-f
G =
g = 23
f(t+2)-f(t) >> class(G)
Once a specific function is defined for f (t), the ans =
function g (t) will be available. double
10-
10-11 Z.R.K
10-
10-12 Z.R.K More? See pages 592-593.
If the expression contains more than one variable, the The function diff(E,n) returns the nth derivative
diff function operates on the variable x, or the variable of the expression E with respect to the default
closest to x, unless told to do otherwise. When there is independent variable.
more than one variable, the diff function computes the
partial derivative.
>> syms x
>> diff(x^3,2)
>> diff(sin(x*y))
ans =
ans =
6*x
cos(x*y)*y
The function diff(E,v,n) returns the nth
The function diff(E,v) returns the derivative of derivative of the expression E with respect to the
the expression E with respect to the variable v. variable v.
>> syms x y >> syms x y
>> diff(x*sin(x*y),y) >> diff(x*sin(x*y),y,2)
ans = ans =
x^2*cos(x*y) -x^3*sin(x*y)
10-
10-19 Z.R.K
10-
10-20 Z.R.K More? See pages 603-606.
Integration with the int function. The form int(E,v) returns the integral of the
>> syms x expression E with respect to the variable v.
>> int(2*x) >> syms n x
ans = >> int(x^n,n)
x^2 ans =
The function int(E) returns the integral of the expression
1/log(x)*x^n
E with respect to the default independent variable.
>> syms n x y The form int(E,a,b) returns the integral of
>> int(x^n) the expression E with respect to the default
ans = independent variable evaluated over the
x^(n+1)/(n+1) interval [a, b], where a and b are numeric
>> int(1/x) expressions.
ans =
>> syms x
log(x)
>> int(cos(x)) >> int(x^2,2,5)
ans = ans =
sin(x) 39
Z.R.K Z.R.K
10-
10-21 10-
10-22
For example,
LAPLACE INVERSE LAPLACE
>> dsolve(’Dx=3*x+4*y’, ...
’Dy=-4*x+3*y’, ’x(0)=0’,’y(0)=1’) TRANSFORM TRANSFORM