Contents

% Demo der Symbolic Toolbox

Symbolische Funktionen

clear; clc;

disp('Symbolische Funktionen');
%  Deklaration
syms a b c;        % deklariere symbolische Variablen
f = a+b+c          % symbolische Funktion

% Ersetzen von Variablen durch Zahlenwerten oder Ausdruecke
subs(f, a, 3)      % Ersetze a durch 3

subs(f, a, b)      % Ersetze a durch (bereits als symb. def.) Variable b

subs(f, a, 'x')    % Ersetze a durch (noch nicht als symb.) Variable x

subs(f, a, (b+c)^2)  % Ersetze a durch den Term (b+c)^2

subs(f, [a,b,c], [1,2,3])  % Ersetze die Variablen durch die Werte/Terme
Symbolische Funktionen
f =
a + b + c
ans =
b + c + 3
ans =
2*b + c
ans =
b + c + x
ans =
b + c + (b + c)^2
ans =
     6

Manipulation algebraischer Ausdruecke

clear; clc;

disp('Manipulation algebraischer Ausdruecke');
syms a b;          % deklariere symb. Variablen
f = a^2-2*a*b+b^2 % symb. Funktion

fe = factor(f)    % faktorisieren

expand(fe)        % ausmultiplizieren

disp('simple anwenden')
simple(f)         % (bestmoeglich) vereinfachen
Manipulation algebraischer Ausdruecke
f =
a^2 - 2*a*b + b^2
fe =
(a - b)^2
ans =
a^2 - 2*a*b + b^2
simple anwenden
simplify:
(a - b)^2
radsimp:
a^2 - 2*a*b + b^2
simplify(100):
(a - b)^2
combine(sincos):
a^2 - 2*a*b + b^2
combine(sinhcosh):
a^2 - 2*a*b + b^2
combine(ln):
a^2 - 2*a*b + b^2
factor:
(a - b)^2
expand:
a^2 - 2*a*b + b^2
combine:
a^2 - 2*a*b + b^2
rewrite(exp):
a^2 - 2*a*b + b^2
rewrite(sincos):
a^2 - 2*a*b + b^2
rewrite(sinhcosh):
a^2 - 2*a*b + b^2
rewrite(tan):
a^2 - 2*a*b + b^2
mwcos2sin:
a^2 - 2*a*b + b^2
collect(b):
b^2 + ((-2)*a)*b + a^2
ans =
(a - b)^2

Algebraische Gleichungen

Loese nach der einzigen Variablen auf

clear; clc;
disp('Loese nach der einzigen Variablen auf')
% Loese x^2=16:  Variante 1
solve('x^2 = 16')

% Loese x^2=16:  Variante 2
syms x;
y = x^2-16;
solve(y, x)       % Loest  y(x) = 0

% Numerisch mit  fzero()
disp('Numerisch mit  fzero():')

x0  = 1;
%
f_hand = @(x)subs(y,x);          % Functionshandle aus symbolischer Fkt. erzeugen
sol = fzero(f_hand, x0)
disp(['Loesung  ',num2str(sol),'  bei Startloesung ', num2str(x0)]);
Loese nach der einzigen Variablen auf
ans =
 -4
  4
ans =
 -4
  4
Numerisch mit  fzero():
sol =
     4
Loesung  4  bei Startloesung 1

Loese nach einer bestimmten Variablen auf ("Umstellen nach")

clear; clc;
disp('Loese nach einer bestimmten Variablen auf')
% Stelle y = 2*x^2+4  nach x um
syms x y;
f = 2*x^2+4-y       %  Notwendig:  f(x,y) = 0
solve(f,x)
Loese nach einer bestimmten Variablen auf
f =
2*x^2 - y + 4
ans =
 -(2^(1/2)*(y - 4)^(1/2))/2
  (2^(1/2)*(y - 4)^(1/2))/2

Algebraische Gleichungssysteme

Lineares System

                             a +   b = 3
                             a + 2*b = 6

Variante 1: symbolisch

    via      f_1(a,b) := a +   b - 3 = 0
             f_2(a,b) := a + 2*b - 6 = 0
clear; clc;
disp('Lineares System  Variante 1: symbolisch')
syms a b;
f(1) = a +   b - 3;
f(2) = a + 2*b - 6;
[A,B] = solve(f(1),f(2))         % 2 symb. Variable als Ergebnis
%  bzw
SOLUTION = solve(f(1),f(2))      % Cell Array als Ergebnis
SOLUTION.a
SOLUTION.b
Lineares System  Variante 1: symbolisch
A =
0
B =
3
SOLUTION = 
    a: [1x1 sym]
    b: [1x1 sym]
