Matlab handles symbolic operations

Table of contents

1. Basic symbolic operations (mainly including the definition of symbolic expressions, factorization, expansion, and merging of the same type)

Example 1.1 (definition of symbolic expressions, that is, unary functions or multivariate functions in mathematics)

Example 1.2 (Factorization)

 Example 1.3 (merging similar items)

Example 1.4 (finding the expansion)

Example 1.5

 2. Symbolic Calculus

2.1 Finding the limit

 2.2 Sum of symbolic series

 2.3 Signed Differentiation

 2.4 Symbolic integrals

 2.5 Taylor expansion

 3. Symbolic equation solving

3.1 Solving algebraic equations (help can be used to refer to matlab help documentation)

3.2 Solving Differential Equations


1. Basic symbolic operations (mainly including the definition of symbolic expressions, factorization, expansion, and merging of the same type)

The main commands included are as follows:

syms: Define symbolic variables (Note here, when defining multiple symbolic variables, spaces must be used in the middle, commas cannot be used, otherwise an error will be reported, the reason is that two matlab statements are separated by commas, and the right side of the comma is equivalent to displaying an unidentified Defined variables. An error will occur.)

vpa: Arbitrary precision computing

factor: factorization

collect: Merge similar items

expand: find the expansion

numden: extract numerator and denominator

Example 1.1 (definition of symbolic expressions, that is, unary functions or multivariate functions in mathematics)

Define a symbolic expression z=x^2+e^{x+y}-ylnx-3and find the value of z when x=2, y=4.

syms x positive;%对数真数必须要大于0
syms y;%定义符号变量
z=x^2+exp(x+y)-y*log(x)-3;%定义符号表达式
n=subs(z,[x,y],[2,4]);%令x=2,y=4
vpa(n,6)%保留六位有效数字

result:

Of course, the function handle object can also be used for calculation here.

z=@(x,y)x^2+exp(x+y)-y*log(x)-3;%定义函数句柄对象(可以加上“点运算”,有可能输入变量是矩阵。)
double(z(2,4))

 operation result:

 

Example 1.2 (Factorization)

x^4-5x^3+5x^2+5x-6

syms x;
y=x^4-5*x^3+5*x^2+5*x-6;
factor(y)

result :

 Example 1.3 (merging similar items)

syms x;
y=(x+1)^3+(x+1)^2+5*x-6;
collect(y)%合并同类项

result:

Example 1.4 (finding the expansion)

y=(x+1)^5

syms x;
y=(x+1)^5;
expand(y)%合并同类项

result:

Example 1.5

numerator f(x,y)=\frac{1}{x^3-1}+\frac{1}{x^2+y+1}+\frac{1}{x+y+1}+8and denominator

syms x y;
f=1/(x^3-1)+1/(x^2+y+1)+1/(x+y+1)+8;
[a,b]=numden(f)%其中a为分子,b为分母

result:

 2. Symbolic Calculus

2.1 Finding the limit

Matlab's function library does not provide a numerical function for directly seeking the limit of a function, but the function limit for directly seeking the limit of a function is provided in the symbolic calculation toolbox .

limit(f,x,a): Find the limit value of the symbolic function f(x) when x tends to the constant a, f is a symbolic expression or a string expression, and x must be a symbolic variable.

limit(f,a): Find the limit value of the sign function f(x) when x tends to a

limit(f): Find the limit value of the sign function f(x) when x tends to 0

limit(f,x,a,'right') : Find the limit value of the sign function f(x) when x tends to a from the right

limit(f,x,a,'left') : Find the limit value of the sign function f(x) when x tends to a from the right

Example 2.1:

syms x;
f1=sin(x)/x;
f2=(tan(x)-sin(x))/x^3;
limit(f1, x,0) 
limit(f2,x,0) 

 result:

 2.2 Sum of symbolic series

symsum(S,v,a,b): Find the series sum of the symbolic expression S when v goes from a to b.

Example 2.2

syms n x;
f1=1/2^n;
f2=1/n^2;
f3=x^n/(n*2^n);
r1=symsum(f1,n,1,inf)
r2=symsum(f2,n,1,inf)
r3=symsum(f3,n,1,inf)
disp('r1=',r1)
disp('r2=',r2)
disp('r3=',r3)

 result:

 2.3 Signed Differentiation

diff(f): Find the first derivative of the symbolic expression f with respect to the free variable

diff(f,n): Find the n-order derivative of f with respect to the free variable, which is determined by findsym(f,1).

diff(f,v): Find the first derivative of f with respect to the free variable v

diff(f,v,n): Find the n-order derivative of f with respect to the free variable v

f can be a symbolic expression or a string expression, and v can be a variable name string or a symbolic variable.

Example 2.3 (Derivative)

 

Example 2.4 Finding Partial Derivatives

