T =
Order terms in symbolic poly converted to string
Afficher commentaires plus anciens
Is there a way to control the order of the terms in a symbolic polynomial that has been converted to string?
E.g. the following
syms x
T=taylor(exp(x),order=3);
string(T)
results in
ans = "x + x^2/2 + 1"
and I would rather have it as 1+x+x^2/2 or the reverse order. I'll be putting this in the title of a plot so if there is some other way to automatically get the correct order in the title that would work equally as well.
Réponse acceptée
Plus de réponses (1)
James Tursa
le 18 Juil 2024
Modifié(e) : James Tursa
le 18 Juil 2024
Brute force if you need the string for some reason?
syms x
ex = taylor(exp(x),order=3)
[C,T] = coeffs(ex)
terms = fliplr(C.*T)
n = numel(terms);
s = char(terms(1));
for k=2:n
c = char(terms(k));
if( c(1)=='-' )
s = [s ' - ' c(2:end)];
else
s = [s ' + ' c];
end
end
s
1 commentaire
Ethan Duckworth
le 18 Juil 2024
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


