全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 数据分析与数据挖掘
1220 0
2016-03-10

MATLAB课程:代码示例之Math, Statistics, and Optimization(一)



Computational Mathematics in the Symbolic Toolbox

This example provides an overview of the Symbolic Math Toolbox which offers a complete set of tools for computational and analytical mathematics including

Equations, expressions and functions

Substitution and solving

Simplification and manipulation

Calculus (Differentiation, Integration, Limits, Series)

Differential Equations

Linear Algebra

Graphics

For more details see Getting Started with Symbolic Math Toolbox.  For more details on documenting and sharing your mathematics see Live Scripts.

Variables, expressions, functions and equations

The Symbolic Math Toolbox supports mathematical expressions, functions and equations.  It supports a wide breadth of functions including trigonometric, logarithmic, exponential, and special functions.

Express numbers in exact rational form instead of decimal approximations.

sym(pi/6) + sym(pi/4)

ans =

5 π

12

pi/6 + pi/4

ans = 1.3090

Create symbolic variables, vectors and matrices using sym. Create symbolic expressions and perform mathematical calculations.

syms x y

log(x) + exp(y)

ans =e

y

+log(x)

Create and evaluate functions, find the value of f(x)=x

4

−2∗x

3

+6∗x

2

−2∗x+10 at x=−5.

syms f(x)

f(x) = x^4-2*x^3+6*x^2-2*x+10

f(x) =x

4

−2 x

3

+6 x

2

−2 x+10

f(-5)

ans =1045

Create equations using the == operator; find the intersection between lines y1 and y2 using solve.

syms y1 y2

y1 = x+3; y2 = 3*x;

solve(y1 == y2)

ans =

3

2

Make assumptions on symbolic variables. There are 4 solutions to x

4

=1, two real and two complex.  Assuming that x is real and x>0 there is only one solution.

syms x

solve(x^4 == 1)

ans =



−1

1

−i

i



assume(x,'real')

assumeAlso( x > 0)

assumptions(x)

ans =(

0<x

x∈ℝ

)

solve(x^4 == 1)

ans =1

assume(x,'clear')

Substitution and Solving

The Symbolic Math Toolbox supports evaluation of mathematical functions by substituting for any part of an expression using subs. You can substitute numeric values, other symbolic variables or expressions, vectors, or matrices. The Symbolic Math Toolbox supports the solving of equations and systems of equations using solve.  It supports solving multi-variate equations, solving inequalities and solving with assumptions. Solutions can be found symbolically or numerically with high precision variable precision arithmetic.

Make substitutions with your symbolic variables;  substitute x=xo−1 into x

2

+1

syms x xo

subs(x^2+1,x,xo-1)

ans =(xo−1)

2

+1

Substitute multiple values, evaluate cos(a)+sin(b)−e

2C

by substituting  a=

π

2

,b=

π

4

,c=−1.

syms a b c

subs(cos(a) + sin(b) - exp(2*c), [a b c], [pi/2 pi/4 -1])

ans =

G

2

2

−e

−2

Create and solve equations; Find the zeros of 9x

2

−1=0.

solve(9*x^2 - 1 == 0)

ans =



1

3

1

3



Or solve the general quadratic equation ax

2

+bx+c=0 and use subs to evaluate that solution for a=9,b=0,c=−1.

eqn = a*x^2 + b*x + c == 0;

sol = solve(eqn)

sol =



b+

G

b

2

−4 a c

2 a

b−

G

b

2

−4 a c

2 a



subs(sol,[a b c],[9 0 -1])

ans =



1

3

1

3



Solve equations numerically using variable precision arithmetic when high accuracy or speed is required.

syms f(x)

f(x) = 6*x^7-2*x^6+3*x^3-8;

sol = vpasolve(f)

sol =



1.0240240759053702941448316563337

−0.88080620051762149639205672298326+0.50434058840127584376331806592405 i

−0.88080620051762149639205672298326−0.50434058840127584376331806592405 i

−0.22974795226118163963098570610724+0.96774615576744031073999010695171 i

−0.22974795226118163963098570610724−0.96774615576744031073999010695171 i

0.7652087814927846556172932675903+0.83187331431049713218367239317121 i

0.7652087814927846556172932675903−0.83187331431049713218367239317121 i



Simplification and Manipulation

The Symbolic Math Toolbox supports the simplification and manipulation of mathematical functions. Most mathematical expressions can be represented in different, but mathematically equivalent forms and the Symbolic Math Toolbox supports a number of operations including factoring or expanding expressions, combining terms, rewriting or rearranging expressions and simplification based on assumptions.

Perform polynomial multiplication and simplify the results, show that (x−1)∗(x+1)∗(x

2

+x+1)∗(x

2

+1)∗(x

2

−x+1)∗(x

4

−x

2

+1) simplifies to x