ans =
0
ans =
3

Variante 2: numerisch

   via loesen von           K*x = f     (x enthaelt Loesung fuer [a;b])
clear; clc;
disp('Lineares System  Variante 2: numerisch')
% Koeffizientenmatrix
K = [1 1; 1 2]
% rechte Seite
f = [ 3; 6]
% Loesen des linearen Gleichungssystems
x = K\f
Lineares System  Variante 2: numerisch
K =
     1     1
     1     2
f =
     3
     6
x =
     0
     3

Nichtlineares System

                             a^2 + b^2 = 1
                             a   + b   = 1

Variante 1: symbolisch

    via      f_1(a,b) := a^2 + b^2 - 1 = 0
             f_2(a,b) := a   + b   - 1 = 0
clear; clc;
disp('Nichtlineares System  Variante 1: symbolisch ')
syms a b;
f(1) = 3*a^2 + b^2 - 1;
f(2) = a   + b   - 1;
[A,B] = solve(f(1),f(2))
%  bzw
SOLUTION = solve(f(1),f(2))
SOLUTION.a       % beide Loesungen werden ausgegeben
SOLUTION.b
% Eine einzelne Loesung extrahieren
Solution = [SOLUTION.a, SOLUTION.b]
Solution1 = Solution(1,:)
Nichtlineares System  Variante 1: symbolisch 
A =
   0
 1/2
B =
   1
 1/2
SOLUTION = 
    a: [2x1 sym]
    b: [2x1 sym]
ans =
   0
 1/2
ans =
   1
 1/2
Solution =
[   0,   1]
[ 1/2, 1/2]
Solution1 =
[ 0, 1]

nochmal symbolisch, aber mit Variablen in Loesung

disp('Nichtlineares System  Variante 1: symbolisch mit Variablen in Loesung')
syms c d;
f(1) = a^2 + b^2 - 3*c;
f(2) = a   + b   - d;
[A,B] = solve(f(1),f(2),a,b)
Nichtlineares System  Variante 1: symbolisch mit Variablen in Loesung
A =
 d/2 + (6*c - d^2)^(1/2)/2
 d/2 - (6*c - d^2)^(1/2)/2
B =
 d/2 - (6*c - d^2)^(1/2)/2
 d/2 + (6*c - d^2)^(1/2)/2

numerische Loesung via fsolve();

disp('Nichtlineares System  Variante 2: numerisch')

x_0 = [0.5, 0.6];
sol = fsolve(@(x)[x(1)^2 + x(2)^2 - 1; x(1)   + x(2)   - 1], x_0)
disp(['Numerische Loesung  ',num2str(sol),' bei Startloesung ',num2str(x_0)])
Nichtlineares System  Variante 2: numerisch

Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.



sol =
   -0.0000    1.0000
Numerische Loesung  -1.6575e-10           1 bei Startloesung 0.5         0.6

Integrieren und Differenzieren nach einer Variablen

clear; clc;

disp('Integrieren und Differenzieren nach einer Variablen')
syms x;
f = x^2 - 3*x + 4
% Differenzieren
diff(f)              % oder  diff('x^2 - 3*x + 4')

% Integrieren
int(f)               % unbestimmtes Integral
int(f,x)
int(f,0,1)           % bestimmtes Integral
int(f,x,0,1)
Integrieren und Differenzieren nach einer Variablen
f =
x^2 - 3*x + 4
ans =
2*x - 3
ans =
(x*(2*x^2 - 9*x + 24))/6
ans =
(x*(2*x^2 - 9*x + 24))/6
ans =
17/6
ans =
17/6

Integrieren und Differenzieren mehrdimensionaler Funktionen

clear; clc;

disp('Integrieren und Differenzieren mehrdimensionaler Funktionen')
syms a b
f = [a^2 + b^2 - 1, a + b - 1]

Jac = jacobian(f);

f1_a = diff(f(1),a)

int(f(1))

int(f(1),a)

int(f(1),a,1,2);
Integrieren und Differenzieren mehrdimensionaler Funktionen
f =
[ a^2 + b^2 - 1, a + b - 1]
f1_a =
2*a
ans =
b^3/3 + (a^2 - 1)*b
ans =
a^3/3 + (b^2 - 1)*a

Limit

from docu for limit

clc
syms x a;
pretty(limit((1 + a/x)^x, x, inf))

% from docu for 'symsum'
% Evaluate the sum of the following multivariable expression with respect to k:
syms x k;
pretty(symsum(x^k/sym('k!'), k, 0, inf))

% anderes Bsp:
pretty(symsum(1/k^2, k, 1, inf))
  exp(a)

  exp(x)

    2
  pi
  ---
   6