CompMath-Vorlesung 13.11.2020

Demonstration symbolischer Methoden in Matlab, Siehe auch die gute Einfuehrung dazu.

Contents

Symbolische Funktionen

clear all; 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

a^2-2*a*b+b^2
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 in Faktoren
prod(fe)          % ausmultiplizieren der Faktoren
Manipulation algebraischer Ausdruecke
 
f =
 
a^2 - 2*a*b + b^2
 
 
fe =
 
[ a - b, a - b]
 
 
ans =
 
(a - b)^2
 

Ausdrücke vereinfachen

disp('simple anwenden')
% simple(f)         % (bestmoeglich) vereinfachen, zeigt alle Versuche
ff=simplify(f)      % (bestmoeglich) vereinfachen, zeigt alle Versuche
expand(ff)          % ausmultiplizieren der Ausdrücke
simple anwenden
 
ff =
 
(a - b)^2
 
 
ans =
 
a^2 - 2*a*b + b^2
 

Algebraische Gleichungen

Loese x^2=16

Loese nach der einzigen Variablen auf: symbolisch

clear;
%clc;
disp('Loese nach der einzigen Variablen auf')
% Loese x^2=16:  Variante 1
syms x
assume(x,'clear')                   % ensure that all old assumtions are deleted
solve(x^2 == 16)

% Loese x^2=16:  Variante 2
syms x;
y = x^2-16;
solve(y==0, x)       % Loest  y(x) = 0
Loese nach der einzigen Variablen auf
 
ans =
 
 -4
  4
 
 
ans =
 
 -4
  4
 

Loese symbolisch mit Zusatzannahme

clear;
syms x
assume(x,'clear')                   % ensure that all old assumtions are deleted
f = x^4 - x^3 - 5*x^2 - 7*x - 84
disp('Loese ohne Zusatzannahme')
xsol = solve(f==0)

disp('x soll reell sein')
assume(x,'real')
xsol = solve(f==0)

disp('x soll negativ sein')
assumeAlso(x<0)
xsol = solve(f==0)

disp('assumptions(x) :')
assumptions(x)
 
f =
 
x^4 - x^3 - 5*x^2 - 7*x - 84
 
Loese ohne Zusatzannahme
 
xsol =
 
          -3
           4
 -7^(1/2)*1i
  7^(1/2)*1i
 
x soll reell sein
 
xsol =
 
 -3
  4
 
x soll negativ sein
 
xsol =
 
-3
 
assumptions(x) :
 
ans =
 
[ in(x, 'real'), x < 0]
 

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

Stelle y = 2*x^2+4 nach x um

clear;
%clc;
disp('Loese nach einer bestimmten Variablen auf')
syms x y;
f  = 2*x^2+4-y       %  Notwendig:  f(x,y) = 0
% f = sin(x)-x/2-0.1;
aa = solve(f==0,x)
pretty(aa)
% Schoenere Ausgabe
Loese nach einer bestimmten Variablen auf
 
f =
 
2*x^2 - y + 4
 
 
aa =
 
 -(2^(1/2)*(y - 4)^(1/2))/2
  (2^(1/2)*(y - 4)^(1/2))/2
 
/   sqrt(2) sqrt(y - 4) \
| - ------------------- |
|            2          |
|                       |
|  sqrt(2) sqrt(y - 4)  |
|  -------------------  |
\           2           /

Schraenke das Loesen auf x>0 ein.

assume(x>0)                 % nur positive Lösungen
a1 = solve(f==0,x)          % matlab weist auf Lösungsbedingung hin

assume(4<y)                 % Lösungsbedingung bzgl.  y  festlegen
a2 = solve(f==0,x)
Warning: Solutions are valid under the following conditions: 4 < y. To include
parameters and conditions in the solution, specify the 'ReturnConditions' value
as 'true'. 
 
a1 =
 
(2^(1/2)*(y - 4)^(1/2))/2
 
 
a2 =
 
