Skip to content

Instantly share code, notes, and snippets.

@kor01
Last active December 17, 2017 00:20
Show Gist options
  • Save kor01/314fcb49a41c5c32896c6b5dae805bec to your computer and use it in GitHub Desktop.
Save kor01/314fcb49a41c5c32896c6b5dae805bec to your computer and use it in GitHub Desktop.
[matlab symbolic] symbolic computation in matlab #symbolic #matlab
% show current assumptions
syms z
assumptions(z)
% set assumptions
syms x
assume(x >= 0)
% add assumptions
assumeAlso(x,'integer')
% create variables with assumptions
a = sym('a', 'real');
b = sym('b', 'real');
c = sym('c', 'positive');
syms a b real
syms c positive
% delete assumptions
clear x
% inheritance of assumptions
syms x real
clear x
syms x
solve(x^2 + 1 == 0, x)
% clear symbolic engine
reset(symengine)
syms x
c = coeffs(16*x^2 + 19*x + 11)
% reverse order of coefficient
c = fliplr(c)
% collect w.r.t variables
syms x y
cxy = coeffs(x^3 + 2*x^2*y + 3*x*y^2 + 4*y^3, [x y])
cyx = coeffs(x^3 + 2*x^2*y + 3*x*y^2 + 4*y^3, [y x])
% collect all terms
syms x
[c,t] = coeffs(16*x^2 + 19*x + 11)
% coefficients and terms of multivariate poly
syms x y
[cx,tx] = coeffs(x^3 + 2*x^2*y + 3*x*y^2 + 4*y^3, x)
[cy,ty] = coeffs(x^3 + 2*x^2*y + 3*x*y^2 + 4*y^3, y)
% variables of x and y
syms x y
[cxy, txy] = coeffs(x^3 + 2*x^2*y + 3*x*y^2 + 4*y^3, [x,y])
[cyx, tyx] = coeffs(x^3 + 2*x^2*y + 3*x*y^2 + 4*y^3, [y,x])
% all coefficients
syms a b y
[cxy, txy] = coeffs(a*x^2 + b*y, [y x], 'All')
% use double equal to formulate an equation
syms x
solve(x^3 - 6*x^2 == 6 - 11*x)
% default == 0
syms x
solve(x^3 - 6*x^2 + 11*x - 6)
% multiple variables
syms x y
solve(6*x^2 - 6*x^2*y + x*y^2 - x*y + y^3 - y^2 == 0, y)
% system of algebraic equations
syms x y z
[x, y, z] = solve(z == 4*x, x == y, z == x^2 + y^2)
% collect linear equation by terms
syms x y
coeffs_x = collect(x^2*y + y*x - x^2 - 2*x, x)
coeffs_y = collect(x^2*y + y*x - x^2 - 2*x, y)
coeffs_xy = collect(a^2*x*y + a*b*x^2 + a*x*y + x^2, [x y])
%
% declaration
syms f(x, y)
f(x, y) = x^3*y^3
% second order differentiation
d2fy(f, y, 2)
% bind variable with expression
g = f(y + 1, y)
% single variable differentiation
syms x
f = sin(x)^2;
diff(f)
% partial derivatives
syms x y
f = sin(x)^2 + cos(y)^2;
diff(f, y)
% higher order derivatives
syms x y
f = sin(x)^2 + cos(y)^2;
diff(diff(f, y), x)
% indefinite integral
syms x
f = sin(x)^2;
int(f)
% integration w.r.t one of the variables
syms x y n
f = x^n + y^n;
int(f, y)
% definite integral
syms x y n
f = x^n + y^n;
int(f, 1, 10)
% the case with no close form
syms x
int(sin(sinh(x)))
% variable sharing
syms a b c
A = [a b c; c a b; b c a]
% matrix operations
sum(A(1,:))
% logical expression
isAlways(sum(A(1,:)) == sum(A(:,2)))
% generate matrix
A = sym('A', [2 4])
% control the naming format
A = sym('A%d%d', [2 4])
% convert numerics to symbolics
A = hilb(3)
A = sym(A)
% fplot to create 2-D plots of symbolic expressions, equations, or functions in Cartesian coordinates.
% fplot3 to create 3-D parametric plots.
% ezpolar to create plots in polar coordinates.
% fsurf to create surface plots.
% fcontour to create contour plots.
% fmesh to create mesh plots.
syms x
f = x^3 - 6*x^2 + 11*x - 6;
fplot(f)
xlabel('x')
ylabel('y')
title(texlabel(f))
grid on
% simplify
phi = (1 + sqrt(sym(5)))/2;
f = phi^2 - phi - 1
simplify(f)
% expand expression
syms x
f = (x ^2- 1)*(x^4 + x^3 + x^2 + x + 1)*(x^4 - x^3 + x^2 - x + 1);
expand(f)
% collect by coeffcient
syms a b
coeffs_xy = collect(a^2*x*y + a*b*x^2 + a*x*y + x^2, [x y])
% factorize expression
syms x
g = x^3 + 6*x^2 + 11*x + 6;
factor(g)
% nested (Horner) representation of poly nomial
syms x
h = x^5 + x^4 + x^3 + x^2 + x;
horner(h)
% subs
syms x y
f = x^2*y + 5*x*sqrt(y);
% with a constant
subs(f, x, 3)
% with another variable
subs(f, y, x)
% substitude matrix into a poly
syms x
f = x^3 - 15*x^2 - 24*x + 350;
A = [1 2 3; 4 5 6];
subs(f,A)
% replace symbols in a matrix
syms a b c
A = [a b c; c a b; b c a]
alpha = sym('alpha');
beta = sym('beta');
A(2,1) = beta;
A = subs(A,b,alpha)
% create rationals
sym(1/3);
% special constant
sym(pi)
sin(sym(pi))
% declaration
syms x
y = sym('y')
% declaration of variables
syms a b c
% declaration of a matrix of variables
A = sym('a', [3, 20])
% declare a constant
phi = (1 + sqrt(sym(5)))/2;
f = phi^2 - phi - 1
% zero ans
simplify(f)
% declare polynomials
f = a*x^2 + b*x + c;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment