필터 지우기
필터 지우기

Finding coefficients of variables of specific powers

조회 수: 6 (최근 30일)
Connor LeClaire
Connor LeClaire 2021년 12월 4일
댓글: Connor LeClaire 2021년 12월 4일
Hello,
I am trying to find coefficients of variables of specific powers, or of 2+ variables multiplied together, however I am running into issues.
Problem 1:
syms x
y = x^2 + 3;
disp(coeffs(y,x));
The equation y does not include a x^1 term, however the returned matrix from coeffs is (3 1) which could be the same for the equation y = x + 3. I need something like (3 0 1) to show zero coefficients of intermediate powers of the variable.
Problem 2:
Trying to find coefficients of multiple variables. For instance z = x^2*y + 3y + 5x^2 +4y^2 + 3xyr + 5
If I try to find the coefficients of xy I want it to return 3r and ignore the x^2y term. Is there an easy way to do this or is it a nested (or 2-line) coeffs? If so this also runs into problem #1 should the equation not include the powers of that variable.
Thanks!

채택된 답변

Voss
Voss 2021년 12월 4일
Problem 1: Try using the 'All' flag in your call to coeffs.

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 12월 4일
syms x y r
z = x^2*y + 3*y + 5*x^2 +4*y^2 + 3*x*y*r + 5
z = 
[coeffsOfX, powersOfX] = coeffs(z, x, 'all')
coeffsOfX = 
powersOfX = 
[coeffsOfX2y, powersOfY] = coeffs(coeffsOfX(powersOfX == x^2), y, 'all')
coeffsOfX2y = 
powersOfY = 
coefficientOfX2NoY = coeffsOfX2y(powersOfY == 1)
coefficientOfX2NoY = 
5
  댓글 수: 2
Connor LeClaire
Connor LeClaire 2021년 12월 4일
This is great! I would accept the answer if I hadn't posted 2 different problems in the question... sorry
Connor LeClaire
Connor LeClaire 2021년 12월 4일
Should there exist no 2nd order power of x for example, using the index of powersOfX == x^2 throw an error?

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

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by