Mudanças entre as edições de "Uso do calculo simbólico na Matlab"
(15 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
Linha 1: | Linha 1: | ||
− | =[http://www.mathworks.com/help/symbolic/ | + | = Funções importantes para o uso do cálculo simbólico = |
− | + | *[http://www.mathworks.com/help/symbolic/vpa.html vpa(x)] Variable-precision arithmetic: evaluate each element of the symbolic input x to at least d significant digits, where d is the value of the digits function. The default value of digits is 32. | |
− | + | *[http://www.mathworks.com/help/symbolic/digits.html digits(d)] Change variable precision used: sets the precision used by vpa to d significant decimal digits. The default is 32 digits. | |
− | + | :*'''ATENÇÃO''': o digits por ser uma variável global, uma vez alterada para outro valor, permanecerá com este valor mesmo que outro script seja executado. Isso pode afetar os resultados dos cálculos de outros scripts. Assim sendo recomenda-se que após utilizar um digits diferente do default, se retorno a ele usando '''digits(32).''' | |
− | + | *[http://www.mathworks.com/help/symbolic/double.html double(s)] Convert symbolic values to MATLAB double precision: converts the symbolic value s to double precision. Converting symbolic values to double precision is useful when a MATLAB® function does not accept symbolic values. | |
− | + | *[http://www.mathworks.com/help/symbolic/sym.html x = sym('x')] creates symbolic variable x | |
− | + | *[http://www.mathworks.com/help/symbolic/syms.html syms x] Create symbolic variables and functions: Create symbolic variable x. | |
− | + | *[http://www.mathworks.com/help/symbolic/poly2sym.html p = poly2sym(c)] Create symbolic polynomial from vector of coefficients: creates the symbolic polynomial expression p from the vector of coefficients c. | |
+ | *[http://www.mathworks.com/help/symbolic/sym2poly.html c = sym2poly(p)] Extract vector of all numeric coefficients, including zeros, from symbolic polynomial. | ||
+ | *[http://www.mathworks.com/help/symbolic/solve.html S = solve(eqn,var)] Equations and systems solver. | ||
+ | *[http://www.mathworks.com/help/symbolic/subs.html subs(s,new)] Symbolic substitution. | ||
+ | *[http://www.mathworks.com/help/symbolic/matlabfunction.html g = matlabFunction(f)] Convert symbolic expression to function handle or file. | ||
+ | *[http://www.mathworks.com/help/symbolic/simplify.html simplify(S)] Algebraic simplification | ||
+ | *[http://www.mathworks.com/help/symbolic/expand.html expand(S)] Symbolic expansion of polynomials and elementary functions | ||
+ | *[http://www.mathworks.com/help/symbolic/collect.html collect(P)] Collects coefficients in P of the powers | ||
+ | *[http://www.mathworks.com/help/symbolic/factor.html F = factor(x)] Factorization | ||
+ | *[http://www.mathworks.com/help/symbolic/numden.html numden(A)] Extract numerator and denominator | ||
+ | *[http://www.mathworks.com/help/symbolic/partfrac.html partfrac(expr,var)] Partial fraction decomposition | ||
+ | *[http://www.mathworks.com/help/symbolic/latex.html latex(S)] LaTeX form of symbolic expression | ||
+ | *[http://www.mathworks.com/help/matlab/creating_plots/text-with-mathematical-expressions-using-latex.html text(x,y, latex(S), 'Interpreter', 'latex') ] Text with Mathematical Expression Using LaTeX: how add text to a graph that includes mathematical expressions using LaTeX | ||
− | + | = Algumas dicas sobre o cálculo simbólico = | |
− | + | *[http://www.mathworks.com/help/symbolic/choose-symbolic-or-numeric-arithmetic.html Choose Symbolic or Numeric Arithmetic] | |
− | + | *[http://www.mathworks.com/help/symbolic/performing-symbolic-computations.html Perform Symbolic Computations] | |
− | + | *[http://www.mathworks.com/help/symbolic/create-symbolic-numbers-variables-and-expressions.html Create Symbolic Numbers, Variables, and Expressions] | |
+ | *[http://www.mathworks.com/help/symbolic/create-symbolic-functions.html Create Symbolic Functions] | ||
+ | <syntaxhighlight lang=matlab> | ||
+ | syms x y | ||
+ | f = sin(x)^2 + cos(y)^2; | ||
+ | diff(f) | ||
+ | </syntaxhighlight> | ||
+ | ans = | ||
+ | 2*cos(x)*sin(x) | ||
+ | *[https://www.mathworks.com/help/symbolic/extract-numerators-and-denominators-of-rational-expressions.html Extract Numerators and Denominators of Rational Expressions] | ||
+ | *[https://www.mathworks.com/help/symbolic/examples/computational-mathematics-in-symbolic-math-toolbox.html Computational Mathematics in Symbolic Math Toolbox] | ||
+ | *[http://www.mathworks.com/help/symbolic/generate-matlab-functions.html Generate MATLAB Functions from Symbolic Expressions] | ||
+ | *[https://www.mathworks.com/help/symbolic/examples/learn-calculus.html Learn Calculus in the Live Editor] | ||
+ | *[http://www.mathworks.com/help/symbolic/use-subs-to-evaluate-expressions-and-functions.html Use subs to Evaluate Expressions and Functions] | ||
+ | *[http://www.mathworks.com/help/symbolic/formula-rearrangement-and-rewriting.html Formula Rearrangement and Rewriting] | ||
+ | *[https://www.mathworks.com/help/matlab/creating_plots/text-with-mathematical-expressions-using-latex.html Text with Mathematical Expression Using LaTeX] | ||
+ | |||
+ | =Exemplo aplicado a substituição de variáveis= | ||
+ | Definindo uma função de transferência <math> H(s) = \frac{s^2 + 1}{3\, s^2 + 4\, s + 6} </math> como uma função simbólica | ||
+ | |||
+ | syms s | ||
+ | H(s) = (s^2 + 1)/(3*s^2 + 4*s + 6) | ||
+ | |||
+ | H(s) = | ||
+ | (s^2 + 1)/(3*s^2 + 4*s + 6) | ||
+ | Imprimindo H(s) em formato LaTeX. | ||
+ | |||
+ | latex(H(s)) | ||
ans = | ans = | ||
− | + | \frac{s^2 + 1}{3\, s^2 + 4\, s + 6} | |
+ | |||
+ | Substituindo a variável s pela f(z) = 2*(z-1)/(z+1); | ||
− | = | + | syms z |
− | + | Hz(z) = subs(H,2*(z-1)/(z+1)) | |
− | + | ||
− | + | Hz(z) = | |
+ | ((2*z - 2)^2/(z + 1)^2 + 1)/((3*(2*z - 2)^2)/(z + 1)^2 + (4*(2*z - 2))/(z + 1) + 6) | ||
+ | Obtem-se a expressão: <math> H(z) = \frac{\frac{{\left(2\, z - 2\right)}^2}{{\left(z + 1\right)}^2} + 1}{\frac{3\, {\left(2\, z - 2\right)}^2}{{\left(z + 1\right)}^2} + \frac{4\, \left(2\, z - 2\right)}{z + 1} + 6} </math> | ||
+ | |||
+ | Agrupando os termos comuns em potências de z | ||
+ | |||
+ | Hz2 = collect(Hz) | ||
+ | |||
+ | Hz2 = | ||
+ | (5*z^2 - 6*z + 5)/(26*z^2 - 12*z + 10) | ||
+ | |||
+ | Obtem-se a expressão: <math> H(z) = | ||
+ | \frac{5\, z^2 - 6\, z + 5}{26\, z^2 - 12\, z + 10} </math> | ||
− | + | O numerador e denominador de H(z) podem ser obtidos por: | |
− | |||
− | [N,D] = numden( | + | [N,D] = numden(Hz2) |
N = | N = | ||
− | + | 5*z^2 - 6*z + 5 | |
+ | |||
D = | D = | ||
− | + | 26*z^2 - 12*z + 10 | |
− | + | Por sua vez os pólos de H(z) são obtidos por | |
− | + | pk = roots(sym2poly(D)) | |
− | |||
− | + | e os zeros por: | |
− | + | zk = roots(sym2poly(N)) | |
− | |||
− | |||
− | + | =Exemplo: Geração de uma função de transferência a partir dos pólos e zeros= | |
− | |||
− | |||
− | |||
− | :* | + | Considerando que são conhecidos os pólos (pk) e zeros (zk) de uma função de transferência H(s), deseja-se obter a sua expressão simbólica. |
− | + | ||
− | + | Para obter os coeficientes do numerador | |
+ | zk = [ -1 2 0 +1] | ||
+ | cN = poly(zk) | ||
+ | |||
+ | Obtendo o polinômio do numerador | ||
+ | syms s; | ||
+ | N(s) = poly2sym(cN,s) | ||
+ | vpa(N(s),4) | ||
+ | |||
+ | Para obter os coeficientes do denumerador | ||
+ | pk = [ -1 -2 -0.3+1j -0.3-1j ] | ||
+ | cD = poly(pk) | ||
+ | |||
+ | Obtendo o polinômio do numerador | ||
+ | syms s; | ||
+ | D(s) = poly2sym(cD,s) | ||
+ | vpa(D(s),4) | ||
+ | |||
+ | Resultando na função de transferência desejada | ||
+ | |||
+ | digits(4) | ||
+ | H(s) = vpa(N(s))/vpa(D(s)); | ||
+ | pretty(H(s)) | ||
+ | |||
+ | 4 3 2 | ||
+ | s - 2.0 s - 1.0 s + 2.0 s | ||
+ | ------------------------------------- | ||
+ | 4 3 2 | ||
+ | s + 3.6 s + 4.89 s + 4.47 s + 2.18 | ||
+ | |||
+ | |||
+ | <math> H(s) = \frac{s^4 - 2.0\, s^3 - 1.0\, s^2 + 2.0\, s}{s^4 + 3.6\, s^3 + 4.89\, s^2 + 4.47\, s + 2.18} </math> | ||
+ | |||
+ | =Exemplo: Expansão de uma função de transferência e frações parciais= | ||
+ | Considerando o exemplo anterior de H(s). | ||
+ | syms s; | ||
+ | H(s) = (s^4 - 2.0*s^3 - 1.0*s^2 + 2.0*s)/(s^4 + 3.6*s^3 + 4.89*s^2 + 4.47*s + 2.18); | ||
+ | H_fr(s) = partfrac(H(s)) | ||
+ | digits(4); | ||
+ | pretty(vpa(H_fr(s))) | ||
+ | |||
+ | =Exemplo: Substituindo a variável s por valores= | ||
+ | Para fazer a substituição, basta definir os valores que devem ser substituídos. Seguem alguns exemplos: | ||
+ | |||
+ | Substituindo s por 0 | ||
+ | H(0) | ||
+ | H_fr(0) | ||
+ | |||
+ | substituindo s pelos valor <math> jw </math> | ||
+ | w = [0 1 2 4 10 100]; | ||
+ | H(w*1j) | ||
+ | ans = | ||
+ | [ 0, 20/1227 - 2860i/1227, - 11800/11009 + 30800i/33027, 196480/760227 + 268880i/253409, | ||
+ | 366755000/425498151 + 227414000i/425498151, 832325926000000/833514949238127 + | ||
+ | 46654792030000i/833514949238127] | ||
+ | |||
+ | ou obtendo os valores aritméticos com precisão variável | ||
+ | vpa(H(w*1j),8) | ||
+ | ans = | ||
+ | [ 0, 0.0163 - 2.3309i, - 1.0719 + 0.93257i, 0.25845 + 1.0611i, 0.86194 + 0.53447i, 0.99857 + 0.055974i] | ||
− | + | ou ainda convertendo os valores simbólicos para valores de dupla precisão do MATLAB | |
− | |||
− | |||
− | + | H_w = double(H(w*1j)) | |
− | + | format long | |
+ | H_w = | ||
+ | Column 1 | ||
+ | 0.000000000000000 + 0.000000000000000i | ||
+ | Column 2 | ||
+ | 0.016300000000000 - 2.331000000000000i | ||
+ | Column 3 | ||
+ | -1.072000000000000 + 0.932600000000000i | ||
+ | Column 4 | ||
+ | 0.258400000000000 + 1.061000000000000i | ||
+ | Column 5 | ||
+ | 0.861900000000000 + 0.534500000000000i | ||
+ | Column 6 | ||
+ | 0.998600000000000 + 0.055970000000000i |
Edição atual tal como às 15h57min de 30 de março de 2020
Funções importantes para o uso do cálculo simbólico
- vpa(x) Variable-precision arithmetic: evaluate each element of the symbolic input x to at least d significant digits, where d is the value of the digits function. The default value of digits is 32.
- digits(d) Change variable precision used: sets the precision used by vpa to d significant decimal digits. The default is 32 digits.
- ATENÇÃO: o digits por ser uma variável global, uma vez alterada para outro valor, permanecerá com este valor mesmo que outro script seja executado. Isso pode afetar os resultados dos cálculos de outros scripts. Assim sendo recomenda-se que após utilizar um digits diferente do default, se retorno a ele usando digits(32).
- double(s) Convert symbolic values to MATLAB double precision: converts the symbolic value s to double precision. Converting symbolic values to double precision is useful when a MATLAB® function does not accept symbolic values.
- x = sym('x') creates symbolic variable x
- syms x Create symbolic variables and functions: Create symbolic variable x.
- p = poly2sym(c) Create symbolic polynomial from vector of coefficients: creates the symbolic polynomial expression p from the vector of coefficients c.
- c = sym2poly(p) Extract vector of all numeric coefficients, including zeros, from symbolic polynomial.
- S = solve(eqn,var) Equations and systems solver.
- subs(s,new) Symbolic substitution.
- g = matlabFunction(f) Convert symbolic expression to function handle or file.
- simplify(S) Algebraic simplification
- expand(S) Symbolic expansion of polynomials and elementary functions
- collect(P) Collects coefficients in P of the powers
- F = factor(x) Factorization
- numden(A) Extract numerator and denominator
- partfrac(expr,var) Partial fraction decomposition
- latex(S) LaTeX form of symbolic expression
- text(x,y, latex(S), 'Interpreter', 'latex') Text with Mathematical Expression Using LaTeX: how add text to a graph that includes mathematical expressions using LaTeX
Algumas dicas sobre o cálculo simbólico
- Choose Symbolic or Numeric Arithmetic
- Perform Symbolic Computations
- Create Symbolic Numbers, Variables, and Expressions
- Create Symbolic Functions
syms x y
f = sin(x)^2 + cos(y)^2;
diff(f)
ans = 2*cos(x)*sin(x)
- Extract Numerators and Denominators of Rational Expressions
- Computational Mathematics in Symbolic Math Toolbox
- Generate MATLAB Functions from Symbolic Expressions
- Learn Calculus in the Live Editor
- Use subs to Evaluate Expressions and Functions
- Formula Rearrangement and Rewriting
- Text with Mathematical Expression Using LaTeX
Exemplo aplicado a substituição de variáveis
Definindo uma função de transferência como uma função simbólica
syms s H(s) = (s^2 + 1)/(3*s^2 + 4*s + 6)
H(s) = (s^2 + 1)/(3*s^2 + 4*s + 6)
Imprimindo H(s) em formato LaTeX.
latex(H(s))
ans = \frac{s^2 + 1}{3\, s^2 + 4\, s + 6}
Substituindo a variável s pela f(z) = 2*(z-1)/(z+1);
syms z Hz(z) = subs(H,2*(z-1)/(z+1))
Hz(z) = ((2*z - 2)^2/(z + 1)^2 + 1)/((3*(2*z - 2)^2)/(z + 1)^2 + (4*(2*z - 2))/(z + 1) + 6)
Obtem-se a expressão:
Agrupando os termos comuns em potências de z
Hz2 = collect(Hz)
Hz2 = (5*z^2 - 6*z + 5)/(26*z^2 - 12*z + 10)
Obtem-se a expressão:
O numerador e denominador de H(z) podem ser obtidos por:
[N,D] = numden(Hz2)
N = 5*z^2 - 6*z + 5 D = 26*z^2 - 12*z + 10
Por sua vez os pólos de H(z) são obtidos por
pk = roots(sym2poly(D))
e os zeros por:
zk = roots(sym2poly(N))
Exemplo: Geração de uma função de transferência a partir dos pólos e zeros
Considerando que são conhecidos os pólos (pk) e zeros (zk) de uma função de transferência H(s), deseja-se obter a sua expressão simbólica.
Para obter os coeficientes do numerador
zk = [ -1 2 0 +1] cN = poly(zk)
Obtendo o polinômio do numerador
syms s; N(s) = poly2sym(cN,s) vpa(N(s),4)
Para obter os coeficientes do denumerador
pk = [ -1 -2 -0.3+1j -0.3-1j ] cD = poly(pk)
Obtendo o polinômio do numerador
syms s; D(s) = poly2sym(cD,s) vpa(D(s),4)
Resultando na função de transferência desejada
digits(4) H(s) = vpa(N(s))/vpa(D(s)); pretty(H(s))
4 3 2 s - 2.0 s - 1.0 s + 2.0 s ------------------------------------- 4 3 2 s + 3.6 s + 4.89 s + 4.47 s + 2.18
Exemplo: Expansão de uma função de transferência e frações parciais
Considerando o exemplo anterior de H(s).
syms s; H(s) = (s^4 - 2.0*s^3 - 1.0*s^2 + 2.0*s)/(s^4 + 3.6*s^3 + 4.89*s^2 + 4.47*s + 2.18); H_fr(s) = partfrac(H(s)) digits(4); pretty(vpa(H_fr(s)))
Exemplo: Substituindo a variável s por valores
Para fazer a substituição, basta definir os valores que devem ser substituídos. Seguem alguns exemplos:
Substituindo s por 0
H(0) H_fr(0)
substituindo s pelos valor
w = [0 1 2 4 10 100]; H(w*1j) ans = [ 0, 20/1227 - 2860i/1227, - 11800/11009 + 30800i/33027, 196480/760227 + 268880i/253409, 366755000/425498151 + 227414000i/425498151, 832325926000000/833514949238127 + 46654792030000i/833514949238127]
ou obtendo os valores aritméticos com precisão variável
vpa(H(w*1j),8) ans = [ 0, 0.0163 - 2.3309i, - 1.0719 + 0.93257i, 0.25845 + 1.0611i, 0.86194 + 0.53447i, 0.99857 + 0.055974i]
ou ainda convertendo os valores simbólicos para valores de dupla precisão do MATLAB
H_w = double(H(w*1j)) format long H_w = Column 1 0.000000000000000 + 0.000000000000000i Column 2 0.016300000000000 - 2.331000000000000i Column 3 -1.072000000000000 + 0.932600000000000i Column 4 0.258400000000000 + 1.061000000000000i Column 5 0.861900000000000 + 0.534500000000000i Column 6 0.998600000000000 + 0.055970000000000i