Coeffs and collect partially work

I have this symbolic function which is a series of pure waves in time, with coefficients depending on other constant (symbolic) variables, say:
x_result = f1(a,b,c) * sin(t) + f2(a,b,c) * sin(2*t) ...
and so on, where a,b,c and t are assumed real symbolic variables.
The function is a result of a symbolic solution using 'dsolve'.
When I try to get the coefficient of or , coeffs and collect work perfectly, yet when I try it with some other frequency coeffs and collect give exactly x_result; for example for they return the entire expression of x_result and not an array with f2(a,b,c) as the second term. This problem is consistent with any frequency other than 1, and regardless of if I simplify or expand x_result.
The only thing I found that corrects this, is printing x_result to the console
disp(x_result)
and then copying the outputed value and assigning x_result this copied value (i.e., x_result = -value of x_result copied from console-).
Since x_result is usually very long in my code, this method would not be helpful as after a certain point the output would be truncated.
My goal is to be able to directly obtain the coefficients of the different waves of x_result without any "by hand" manipulations.
Any help would be appreciated.

3 Comments

Using expand() won't work, because it would expand sin(n*t) and cos(n*t) (where n is an integer) in terms of sin(t) and cos(t).
Could you share the code that generates the expression x_result?
collect() works here for a expression I made up -
syms x y a b
f = sin(x)*cos(3*y) + exp(b*x)*sin(2*x)/log(a) + sin(x)*y/b^a + a*sin(2*x)/(x+y) + a-b*cos(3*y)
f = 
collect(f,[sin(2*x) cos(3*y)])
ans = 
Thanks for your response.
Unfortunately I cannot share my code. When I try it "manually" like you did it works flawlessly, yet when the expression is a result of dsolve directly it doesn't do so. When I copy the dsolve solution into the clipboard and then assign the copied value into a new expression, it also works with no problem.
The code that generates the expression is basically an iterative solution of multiple forced harmonic oscillators, for example:
syms t a b real
syms x(t)
for n=1:N
dx = diff(x, t);
ddx = diff(dx, t);
EQ = ddx + x == a*sin(t) + b*sin(2*t) + ...
X(n) = simplify(dsolve(EQ, [x(0)==0, dx(0)==0]));
end
x_result = rewrite(sum(X), 'sincos');
The result is then a simple series of cosine or sine waves of different frequencies.
Also, I've noticed that
collect(x_result, {'sin' 'cos'})
seems to be working fine.
Thanks a lot.
Sagi
Sagi on 25 Oct 2023
Edited: Sagi on 25 Oct 2023
A workaround that I found was to set the frequency as a symbolic variable as all frequencies in my specific problem are dependant on one frequency, then collect works great and I can simply substitude the frequency value I want.
Basically:
syms f
EQ = ddx + x == a*sin(f*t) + b*sin(2*f*t) + ...
And then when I try coeffs on whichever frequency in terms of f from x_result it gives me the corresponding coefficients. Then I can just substitude f for whatever value I want.
I guess that will do for now, yet still if there is a known solution to this problem it would help (as more symbolic variables take more memory).

Sign in to comment.

Answers (0)

Products

Release

R2023b

Asked:

on 24 Oct 2023

Edited:

on 25 Oct 2023

Community Treasure Hunt

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

Start Hunting!