(2^(1/2)*(y - 4)^(1/2))/2
 

Loese nach der einzigen Variablen auf: numerisch mit vpasolve()

syms x;
y = x^2-16;
% y = sin(x)-x/2-0.1;

disp('Numerisch mit  vpasolve():')
x0  = -1;
%
% sol = vpasolve(y)         % funktioniert auch
sol = vpasolve(y, x0)
disp(['Startloesung ', num2str(x0), ' ergibt Lösung: ']);
sol
Numerisch mit  vpasolve():
 
sol =
 
 -4.0
  4.0
 
Startloesung -1 ergibt Lösung: 
 
sol =
 
 -4.0
  4.0
 

Loese nach der einzigen Variablen auf: numerisch fzero()

syms x;
y = x^2-16;

disp('Numerisch mit  fzero():')
x0  = -1;
%
%f_hand = @(x)subs(y,x);          % Functionshandle aus symbolischer Fkt. erzeugen (alt)
f_hand = matlabFunction(y);       % numerische Funktion <-- symbolische Funktion
sol = fzero(f_hand, x0)
disp(['Loesung  ',num2str(sol),'  bei Startloesung ', num2str(x0)]);
Numerisch mit  fzero():

sol =

    -4

Loesung  -4  bei Startloesung -1

Algebraische Gleichungssysteme

Lineares System

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

Variante 1: symbolisch (mit Parameter) via solve()

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

SOLUTION = 

  struct with fields:

    a: [1×1 sym]
    b: [1×1 sym]

 
ans =
 
6 - c
 
 
ans =
 
c - 3
 

Variante 2: numerisch via \

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

x =

     0
     3

Nichtlineares System

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

Variante 1: symbolisch via solve()

             f_1(a,b) := 3a^2 + b^2 - 1 = 0
             f_2(a,b) := a   + b   - 1 = 0
clear;
disp('Nichtlineares System  Variante 1: symbolisch ')
syms a b;
f = [ 3*a^2 + b^2 - 1, a   + b   - 1 ]
[A,B] = solve(f)
%  bzw
SOLUTION = solve(f)
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 
 
f =
 
[ 3*a^2 + b^2 - 1, a + b - 1]
 
 
A =
 
   0
 1/2
 
 
B =
 
   1
 1/2
 

SOLUTION = 

  struct with fields:

    a: [2×1 sym]
    b: [2×1 sym]

 
ans =
 
   0
 1/2
 
 
ans =
 
   1
 1/2
 
 
Solution =
 
[   0,   1]
[ 1/2, 1/2]
 
 
Solution1 =
 
[ 0, 1]
 

nochmal symbolisch, aber mit Parameter in Loesung via solve()

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

numerische Loesung via vpasolve()

                            3a^2 + b^2 = 1
                             a   + b   = 1
disp('Nichtlineares System  Variante 2: numerisch vpnsolve()')
syms a b;
f = [ 3*a^2 + b^2-1 ;  a + b-1];         % keine Leerzeichen vor '-1', da vpasolve() ansonsten Probleme hat
x_0 = [0.3, 0.6];

sol = vpasolve(f, [a,b])                 % auch vpasolve(f, [a,b], x_0)  möglich
% sol = vpasolve([3*a^2 + b^2-1, a+b-1], [a,b])

% disp(['Numerische Loesung bei Startloesung ',num2str(x_0)])
disp(['Numerische Loesung:'])
sol.a
sol.b
Nichtlineares System  Variante 2: numerisch vpnsolve()

sol = 

  struct with fields:

    a: [2×1 sym]
    b: [2×1 sym]

Numerische Loesung:
 
ans =
 
   0
 0.5
 
 
ans =
 
 1.0
 0.5
 

numerische Loesung via fsolve() [benoetigt die Optimization Toolbox]

                            3a^2 + b^2 = 1
                             a   + b   = 1
disp('Nichtlineares System  Variante 3: numerisch fsolve')

