How I explicit one function in terms of another function using symbolic?

syms t T
syms a(t) c0(t) d(t)
I(t) = a(0)*c0(t) + a(2)*c0(t - 2*T) + a(4)*c0(t - 4*T)
Q(t) = a(1)*c0(t - T) + a(3)*c0(t - 3*T) + a(5)*c0(t - 5*T)
s(t) = I(t)*cos(t) - Q(t)*sin(t)
Suppose I wish to express s(t) in terms of I(t) and Q(t). That is, I wish to see
s(t) = I(t)*cos(t) - Q(t)*sin(t)
in the command window. How i do that?
OBS: I don't wanna substitute the awser by I(t) and Q(t). I just wanna explicit I(t) and Q(t) in my expression.
(I know, it's don't seems productive. But in longs expressions, it do).

8 commentaires

Can't you just ype in Command Window:
I(t)*cos(t) - Q(t)*sin(t)
No. Because it's just an example. In my real symbolic computation, i want express my long symbolic function in terms of another symbolic function.
There is pretty() function. What about it?
There is a very obscure way of doing it, but unless you take obscure special steps, when you try to display the result, your symbolic engine would be reset. You have to use MuPAD facilities that are not recognized by the MATLAB level.
darova, pretty() function doesn't solve my problem, it's just a arcaic function for to print s(t). The output is:
cos(t) (a(0) c0(t) + a(2) c0(t - 2 T) + a(4) c0(t - 4 T))
- sin(t) (a(1) c0(t - T) + a(3) c0(t - 3 T) + a(5) c0(t - 5 T))
So, it's don't work for me. I need see:
I(t)*cos(t) - Q(t)*sin(t)
Walter Roberson, how can I do that?
I had thought it was easy :(
You would need to evalin(symengine) or feval(symengine) using hold() constructs on the symbols I(t) and Q(t) to prevent them from being evaluated to the definitions you gave for the functions. However, most of the time at least one mupad eval() operation is done on your behalf, often at least three of them, and that would replace the symbols with the contents. You would need to experiment at the feval() or evalin() level to find the perfect number of nested hold() operations to put around the symbols so that the mupad print operation applied to the expression removed all of hold objects without execution of the symbols to their content while still permitting the expression to be evaluated more fully to get the expanded form when needed.
It would be a heck of a lot easier if you were willing to live with a printed representation that did not use exactly the same symbol names, and then used subs() when you wanted to expand.
Walter Roberson , Thanks very much. It's seems pretty complicated, actually. I expected to do it easily, because it's so basic (my though). However, if I will do it, I take a look in this topic to refresh my mind. Thks!

Connectez-vous pour commenter.

 Réponse acceptée

You define functions I(t) and Q(t) and then use the expression
s(t) = I(t)*cos(t) - Q(t)*sin(t)
and hope to see I(t) and Q(t) in the result even though those have been defined as functions.
In order to do that, you would somehow have to prevent I(t) and Q(t) from executing, and yet not lose their definitions. That involves hacking the way that MATLAB evaluates expressions, which is not going to end well.
You would find it easier to do
syms t T
syms a(t) c0(t) d(t)
syms I_(t) Q(t)
s(t) = I_(t)*cos(t) - Q(t)*sin(t)
I_(t) = a(0)*c0(t) + a(2)*c0(t - 2*T) + a(4)*c0(t - 4*T)
Q(t) = a(1)*c0(t - T) + a(3)*c0(t - 3*T) + a(5)*c0(t - 5*T)
Now if you ask to display s, you would get
I_(t)*cos(t) - Q(t)*sin(t)
and you could then do
subs(s)
to have I_ and Q evaluated to get
cos(t)*(a(0)*c0(t) + a(2)*c0(t - 2*T) + a(4)*c0(t - 4*T)) - sin(t)*(a(1)*c0(t - T) + a(3)*c0(t - 3*T) + a(5)*c0(t - 5*T))
Note the change I had to make to change I(t) to I_(t) . This change is because the symbolic engine treats I specially because inside the symbolic engine, I is used to refer to the imaginary constant sqrt(-1) .
If you ***really** want s to be in terms of I(t) then you can instead use
syms t T
syms a(t) c0(t) d(t)
syms I(t) Q(t)
s(t) = I(t)*cos(t) - Q(t)*sin(t)
I(t) = a(0)*c0(t) + a(2)*c0(t - 2*T) + a(4)*c0(t - 4*T)
Q(t) = a(1)*c0(t - T) + a(3)*c0(t - 3*T) + a(5)*c0(t - 5*T)
s
subs(s, {str2sym('I(t)'), str2sym('Q(t)')}, {I, Q})

2 commentaires

Fantastic Walter Roberson, you solved my problem in a simple way!
You mean I need declare s first. Then i declare Q and I_, right? I swapped the these lines just out for curiosity, like that:
syms t T
syms a(t) c0(t) d(t)
syms I_(t) Q(t)
I_(t) = a(0)*c0(t) + a(2)*c0(t - 2*T) + a(4)*c0(t - 4*T)
Q(t) = a(1)*c0(t - T) + a(3)*c0(t - 3*T) + a(5)*c0(t - 5*T)
s(t) = I_(t)*cos(t) - Q(t)*sin(t)
I this mode, if I ask to display s, I would get:
s(t) =
cos(t)*(a(0)*c0(t) + a(2)*c0(t - 2*T) + a(4)*c0(t - 4*T)) - sin(t)*(a(1)*c0(t - T) + a(3)*c0(t - 3*T) + a(5)*c0(t - 5*T))
But If I follow your order, I achieve my goal, this is, I get:
s(t) =
I_(t)*cos(t) - Q(t)*sin(t)
Yes, exactly. If you assign to s(t) first in terms of the unresolved symbolic functions I_(t) and Q(t) then if you just display s(t) then you get it in terms of the unresolved names, and when you then want to do the full expansion, subs() will do the expansion.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by