12

−1

simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))

ans =x

12

−1

Apply trigonometric identities to simplifications, for example  sin

2

(x)=

1−cos(2∗x)

2

combine(2*sin(x)*cos(x) + (1- cos(2*x))/2 + cos(x)^2,'sincos')

ans =sin(2 x)+1

Factor or expand multivariate polynomials

factor(y^6-x^6)

ans =(

−1

x−y

x+y

x

2

+x y+y

2

x

2

−x y+y

2

)

f(x) = (x^3 + 7);

expand(f(y-1))

ans =y

3

−3 y

2

+3 y+6

Find the functional composition f(g(x))

f(x) = sqrt(log(x));

g(x) = sqrt(1-x);

h = compose(g,f,x)

h =

G

1−

G

log(x)

Calculus (Differentiation, Integration, Limits, Series, etc)

The Symbolic Math Toolbox has a full set of calculus tools for applied mathematics. It can perform multivariate symbolic integration and differentiation.  It can generate, manipulate and perform calculations with series.

Find the derivative of

d

dx

(sin(x)).

diff(sin(x))

ans =cos(x)

Find the derivative of

d

dx

(x

2

+sin(2x

4

)+1) using the chain rule.

diff(x^2+sin(2*x^4)+1,x)

ans =2 x+8 x

3

 cos(2 x

4

)

Find the indefinite integral f(x)dx for f(x)=e

−x

2

2

.

int(exp(-x^2/2),x)

ans =

G

2

G

π

 erf(

G

2

 x

2

)

2

Find the definite integral 

b

a

f(x)dx for f(x)=xlog(1+x) from 0 to 1.

int(x*log(1+x),0,1)

ans =

1

4

Show that

sin(x)

x

=1 at x=0 by computing the taylor series expansion (x−a)

n

f

(n)

(a)

n!

  for f(x)=

sin(x)

x

around the point x=0.

syms x

T = taylor(sin(x)/x)

T =

x

4

120

x

2

6

+1

subs(T,x,0)

ans =1

Show that tan(x) is discontinuous at x=

π

2

by showing that the left and right limits are not equal.

lim

x→

π

2

+

tan(x)≠

lim

x→

π

2

tan(x).

limit(tan(x),x,pi/2,'left')

ans =∞

limit(tan(x),x,pi/2,'right')

ans =−∞

limit(tan(x),x,pi/2)

ans =NaN

Differential Equations

The Symbolic Math Toolbox can analytically solve systems of ordinary differential equations using dsolve.

Solve the first order ODE  

dy

dx

=−ay

syms a b y(x)

dsolve(diff(y) == -a*y)

ans =C

4

 e

−a x

Solve the same ODE with the intial condition  y(0)=b

dsolve(diff(y)== -a*y,y(0)==b)

ans =b e

−a x

Solve the system of coupled first order ODEs

dx

dt

=y and

dy

dt

=−x

syms x(t) y(t)

z = dsolve(diff(x) == y, diff(y) == -x);

disp([z.x;z.y])



C

7

 cos(t)+C

6

 sin(t)

C

6

 cos(t)−C

7

 sin(t)



Linear Algebra

The Symbolic Math Toolbox can work with symbolic vectors and matricies. It can compute eigenvalues and eigenvectors of symbolic matrices.

Perform the matrix vector multiplicationA∗x=b where A=[

a

c

b

d

]and x=[x1,x2]  

syms a b c d

syms x1 x2

x = [x1; x2];

A = [a b ; c d];

b = A*x

b =

(

a x

1

+b x

2

c x

1

+d x

2

)

Find the determinant of A

det(A)

ans =a d−b c

Find the eigenvectors of A

lambda = eig(A)

lambda =



a

2

+

d

2

G

a

2

−2 a d+d

2

+4 b c

2

a

2

+

d

2

+

G

a

2

−2 a d+d

2

+4 b c

2



Graphics

The Symbolic Math Toolbox supports analytical plotting in 2D and 3D.

fplot(tan(x))

Plot the parametric curve x(t)=t∗sin(5t) and y(t)=t∗cos(5t).

syms t

x = t*sin(5*t);

y = t*cos(5*t);

fplot(x, y)

grid on

Plot the 3D parametric curve x(t)=e

t

10

sin(5t), y(t)=e

t

10

cos(5t) and z(t)=t from [-10,10] with a dashed red line.

syms t

xt = exp(abs(t)/10).*sin(5*abs(t));

yt = exp(abs(t)/10).*cos(5*abs(t));

zt = t;

h = fplot3(xt,yt,zt, [-10,10],'--r');

Plot the 3D surface f(x,y)=sin(x)+cos(y).

syms x y

fsurf(sin(x) + cos(y))

Plot the 2D contours of the same surface.

fcontour(sin(x) + cos(y))


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群