x_0 = [0.3, 0.6];
sol = fsolve(@(x)[3*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 3: numerisch fsolve

Equation solved.

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


sol =

    0.5000    0.5000

Numerische Loesung  0.5         0.5 bei Startloesung 0.3         0.6

Integrieren und Differenzieren nach einer Variablen

$$f = x^2 - 3*x + 4$$

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
 

Int+Diff+Vereinfachen

syms x
f  = exp(x)*cos(x)
F = int(f,x)
Fx = diff(F,x)
Fx-f                 % das soll == 0 sein?
simplify(Fx-f)       % OK, jetzt glaube ich das.
 
f =
 
exp(x)*cos(x)
 
 
F =
 
(exp(x)*(cos(x) + sin(x)))/2
 
 
Fx =
 
(exp(x)*(cos(x) + sin(x)))/2 + (exp(x)*(cos(x) - sin(x)))/2
 
 
ans =
 
(exp(x)*(cos(x) + sin(x)))/2 - exp(x)*cos(x) + (exp(x)*(cos(x) - sin(x)))/2
 
 
ans =
 
0
 

Integrieren und Differenzieren mehrdimensionaler Funktionen

$$f = [a^2 + b^2 - 1, a + b - 1]$$

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)            % 1. Ableitung 1. Funktion bzgl. a
f1_a = diff(f(1),2)            % 2. Ableitung 1. Funktion bzgl. a

int(f(1))                      % unbestimmtes Integral bzgl. letzer Variable

int(f(1),a)                    % unbestimmtes Integral bzgl. Variable a

int(f(1),a,1,2)                % bestimmtes Integral bzgl. Variable a
Integrieren und Differenzieren mehrdimensionaler Funktionen
 
f =
 
[ a^2 + b^2 - 1, a + b - 1]
 
 
f1_a =
 
2*a
 
 
f1_a =
 
2
 
 
ans =
 
b^3/3 + b*(a^2 - 1)
 
 
ans =
 
a^3/3 + a*(b^2 - 1)
 
 
ans =
 
b^2 + 4/3
 

Gradient, Laplace, Rotation, Divergenz

clear
%clc
syms x y z a
f = x^3+sin(y)^2+exp(a*z)

% Gradient von f
disp('Gradient von f')
gradient(f,[x,y,z])

% Laplace von f
disp('Laplace von f')
laplacian(f,[x,y,z])

% Divergenz von g
g = [x^3+sin(y)^2+exp(a*z); exp(x)-log(z); y*z+x*y]
disp('Divergenz von g')
divergence(g,[x,y,z])

% Rotation von g
disp('Rotation von g')
curl(g,[x,y,z])
 
f =
 
x^3 + sin(y)^2 + exp(a*z)
 
Gradient von f
 
ans =
 
           3*x^2
 2*cos(y)*sin(y)
      a*exp(a*z)
 
Laplace von f
 
ans =
 
6*x + 2*cos(y)^2 - 2*sin(y)^2 + a^2*exp(a*z)
 
 
g =
 
 x^3 + sin(y)^2 + exp(a*z)
           exp(x) - log(z)
                 x*y + y*z
 
Divergenz von g
 
ans =
 
3*x^2 + y
 
Rotation von g
 
ans =
 
              x + z + 1/z
           a*exp(a*z) - y
 exp(x) - 2*cos(y)*sin(y)
 

Grenzwerte:

from docu for limit : $$ \lim_{x\to\infty} (1+\frac{a}{x})^x $$

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

Evaluate the sum of the following multivariable expression with respect to k:

from docu for 'symsum'

$$\sum_{k=0}^\infty \frac{x^k}{k!}$$

syms x k;
pretty(symsum(x^k/factorial(k), k, 0, inf))

% anderes Bsp: $ \sum_{k=1}^\infty \frac{1}{k^2} $
pretty(symsum(1/k^2, k, 1, inf))
exp(x)

  2
pi
---
 6

Publishing publish('v_6_b.m')