more off
format compact
qNum=@(x) -4*x.^2+3*x+12
qNum =
function_handle with value:
@(x)-4*x.^2+3*x+12
qNum(1)
ans =
11
qNumInt02=integral(qNum,0,2)
qNumInt02 =
19.3333
format long
qNumInt02=integral(qNum,0,2)
qNumInt02 =
19.333333333333332
qNumRoot=fzero(qNum,1)
qNumRoot =
2.147180859844728
% Symbolic to the rescue
syms x
qSym=-4*x^2+3*x+12
qSym =
- 4*x^2 + 3*x + 12
qSymInt02=int(qSym,x,[0 2])
qSymInt02 =
58/3
qSymInt02=int(qSym,x)
qSymInt02 =
(x*(- 8*x^2 + 9*x + 72))/6
qSymInt02=int(qSym,x,[0 2])
qSymInt02 =
58/3
qSymInt=int(qSym,x)
qSymInt =
(x*(- 8*x^2 + 9*x + 72))/6
qSym
qSym =
- 4*x^2 + 3*x + 12
expand(qSymInt)
ans =
- (4*x^3)/3 + (3*x^2)/2 + 12*x
diff(qSym)
ans =
3 - 8*x
qSymRoots=solve(qSym==0,x)
qSymRoots =
3/8 - 201^(1/2)/8
201^(1/2)/8 + 3/8
qNumRoot
qNumRoot =
2.147180859844728
pretty(qSymRoots)
/ 3 sqrt(201) \
| - - --------- |
| 8 8 |
| |
| sqrt(201) 3 |
| --------- + - |
\ 8 8 /
qSymRoots
qSymRoots =
3/8 - 201^(1/2)/8
201^(1/2)/8 + 3/8
double(qSymRoots)
ans =
-1.397180859844728
2.147180859844728
qNumRoot
qNumRoot =
2.147180859844728
qNumRoot-double(qSymRoots(2))
ans =
0
qSymFactors=factor(qSym)
qSymFactors =
[ -1, 4*x^2 - 3*x - 12]
qSym
qSym =
- 4*x^2 + 3*x + 12
help factor
factor Prime factors.
factor(N) returns a vector containing the prime factors of N.
This function uses the simple sieve approach. It may require large
memory allocation if the number given is too big. Technically it is
possible to improve this algorithm, allocating less memory for most
cases and resulting in a faster execution time. However, it will still
have problems in the worst case.
Class support for input N:
float: double, single
integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64
See also primes, isprime.
Reference page for factor
Other functions named factor
help sym/factor
factor Symbolic factorization.
factor(S), where S is a SYM, returns all irreducible factors of S.
If S is an integer, the prime factorization is computed.
To factor an integer N greater than 2^52, use factor(SYM('N')).
factor(S, VARS), where VARS is a vector of variables,
returns all irreducible factors of S, but does not split factors
that do not contain VARS.
factor(S, 'FACTORMODE', MODE) or
factor(S, VARS, 'FACTORMODE', MODE)
factors S in the factorization mode MODE.
The modes differ only in the case of univariate polynomials.
Possible values:
'rational' - rational factorization.
Result can contain only those irrational subexpressions
that occur already in the input.
This is the default mode.
'full' - factor into symbolic linear factors.
'complex' - factor numerically into linear factors.
'real' - factor numerically into linear or quadratic real factors.
Examples:
syms x y
factor(x^9-1) is
[x - 1, x^2 + x + 1, x^6 + x^3 + 1]
factor(sym('12345678901234567890')) is
[2, 3, 3, 5, 101, 3541, 3607, 3803, 27961]
factor(x^2 * y^2, x) is
[y^2, x, x]
factor(x^2 + 1, 'FactorMode', 'rational') is
[x^2 + 1]
factor(x^2 + 1, 'FactorMode', 'full') is
[x - 1i, x + 1i]
See also factor, sym/simplify, sym/expand, sym/collect, sym/partfrac.
Reference page for sym/factor
help sym/factor
factor Symbolic factorization.
factor(S), where S is a SYM, returns all irreducible factors of S.
If S is an integer, the prime factorization is computed.
To factor an integer N greater than 2^52, use factor(SYM('N')).
factor(S, VARS), where VARS is a vector of variables,
returns all irreducible factors of S, but does not split factors
that do not contain VARS.
factor(S, 'FACTORMODE', MODE) or
factor(S, VARS, 'FACTORMODE', MODE)
factors S in the factorization mode MODE.
The modes differ only in the case of univariate polynomials.
Possible values:
'rational' - rational factorization.
Result can contain only those irrational subexpressions
that occur already in the input.
This is the default mode.
'full' - factor into symbolic linear factors.
'complex' - factor numerically into linear factors.
'real' - factor numerically into linear or quadratic real factors.
Examples:
syms x y
factor(x^9-1) is
[x - 1, x^2 + x + 1, x^6 + x^3 + 1]
factor(sym('12345678901234567890')) is
[2, 3, 3, 5, 101, 3541, 3607, 3803, 27961]
factor(x^2 * y^2, x) is
[y^2, x, x]
factor(x^2 + 1, 'FactorMode', 'rational') is
[x^2 + 1]
factor(x^2 + 1, 'FactorMode', 'full') is
[x - 1i, x + 1i]
See also factor, sym/simplify, sym/expand, sym/collect, sym/partfrac.
Reference page for sym/factor
qSymRoots
qSymRoots =
3/8 - 201^(1/2)/8
201^(1/2)/8 + 3/8
help sym/factor
factor Symbolic factorization.
factor(S), where S is a SYM, returns all irreducible factors of S.
If S is an integer, the prime factorization is computed.
To factor an integer N greater than 2^52, use factor(SYM('N')).
factor(S, VARS), where VARS is a vector of variables,
returns all irreducible factors of S, but does not split factors
that do not contain VARS.
factor(S, 'FACTORMODE', MODE) or
factor(S, VARS, 'FACTORMODE', MODE)
factors S in the factorization mode MODE.
The modes differ only in the case of univariate polynomials.
Possible values:
'rational' - rational factorization.
Result can contain only those irrational subexpressions
that occur already in the input.
This is the default mode.
'full' - factor into symbolic linear factors.
'complex' - factor numerically into linear factors.
'real' - factor numerically into linear or quadratic real factors.
Examples:
syms x y
factor(x^9-1) is
[x - 1, x^2 + x + 1, x^6 + x^3 + 1]
factor(sym('12345678901234567890')) is
[2, 3, 3, 5, 101, 3541, 3607, 3803, 27961]
factor(x^2 * y^2, x) is
[y^2, x, x]
factor(x^2 + 1, 'FactorMode', 'rational') is
[x^2 + 1]
factor(x^2 + 1, 'FactorMode', 'full') is
[x - 1i, x + 1i]
See also factor, sym/simplify, sym/expand, sym/collect, sym/partfrac.
Reference page for sym/factor
qSymFactors=factor(qSym,'Factormode',full')
{Error using full
Not enough input arguments.}
qSymFactors=factor(qSym,'Factormode','full')
qSymFactors =
[ -1, x + 201^(1/2)/8 - 3/8, x - 201^(1/2)/8 - 3/8]
prod(qSymFactors)
ans =
(x + 201^(1/2)/8 - 3/8)*(201^(1/2)/8 - x + 3/8)
qSym
qSym =
- 4*x^2 + 3*x + 12
pretty(prod(qSymFactors))
/ sqrt(201) 3 \ / sqrt(201) 3 \
| x + --------- - - | | --------- - x + - |
\ 8 8 / \ 8 8 /
qRatSym=-4*x^2+3*x+27
qRatSym =
- 4*x^2 + 3*x + 27
factor(qRatSym)
ans =
[ -1, 4*x + 9, x - 3]
prod(factor(qRatSym))
ans =
-(4*x + 9)*(x - 3)
629/969
ans =
0.649122807017544
simplify(629/969)
{Undefined function 'simplify' for input arguments of type
'double'.}
simplify(sym('629/969'))
ans =
37/57
pi^2/6
ans =
1.644934066848226
pi^2/6
ans =
1.644934066848226
sumSym=sym('pi^2/6')
{Error using sym>convertChar (line 1459)
Character vectors and strings in the first argument can
only specify a variable or number. To evaluate character
vectors and strings representing symbolic expressions, use
'str2sym'.
Error in sym>tomupad (line 1225)
S = convertChar(x);
Error in sym (line 214)
S.s = tomupad(x);}
sumSym=sym('pi')^2/6
sumSym =
pi^2/6
sumNum=pi^2/6
sumNum =
1.644934066848226
wrongSum=vpa(sumNum)
wrongSum =
1.6449340668482264060656916626613
rightSum=vpa(sumSym)
rightSum =
1.644934066848226436472415166646
wrongSum=vpa(sumNum,50)
wrongSum =
1.6449340668482264060656916626612655818462371826172
rightSum=vpa(sumSym,50)
rightSum =
1.6449340668482264364724151666460251892189499012068
qGen=a*x^2+b*x+c
{Undefined function or variable 'a'.}
syms a b c
syms x a b c
qGen=a*x^2+b*x+c
qGen =
a*x^2 + b*x + c
qGenRoots=solve(qGen==0,x)
qGenRoots =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
qSymRootTest=subs(qSymRoots,{a b c},{-4 3 12))
qSymRootTest=subs(qSymRoots,{a b c},{-4 3 12))
{Error: Invalid expression. When calling a function or
indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
}
qSymRootTest=subs(qSymRoots,{a b c},{-4 3 12})
qSymRootTest =
3/8 - 201^(1/2)/8
201^(1/2)/8 + 3/8
qSymRoots
qSymRoots =
3/8 - 201^(1/2)/8
201^(1/2)/8 + 3/8
qSymRootsFun=matlabFunction(qSymRoots)
qSymRootsFun =
function_handle with value:
@()[sqrt(2.01e2).*(-1.0./8.0)+3.0./8.0;sqrt(2.01e2)./8.0+3.0./8.0]
qGenRootsFun=matlabFunction(qGenRoots)
qGenRootsFun =
function_handle with value:
@(a,b,c)[((b+sqrt(a.*c.*-4.0+b.^2)).*(-1.0./2.0))./a;((b-sqrt(a.*c.*-4.0+b.^2)).*(-1.0./2.0))./a]
qGenRootsFun(-4,3,12)
ans =
2.147180859844728
-1.397180859844728
solve(exp(x)==-1,x)
ans =
pi*1i
ratSym=(2*x^2-3*x+1)/(x^3+2*x^2-9*x-18)
ratSym =
-(2*x^2 - 3*x + 1)/(- x^3 - 2*x^2 + 9*x + 18)
factor(ratSym)
ans =
[ 2*x - 1, x - 1, 1/(x - 3), 1/(x + 3), 1/(x + 2)]
prod(factor(ratSym))
ans =
((2*x - 1)*(x - 1))/((x + 2)*(x - 3)*(x + 3))
partfrac(ratSym)
ans =
1/(3*(x - 3)) - 3/(x + 2) + 14/(3*(x + 3))
pretty(ans)
1 3 14
--------- - ----- + ---------
3 (x - 3) x + 2 3 (x + 3)
taylor(sin(x))
ans =
x^5/120 - x^3/6 + x
taylor(sin(x),'order',10)
ans =
x^9/362880 - x^7/5040 + x^5/120 - x^3/6 + x
taylor(sin(x)/x,'order',10)
ans =
x^8/362880 - x^6/5040 + x^4/120 - x^2/6 + 1
qxySym=a*x*y-b*x-1
{Undefined function or variable 'y'.}
syms y
qxySym=a*x*y-b*x-1
qxySym =
a*x*y - b*x - 1
xSol=solve(qxySym,x)
xSol =
-1/(b - a*y)
xSol=solve(qxySym==0,x)
xSol =
-1/(b - a*y)
ySol=solve(qxySym==0,y)
ySol =
(b*x + 1)/(a*x)
qxySym
qxySym =
a*x*y - b*x - 1
collect(qxySym,x)
ans =
(a*y - b)*x - 1