MATLAB Symbolic Exercise
MATLAB Symbolic Exercise
I. Differentiation
1. syms x n a b y1 = diff(sin(5*x)) y2 = diff(exp(x)* sin(x)); simplify(y2) y3 = diff(x^n) y4 = diff(sin(a*x+b)) y5 = diff((3*x^2+6*x-1)/(x^2+x+3)); simplify(y5)
Answer:
y1 = 5*cos(5*x) y2 = exp(x)*(cos(x) + sin(x)) y3 = n*x^(n - 1) y4 = a*cos(b + a*x) y5 = (- 3*x^2 + 20*x + 19)/(x^2 + x + 3)^2 2. syms x n a b y1 = diff(sin(5*x),2) y2 = diff((exp(x)* sin(x)),2); simplify(y2) y3 = diff(x^n,2) y4 = diff(sin(a*x+b),2) y5 = diff(((3*x^2+6*x-1)/(x^2+x+3)),2); simplify(y5)
Answer:
y1 = -25*sin(5*x) y2 = 2*exp(x)*cos(x) y3 = n*x^(n - 2)*(n - 1) y4 = -a^2*sin(b + a*x) y5 = -(2*(- 3*x^3 + 30*x^2 + 57*x - 11))/(x^2 + x + 3)^3 3. syms x t z y1_x = diff(sin(x*t),'x') y1_t = diff(sin(x*t),'t') y2_x = diff((exp(t)* sin(x)),'x'); simplify(y2_x) y2_t = diff((exp(t)* sin(x)),'t'); simplify(y2_t) y3_x = diff(((3*x^2*z+6*x*z^2-z^3)/(x^2+x*z+3*z^2)),'x'); simplify(y3_x) y3_z = diff(((3*x^2*z+6*x*z^2-z^3)/(x^2+x*z+3*z^2)),'z');
simplify(y3_z)
Answer:
y1_x = t*cos(t*x) y1_t = x*cos(t*x) y2_x = exp(t)*cos(x) y2_t = exp(t)*sin(x) y3_x = (z^2*(- 3*x^2 + 20*x*z + 19*z^2))/(x^2 + x*z + 3*z^2)^2 y3_z = ((10*x^4)/3 + (38*x^3*z)/3 - (11*x^2*z^2)/3)/(x^2 + x*z + 3*z^2)^2 - 1/3
2. Limits
syms y1 = y2 = y3 = y4 = y5 = x n a b limit(sin(5*x)) limit(exp(x)* sin(x)) limit((x^n),'x',10) limit(sin(a*x+b),'x',0) limit((3*x^2+6*x-1)/(x^2+x+3))
Answer:
y1 = 0 y2 = 0 y3 = 10^n y4 = sin(b) y5 = -1/3
3. Integration
syms y1 = y2 = y3 = x n a b int(sin(5*x),0, pi/2) int(x^n) int(cos(a*t+b),0,pi/2)
Answer:
y1 = 1/5 y2 = piecewise([n = -1, log(x)], [n <> -1, x^(n + 1)/(n + 1)]) y3 = (sin(b + (pi*a)/2) - sin(b))/a
equations or initial conditions may be grouped together, separated by commas, in a single input argument. By default, the independent variable is 't'. The independent variable may be changed from 't' to some other symbolic variable by including that variable as the last input argument. The letter 'D' denotes differentiation with respect to the independent variable, i.e. usually d/dt. A "D" followed by a digit denotes repeated differentiation; e.g., D2 is d^2/dt^2. Any characters immediately following these differentiation operators are taken to be the dependent variables; e.g., D3y denotes the third derivative of y(t). Note that the names of symbolic variables should not contain the letter "D". Initial conditions are specified by equations like 'y(a)=b' or 'Dy(a) = b' where y is one of the dependent variables and a and b are constants. If the number of initial conditions given is less than the number of dependent variables, the resulting solutions will obtain arbitrary constants, C1, C2, etc. 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. If no closed-form (explicit) solution is found, an implicit solution is attempted. When an implicit solution is returned, a warning is given. If neither an explicit nor implicit solution can be computed, then a warning is given and the empty sym is returned. In some cases involving nonlinear equations, the output will be an equivalent lower order differential equation or an integral. DSOLVE(...,'IgnoreAnalyticConstraints',VAL) controls the level of mathematical rigor to use on the analytical constraints of the solution (branch cuts, division by zero, etc). The options for VAL are 'all' or 'none'. Specify 'none' to use the highest level of mathematical rigor in finding any solutions. The default is 'all'. Examples: dsolve('Dx = -a*x') returns ans = C1/exp(a*t) x = dsolve('Dx = -a*x','x(0) = 1','s') returns x = 1/exp(a*s) S = dsolve('Df = f + g','Dg = -f + g','f(0) = 1','g(0) = 2') returns a structure S with fields S.f = (i + 1/2)/exp(t*(i - 1)) - exp(t*(i + 1))*(i - 1/2) S.g = exp(t*(i + 1))*(i/2 + 1) - (i/2 - 1)/exp(t*(i - 1)) dsolve('Df = f + sin(t)', 'f(pi/2) = 0') dsolve('D2y = -a^2*y', 'y(0) = 1, Dy(pi/a) = 0') S = dsolve('Dx = y', 'Dy = -x', 'x(0)=0', 'y(0)=1') S = dsolve('Du=v, Dv=w, Dw=-u','u(0)=0, v(0)=0, w(0)=1') w = dsolve('D3w = -w','w(0)=1, Dw(0)=0, D2w(0)=0')
syms t x a b y1 = dsolve('Dy+4*y = exp(-t)','y(0)=1') simplify(y1) y2 = dsolve('D2y+10*Dy-y=0', 'Dy(0)=0','y(0)=1') y3 = dsolve('D2y+3*Dy-2*y = 0','Dy(0)= 5','y(0)=6') y4 = dsolve('D2y+3*Dy-2*y = 0','Dy(0)= 10','y(0)=15') simplify(y4)
Answer:
y1 = 1/(3*exp(t)) + 2/(3*exp(4*t)) ans = 1/(3*exp(t)) + 2/(3*exp(4*t)) y2 = (26^(1/2)*exp(t*(26^(1/2) - 5))*(26^(1/2) + 5))/52 + (26^(1/2)*(26^(1/2) 5))/(52*exp(t*(26^(1/2) + 5))) y3 = (17^(1/2)*exp(t*(17^(1/2)/2 - 3/2))*(3*17^(1/2) + 14))/17 + (17^(1/2)*(3*17^(1/2) - 14))/(17*exp(t*(17^(1/2)/2 + 3/2))) y4 = (5*17^(1/2)*exp(t*(17^(1/2)/2 - 3/2))*(3*17^(1/2) + 13))/34 + (5*17^(1/2)*(3*17^(1/2) - 13))/(34*exp(t*(17^(1/2)/2 + 3/2))) ans = (5*17^(1/2)*exp(t*(17^(1/2)/2 - 3/2))*(3*17^(1/2) + 13))/34 + (5*17^(1/2)*(3*17^(1/2) - 13))/(34*exp(t*(17^(1/2)/2 + 3/2)))
[ 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. See also DSOLVE. Overloaded methods: cgvariable/solve cgvalue/solve cgsubexpr/solve cgfeature/solve cgexpr/solve cgdivexpr/solve
syms x1 x2 x3 [x1 x2 x3] = solve(3*x1+4*x2+5*x3-10, 4*x1+5*x2+x3+15, x1+x2+x3-1) x1 = -4/5 x2 = -17/5 x3 = 26/5 syms x s = solve('cos(x)+sin(x) = 1') s = 0 pi/2