qnum=@(x) -4*x.^2+3*x+12
qnum =
function_handle with value:
@(x)-4*x.^2+3*x+12
qnum(2)
ans =
2
integral(qNum,0,2)
{Undefined function or variable 'qNum'.}
qNum=@(x) -4*x.^2+3*x+12
qNum =
function_handle with value:
@(x)-4*x.^2+3*x+12
integral(qNum,0,2)
ans =
19.3333
qRoot=fzero(qNum,1)
qRoot =
2.1472
% tell matlab that from now on, x is a symbolic name
syms x
qSym=-4*x^2+3*x+12
qSym =
- 4*x^2 + 3*x + 12
% integrate from 0 to 2
int(qSym,x,[0 2])
ans =
58/3
int(qSym,x)
ans =
(x*(- 8*x^2 + 9*x + 72))/6
qSymInt=int(qSym,x)
qSymInt =
(x*(- 8*x^2 + 9*x + 72))/6
expand(qSymInt)
ans =
- (4*x^3)/3 + (3*x^2)/2 + 12*x
qSymDiff=diff(qSym)
qSymDiff =
3 - 8*x
qSym
qSym =
- 4*x^2 + 3*x + 12
qSymRoots=solve(qSym == 0,x)
qSymRoots =
3/8 - 201^(1/2)/8
201^(1/2)/8 + 3/8
double(qSymRoots)
ans =
-1.3972
2.1472
qSymFactors=factor(qSym)
qSymFactors =
[ -1, 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.
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)
qSymRat=-4*x^2+3*x+27
qSymRat =
- 4*x^2 + 3*x + 27
factor(qSymRat)
ans =
[ -1, 4*x + 9, x - 3]
simplify(629/969)
{Undefined function 'simplify' for input arguments of
type 'double'.}
simplify(sym('629/969'))
ans =
37/57
% vpa variable precision arithmetic
pi^2/6
ans =
1.6449
vpa(pi^2/6)
ans =
1.6449340668482264060656916626613
vpa(pi^2/6,50)
ans =
1.6449340668482264060656916626612655818462371826172
vpa(sym('pi^2/6'),50)
[Warning: Support of character vectors will be removed
in a future release. Character vectors can be used
only for variable names and numbers. Instead, to
create symbolic expressions first create symbolic
variables using 'syms'. To evaluate character vectors
and strings representing symbolic expressions, use
'str2sym'.]
[> In sym>convertExpression (line 1581)
In sym>convertChar (line 1486)
In sym>tomupad (line 1236)
In sym (line 215)]
ans =
1.6449340668482264364724151666460251892189499012068
vpa(pi^2/6,50)
ans =
1.6449340668482264060656916626612655818462371826172
% solving equations
syms a b c x qGen
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)
pretty(qGenRoots)
/ 2 \
| b + sqrt(b - 4 a c) |
| - -------------------- |
| 2 a |
| |
| 2 |
| b - sqrt(b - 4 a c) |
| - -------------------- |
\ 2 a /
qGenRootsTest=subs(qGenRoots,{a b c},{-4 3 12})
qGenRootsTest =
201^(1/2)/8 + 3/8
3/8 - 201^(1/2)/8
pretty(qGenRootsTest)
/ sqrt(201) 3 \
| --------- + - |
| 8 8 |
| |
| 3 sqrt(201) |
| - - --------- |
\ 8 8 /
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)
qGenRootsFun=matlabFunction(qGenRoots)
qGenRootsFun =
function_handle with value:
@(a,b,c)[(b.*(-1.0./2.0)-sqrt(a.*c.*-4.0+b.^2).*(1.0./2.0))./a;(b.*(-1.0./2.0)+sqrt(a.*c.*-4.0+b.^2).*(1.0./2.0))./a]
qGenRootsFun(-4,3,12)
ans =
2.1472
-1.3972
% solve e^x = -1
expSol=solve(exp(x) == -1,x)
expSol =
pi*1i
sym y
ans =
y
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)
ySol=solve(qxySym == 0,y)
ySol =
(b*x + 1)/(a*x)
collect(qxySym,x)
ans =
(a*y - b)*x - 1
[xSol ySol]=solve(x+y==7,x-y==1,x,y)
xSol =
4
ySol =
3
[xSol ySol]=solve(x^2+cos(y)==7,cosh(x)-y==1,x,y)
[Warning: Unable to solve symbolically. Returning a
numeric approximation instead.]
[> In solve (line 304)]
xSol =
-2.5330484287641517693678062683806
ySol =
5.3356247748635880949060107078797
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)
ratSymFactors=factor(ratSym)
ratSymFactors =
[ 2*x - 1, x - 1, 1/(x^3 + 2*x^2 + 9*x - 18)]
prod(ratSymFactors)
ans =
((2*x - 1)*(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)
ratSym =
-(2*x^2 - 3*x + 1)/(- x^3 - 2*x^2 + 9*x + 18)
ratSymFactors=factor(ratSym)
ratSymFactors =
[ 2*x - 1, x - 1, 1/(x - 3), 1/(x + 3), 1/(x + 2)]
prod(ratSymFactors)
ans =
((2*x - 1)*(x - 1))/((x + 2)*(x - 3)*(x + 3))
partfrac(ratSymFactors)
ans =
[ 2*x - 1, x - 1, 1/(x - 3), 1/(x + 3), 1/(x + 2)]
partfrac(ratSym)
ans =
1/(3*(x - 3)) - 3/(x + 2) + 14/(3*(x + 3))
intHard1=int(1/(x*(a*x^2+b*x+c)^(1/2)),x)
intHard1 =
-log(b/2 + c/x + (c^(1/2)*(a*x^2 + b*x + c)^(1/2))/x)/c^(1/2)
intHard1=int(1/(x*(a*x^2+b*x+c)^(3/2)),x)
intHard1 =
int(1/(x*(a*x^2 + b*x + c)^(3/2)), x)
int1=int(exp(-x^2),x)
int1 =
(pi^(1/2)*erf(x))/2
int1=int(sin(x)/x,x)
int1 =
sinint(x)
Taylor=taylor(sin(x)/x)
Taylor =
x^4/120 - x^2/6 + 1
Taylor=taylor(sin(x)/x,'Order',10)
Taylor =
x^8/362880 - x^6/5040 + x^4/120 - x^2/6 + 1
sinintTaylor=int(Taylor,x)
sinintTaylor =
x^9/3265920 - x^7/35280 + x^5/600 - x^3/18 + x
t3ot1=x^3/18/x
t3ot1 =
x^2/18
factor(18)
ans =
2 3 3
t5ot3=x^5/600/(x^3/18)
t5ot3 =
(3*x^2)/100
factor(100)
ans =
2 2 5 5