simplify and reorganize symbolic expression

조회 수: 3 (최근 30일)
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 16일
댓글: Torsten 2024년 7월 17일
Hello everyone,
I am trying to simplify a symbolic expression (which is the result of the script below) but i am having trouble as it is pretty big and there are many parameters. My objective is to reorganize the expression "B" as powers of variables "c1" and "c2" with all the other parameters included in the coefficents of the polynomial expression (they are gonna be substitued by values in other calculations). I have tried with "simplify" and triyng to nest "collect" into one another.
Does anybody have any tips on how to make the process more efficient? Are there any functions, which I am not aware of right now, that can simplify polynomial expression with regards to the coefficients that multiply the different powers? how about with multivariable expressioon as is my case?
also, is there a way to make math expressions with powers, parentesis, fractions, etc. more readable in the command window? now i have the standard formatting setting of matlab which makes long expressions pretty hard to read
Thank you very much
clear; clc;
syms m J a1 a2
syms k1 k2 k_a12 k_a21
syms c1 c2 c_a12 c_a21
syms lambda
% matrices definitions
M = [m 0;
0 J];
K = [k1 + k2 + (k_a12+k_a21) k1*a1 - k2*a2 + (k_a21*a1 - k_a12*a2);
k1*a1 - k2*a2 + (k_a12*a1-k_a21*a2) k1*a1^2 + k2*a2^2 - (k_a12+k_a21)*a1*a2];
C = [c1 + c2 + (c_a12+c_a21) c1*a1 - c2*a2 + (c_a21*a1 - c_a12*a2);
c1*a1 - c2*a2 + (c_a12*a1-c_a21*a2) c1*a1^2 + c2*a2^2 - (c_a12+c_a21)*a1*a2];
det_M = det(M);
det_K = simplify(collect(collect(collect(collect(det(K), 2*a1), a2), a1^2), a2^2));
det_C = simplify(collect(collect(collect(collect(det(C), 2*a1), a2), a1^2), a2^2));
Mat = (lambda^2)*M + lambda*C + K;
determinant = collect(det(Mat), lambda);
coeff = coeffs(determinant, lambda);
b0 = simplify(collect(collect(collect(collect(coeff(1), k_a12), k_a21), k2), k1))
b1 = simplify(collect(collect(collect(collect(coeff(2), 2*a1), a2), a1^2), a2^2))
b2 = simplify(collect(collect(collect(collect(collect(collect(coeff(3), m), J), c1), c2), c_a12), c_a21))
b3 = simplify(collect(collect(collect(collect(collect(collect(coeff(4), a1^2), a2^2), a1), a2), m), J))
B = b1*b2*b3 - b1^2 - b0*b3^2;
B = expand(B);
B = simplify(collect(B, c1))
this is B that i get:
(a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 - c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))*(c1*k2 + c2*k1 - c_a12*k_a21 - c_a21*k_a12)*(m*(a1^2*k1 + a2^2*k2 - a1*a2*k_a12 - a1*a2*k_a21) + J*(k1 + k2 + k_a12 + k_a21) + c1*c2*(a1 + a2)^2 - c_a12*c_a21*(a1 + a2)^2) - (a1 + a2)^2*(m*(c1*a1^2 + (- c_a12 - c_a21)*a1*a2 + c2*a2^2) + J*(c1 + c2 + c_a12 + c_a21))^2*(k1*k2 - k_a12*k_a21) - (a1 + a2)^4*(c1*k2 + c2*k1 - c_a12*k_a21 - c_a21*k_a12)^2

채택된 답변

Torsten
Torsten 2024년 7월 16일
편집: Torsten 2024년 7월 16일
The only symbolic function that comes to mind is "coeffs". If this function does not do what you want, I fear you are lost here.
This example should help for your case:
syms x y
P = x^4 + 3*x*y^3 + 2*x^2*y^6 + y^4 - 3*x^2*y^2 + y + 1;
cx = coeffs(P,x,'All');
cy = coeffs(P,y,'All');
nx = numel(cx);
ny = numel(cy);
Coefficients = sym(zeros(nx,ny));
for i = 0:nx-1
for j = 0:ny-1
pxiyj = diff(diff(P,x,i),y,j);
c0xiyj = subs(pxiyj,[x y],sym([0 0]));
c0xiyj = c0xiyj/factorial(i)/factorial(j);
Coefficients(i+1,j+1) = c0xiyj;
end
end
Coefficients
Coefficients = 
  댓글 수: 2
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 17일
thank you, i will try with coeff, and work and try to symplify every coefficient alone
Torsten
Torsten 2024년 7월 17일
As you might have noticed, in the above code Coefficients(i,j) is the coefficient of P related to the power x^(i-1)*y^(j-1) . It should be easy to apply the code to your polynomial B depending on c1 and c2.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by