Array of symbolic functions with index but "same" variable name

3 Ansichten (letzte 30 Tage)
Hendrik Lorenz
Hendrik Lorenz am 14 Jun. 2019
Beantwortet: Hendrik Lorenz am 14 Jun. 2019
I want to generate an array of symbolic expressions depending on variable t:
clear all
reset(symengine)
syms 'x_%d(t)' [6, 1]
x
syms x_init [6, 1]
syms 'x_term_%d' [6, 1]
syms T t0
x(t) = piecewise(t==t0, x_init, t==T, x_term, x(t))
x(t0)
x(T)
My variable x is overwritten by the new definitions of x_init and x_term because apparently _term _init are recognized as "just" indices that do not define a new variable.
When I run
clear all
reset(symengine)
syms 'x_%d(t)' [6, 1]
x
syms xinit [6, 1]
x
syms 'xterm_%d' [6, 1]
x
syms T t0
x(t) = piecewise(t==t0, xinit, t==T, xterm, x(t))
x(t0)
x(T)
I get the intended output but of course my indices _term and _init are not displayed as indices but as variable name.
Did not find any workaround for this with symfun or sym that works.
For me is important to generate also the dependency on t for x.
If anyone also has a better approach for the piecewise line, I appreciate a comment. What I want to do there is to set the initial and final condition of the time dependent function x.
Thanks for any help!

Antworten (1)

Hendrik Lorenz
Hendrik Lorenz am 14 Jun. 2019
Found a work around, though still open for a more elegant solution:
clear all
t = sym('t')
reset(symengine)
% syms 'xVec_%d(t)' [6, 1]
x = sym('x_%d', [6,1])
x(t) = cell2sym(arrayfun(@(xEl) symfun(strcat(char(xEl), '(t)'), [t]), x, 'UniformOutput', false))
x_init = sym('x_init_%d', [6,1])
x_term = sym('x_term_%d', [6,1])
syms T t0
x(t) = piecewise(t==t0, x_init, t==T, x_term, x(t))
x(t0)
assume(t0 ~= T)
x(T)

Produkte


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by