Coefficients in simple equations, y not represented all the time

조회 수: 1 (최근 30일)
katherine keogh
katherine keogh 2021년 1월 12일
답변: Zuber Khan 2024년 9월 19일
I have the equations x=0, and z=50000. Written as a=x==0, and b=z==50000. Potentially in other examples maybe all variables (x,y,z) are represented in the equations, I would like matlab to tell me the coefficients of x,y and z in both equations. For example the x=0 equation would tell me I have 1 x, 0y, 0z as the coefficents. Another exmaple is the other equation would tell me I have 0x,0y and 1 z.
  댓글 수: 2
katherine keogh
katherine keogh 2021년 1월 12일
Okay I have it so that I use children to seperate the equations from the numbers at the end
and then I can get the coefficients but I want it so that it will tell me if there are no xs or zs or ys rather than just omitting the 0s
dpb
dpb 2021년 1월 12일
편집: dpb 2021년 1월 13일
More info on what you're doing and how to use this probably would lead to some other feedback, but if you have the Statistics or CurveFitting TB there are options to use a fitobject or linearmodel object that contains information on coefficients, variables, etc., ...
Other than that, a mechanism such as holding the coefficients vector where the terms are related positionally is about the best one can do in ordinary procedural code; one could write a special class of one's own to deal with somewhat similarly as do the two objects above without all the bells and whistles.
Or, there is the Symbolic TB to treat that way...I've never used/had it so not all that familiar.

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

답변 (1개)

Zuber Khan
Zuber Khan 2024년 9월 19일
Hi,
When working with symbolic expressions in MATLAB, you might find it necessary to extract the coefficients of specific variables, ensuring that even those variables which are not present in the expression are accounted for, with a zero coefficient. To accomplish this, the coeffs function of Symbolic Math Toolbox can be a valuable tool.
Firstly, you need to define the symbolic variables and equations involved as follows.
% Define symbolic variables
syms x y z
% Define the equations
eq1 = 2*x == 0;
eq2 = z == 50000;
eq3 = x+y-3*z == 23;
Then you can write a function to extract the coefficients. Kindly note that you need to take care of the use cases as mentioned below to eliminate any logical errors in the code.
  • Case 1: When the mathematical expression has a single term. For instance, x, 4*z etc.
  • Case 2: When the mathematical expression has more than one term. For instance, x-20, 4*z-3*x-5 etc.
I am attaching the code snippet below for your reference.
% Create a list of all variables
vars = [x, y, z];
% Extract coefficients for each equation
coeffs_eq1 = extractCoefficients(eq1, vars);
coeffs_eq2 = extractCoefficients(eq2, vars);
coeffs_eq3 = extractCoefficients(eq3, vars);
% Display results
disp('Coefficients for eq1 (2*x=0):');
disp(['x: ', num2str(coeffs_eq1(1)), ', y: ', num2str(coeffs_eq1(2)), ', z: ', num2str(coeffs_eq1(3))]);
disp('Coefficients for eq2 (z=50000):');
disp(['x: ', num2str(coeffs_eq2(1)), ', y: ', num2str(coeffs_eq2(2)), ', z: ', num2str(coeffs_eq2(3))]);
disp('Coefficients for eq3 (x+y-3*z==23):');
disp(['x: ', num2str(coeffs_eq3(1)), ', y: ', num2str(coeffs_eq3(2)), ', z: ', num2str(coeffs_eq3(3))]);
% Function to extract coefficients
function coff = extractCoefficients(eq, vars)
% Convert equation to symbolic expression
expr = lhs(eq) - rhs(eq);
% Initialize coefficients array
coff = zeros(1, length(vars));
% Possible operators combining symoblic variables
operators = {'+', '-'};
% Extract coefficients for each variable
for i = 1:length(vars)
[cof,term] = coeffs(expr, vars(i));
Operators = operators(cellfun(@(op) contains(char(expr), op), operators));
if ~isempty(Operators)
% Muti-term expression
if length(term)==1
coff(i) = 0; % If no term found, coefficient is zero
else
coff(i) = cof(1); % If term exists, equal to coefficient
end
else
% Single term expression
if contains(string(expr),string(vars(i)))
coff(i) = cof(1); % Extract the coefficient for term, if it exists
else
coff(i) = 0;
end
end
end
end
This will address your query. If you have any concerns, drop a comment and I will be glad to assist you further.
Regards,
Zuber

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by