syms x y
u=x*log(x+y);
d1=diff(u,x,2)
d2=diff(u,y,2)
d3=diff(diff(u,x),y)

 result:

 2.4 Symbolic integrals

Function int can find both definite integral and indefinite integral.

int(S,v,a,b) : Find the definite integral of the symbolic expression S with respect to the variable v on the interval [a,b]

int(S,v) : Find the indefinite integral of the symbolic expression S with respect to the variable v.

When v is omitted, it is automatically determined by findsym(S,1).

Example 2.5 Calculate the following indefinite integral

%% 1)
syms x 
y=x^2*asin(x);
int(y,x)

 result:

%% 2)
syms x 
y=x^2/(x+1);
int(y,x)

 result:

%% 3)
syms x 
y=sin(2*x)/sqrt(1+sin(x)^2);
int(y,x)

result:

 Example 2.6

syms x;
y1=sin(log(x));
y2=x^3*sin(x)^2/(x^4+2*x^2+1);
y3=sqrt(sin(x)^3-sin(x)^5);
r1=double(int(y1,x,1,exp(1)));
r2=double(int(y2,x,-1,1));
r3=double(int(y3,x,0,pi));
disp('r1=')
disp(r1)
disp('r2=')
disp(r2)
disp('r3=')
disp(r3)

result:

 2.5 Taylor expansion

In Matlab, the function taylor can be used to perform Taylor series expansion on the function. The basic format is:

Taylor(f,x,a) : Perform 5th-order Taylor expansion on f about x at point a

Taylor(f,x) : Perform a 5th-order Taylor expansion on f with respect to x at 0

Taylor(f,x,a,'Order',n+1) : Perform n-order Taylor expansion on f about x at point a

Example 2.7 Find the 6th Maclaurin expansion of y=x*log(1+x)

syms x;
y=x*log(1+x);
taylor(y,x,0,'Order',7)

result:

 3. Symbolic equation solving

3.1 Solving algebraic equations (help can be used to refer to matlab help documentation)

    S = solve(eqn,var)
    S = solve(eqn,var,Name,Value)
    Y = solve(eqns,vars)
    Y = solve(eqns,vars,Name,Value)
    [y1,...,yN] = solve(eqns,vars)
    [y1,...,yN] = solve(eqns,vars,Name,Value)
    [y1,...,yN,parameters,conditions] = solve(eqns,vars,'ReturnConditions',true)

Example 3.1 Solve the equation sinx=1.

syms x
eqn = sin(x) == 1;
solx = solve(eqn,x)

We know that sinx=1 has infinitely many solutions, so we need to specify 'ReturnConditions' to be true

[solx, params, conds] = solve(eqn, x, 'ReturnConditions', true)

 Example 3.2

syms u v w y z;
f1=u*y^2+v*z+w;
f2=y+z+w;
[a,b]=solve([f1,f2],[y,z])

 result:

3.2 Solving Differential Equations

dsolve('eqn1', ..., 'eqnN', 'cond1', ..., 'condN', 'var1', ..., 'varN')

Function: Find the general solution of differential equations eqn1,...,eqnN, cond1,...,condN are initial value or boundary value conditions, var1,...,varN are independent variables (argument variables of the function to be found), by default The independent variable is t.

Note: When entering differential equations, y' should be entered as Dy, y'' should be entered as D2y,..., D should be capitalized.

For example: dsolve('Dx=-a*x') %ans=C1/exp(a*t)

dsolve('Dx=-a*x', 'x(0)=1', 's')  %ans=1/exp(a*s)

S=dsolve('Df=f+g', 'Dg=-f+g', 'f(0)=1', 'g(0)=2')

dsolve('Df=f+sin(t)', 'f(pi/2)=0')

Example 3.3 Solve the following differential equation

syms y(x);
f=diff(y,x,2)+3*diff(y,x,1)+3*exp(x);
y=dsolve(f,x)

 result:

syms y(x);
f=diff(y,x,2)-exp(2*y)*diff(y,x,1);
y=dsolve(f,x)

result:

 Example 3.4

syms y(x);
s=dsolve('(x^2-1)*Dy+2*x*y-cos(x)','y(0)=1',x)

 result:

syms x(t) n a x0 v0;
dsolve('D2x+2*n*Dx+a^2*x','x(0)=x0','Dx(0)=v0',t)

result:

ans =
 
(exp(-t*(n - (-(a + n)*(a - n))^(1/2)))*(v0 + n*x0 + x0*(-(a + n)*(a - n))^(1/2)))/(2*(-(a + n)*(a - n))^(1/2)) - (exp(-t*(n + (-(a + n)*(a - n))^(1/2)))*(v0 + n*x0 - x0*(-(a + n)*(a - n))^(1/2)))/(2*(-(a + n)*(a - n))^(1/2))
 

Guess you like

Origin blog.csdn.net/higerwy/article/details/130266236