Find X,Y,Z of this linear
조회 수: 4 (최근 30일)
이전 댓글 표시
26.720X + 472.274Y + 791.273Z = 408.162.173.304
42.259X + 567.904Y + 1.358.438Z = 369.152.982.902
49.763X + 593.542Y + 1.405.099Z = 420.198.149.812
댓글 수: 1
Stephen23
2023년 8월 24일
이동: Stephen23
2023년 8월 24일
There is no need to use the symbolic toolbox, MATLAB offers basic and efficient numeric solvers for this task, e.g.:
format long G
M = [26720,472274,791273; 42259,567904,1358438;49763,593542,1405099]
V = [408162173304; 369152982902; 420198149812]
XYZ = M\V % simpler and faster than slow symbolic operations
Checking the first equation:
M(1,:)*XYZ
답변 (4개)
Nathan Hardenberg
2023년 8월 24일
This is very simple to do. I choose diffenent numbers, since I don't know how your "."-delimiters are meant to be.
syms x y z
equations = [
26*x + 472*y + 791*z == 408;
42*x + 567*y + 358*z == 369;
49*x + 593*y + 405*z == 1420;
]
solution = solve(equations, [x y z]);
solution.x
solution.y
solution.z
% To evaluate to a double/float you can do
double(solution.x)
eval(solution.x) % or this
댓글 수: 1
Stephen23
2023년 8월 24일
"eval(solution.x) % or this"
Best not. EVAL is not part of the symbolic toolbox and its behavior with symbolic variables is not defined.
Ramtej
2023년 8월 24일
Hi Tiara,
Please refer to Solve System of Linear Equations for the detailed instructions on how to solve system of linear equations using the Symbolic Math Toolbox.
Hope that helps!
댓글 수: 0
Paul Bower
2023년 8월 24일
Hi, You could convert the three strings given into a system of equations and solve it using a linear solver. Some code which shows how to do it is given below:
Based on this code:
This is the matrix A:
26720 472274 791273
42259 567904 1358438
49763 593542 1405099
This is the vector b:
1.0e+11 *
4.081621733040000
3.691529829020000
4.201981498120000
This is the solution x:
1.0e+06 *
5.031741885149838
1.290280227764273
-0.424192123520388
I hope this helps. Have a good day. Regards, Paul Bower
% System of equations expressed as strings
str1 = '26.720X + 472.274Y + 791.273Z = 408.162.173.304';
str2 = '42.259X + 567.904Y + 1.358.438Z = 369.152.982.902';
str3 = '49.763X + 593.542Y + 1.405.099Z = 420.198.149.812';
% Initialize matrix A and vector b
A = [];
b = [];
% Convert first string to numbers and write into A & b
[numsArray, lastNum] = extractNumbers(str1);
A = [A; numsArray];
b = [b;lastNum];
% Convert second string to numbers and write into A & b
[numsArray, lastNum] = extractNumbers(str2);
A = [A; numsArray];
b = [b;lastNum];
% Convert third string to numbers and write into A & b
[numsArray, lastNum] = extractNumbers(str3);
A = [A; numsArray];
b = [b;lastNum];
% Display A
disp('This is the matrix A:');
disp(A);
disp('This is the vector b:');
disp(b);
% Solve the system of equations
x = A\b;
disp('This is the solution, x:');
disp(x);
function [numsArray, lastNum] = extractNumbers(inputString)
% Replace the periods (which represent commas or thousands) with empty spaces
cleanedString = strrep(inputString, '.', '');
% Use regular expressions to extract numbers from the string
nums = regexp(cleanedString, '(-?\d+\.?\d*)', 'match');
% Convert the extracted numbers into doubles and store them in an array
numsArray = cellfun(@str2double, nums(1:end-1));
% Extract the last number
lastNum = str2double(nums{end});
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!