How to display answers in original form, rather than automatically in pretty() form ?

14 views (last 30 days)
clear;clc;
s = tf('s')
tf = (1+s)*(2+s)/(10+s)
MATLAB automatically prints tf in a form with no '*' or slash '/'.
tf =
s^2 + 3 s + 2
-------------
s + 10
Question: How to have it print the answer with '*' and '/' ?
Since I have difficulty in converting tf type to syms type, so I want to copy the result to another file manually and set the same variable to syms type.
.
p.s. I've found an answer that converts tf to syms using tfdata() and latex, but I still want to know the answer of my question.
  1 Comment
Walter Roberson
Walter Roberson on 29 Dec 2015
You should avoid using tf both as the name of a function and as the name of a variable.
Try char() the symbolic variable.

Sign in to comment.

Answers (1)

James Richard
James Richard on 26 Nov 2018
Edited: James Richard on 26 Nov 2018
Don't use tf(), instead use syms and print it using disp()
You can use any other syms variable if you are using s as tf variable.
You musn't use tf as a variable name, as it will messed up the tf internals, you can't also change it into s = tf('s') later again, till you clear the tf variable using clear tf.
The errors may look like this
Index exceeds the number of array elements (1).
Error in sym/subsref (line 870)
R_tilde = builtin('subsref',L_tilde,Idx);
or this
'tf' appears to be both a function and a variable. If this is unintentional, use 'clear tf' to remove the variable 'tf'
from the workspace.
For your question, the code will be like this
syms s;
laplace_tf = (1+s)*(2+s)/(10+s);
disp(laplace_tf);
The outputs will be like this
((s + 1)*(s + 2))/(s + 10)
To change it into tf again, overwrite the syms variable with tf('s') and tf variable with the same expression.
syms s;
laplace_tf = (1+s)*(2+s)/(10+s);
% ... any code
s = tf('s');
laplace_tf = (1+s)*(2+s)/(10+s);
% ... any code
By the way, by using tf function, the output you will get is already simplified. By using syms you can also use it, to print the transfer function before matlab simplified it by using pretty() function, it can be handy somehow.
syms s;
laplace_tf = (1+s)*(2+s)/(10+s);
disp(laplace_tf);
pretty(laplace_tf);
The output will be like this
((s + 1)*(s + 2))/(s + 10)
(s + 1) (s + 2)
---------------
s + 10
If your tf contains many non-integer numbers, it may get messy, as it will printed in fraction format, for example, second order brushed DC motor transfer function.
J = 32e-7;
b = 1.694e-6;
K = 0.173;
R = 1;
L = 6.9e-3;
syms s;
laplace_tf = K/((J*s+b)*(L*s+R)+K^2);
disp(laplace_tf);
pretty(laplace_tf);
The output,
173/(1000*(((69*s)/10000 + 1)*((944473296573929*s)/295147905179352825856 + 7999688821981179/4722366482869645213696) + 2156611731961145/72057594037927936))
173
---------------------------------------------------------------------------------------------
/ / 69 s \ / 944473296573929 s 7999688821981179 \ 2156611731961145 \
| | ----- + 1 | | --------------------- + ---------------------- | + ----------------- | 1000
\ \ 10000 / \ 295147905179352825856 4722366482869645213696 / 72057594037927936 /
For that, you can use vpa() function instead
J = 32e-7;
b = 1.694e-6;
K = 0.173;
R = 1;
L = 6.9e-3;
syms s;
laplace_tf = K/((J*s+b)*(L*s+R)+K^2);
digits(16);
disp(vpa(laplace_tf));
pretty(vpa(laplace_tf));
Trial and error the digits argument untill you get the desired result, in this case 16 looks good enough (there are trade-offs with the precision, use it wisely). The outputs will be like this
0.173/((0.0000032*s + 0.000001694)*(0.0069*s + 1.0) + 0.029929)
0.173
-------------------------------------------------------
(0.0000032 s + 0.000001694) (0.0069 s + 1.0) + 0.029929

Community Treasure Hunt

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

Start Hunting!