Find X,Y,Z of this linear

14 views (last 30 days)
tiara
tiara on 24 Aug 2023
Moved: Stephen23 on 24 Aug 2023
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 Comment
Stephen23
Stephen23 on 24 Aug 2023
Moved: Stephen23 on 24 Aug 2023
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]
M = 3×3
26720 472274 791273 42259 567904 1358438 49763 593542 1405099
V = [408162173304; 369152982902; 420198149812]
V = 3×1
1.0e+00 * 408162173304 369152982902 420198149812
XYZ = M\V % simpler and faster than slow symbolic operations
XYZ = 3×1
1.0e+00 * 5031741.88514984 1290280.22776427 -424192.123520388
Checking the first equation:
M(1,:)*XYZ
ans =
408162173304

Sign in to comment.

Answers (4)

Sam Chak
Sam Chak on 24 Aug 2023
Alternative approach. This should produce the same results as @Nathan Hardenberg.
A = [26 472 791;
42 567 358;
49 593 405]
A = 3×3
26 472 791 42 567 358 49 593 405
B = [ 408;
369;
1420]
B = 3×1
408 369 1420
X = A\B
X = 3×1
182.5581 -15.0968 3.5236

Nathan Hardenberg
Nathan Hardenberg on 24 Aug 2023
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;
]
equations = 
solution = solve(equations, [x y z]);
solution.x
ans = 
solution.y
ans = 
solution.z
ans = 
% To evaluate to a double/float you can do
double(solution.x)
ans = 182.5581
eval(solution.x) % or this
ans = 182.5581
  1 Comment
Stephen23
Stephen23 on 24 Aug 2023
"eval(solution.x) % or this"
Best not. EVAL is not part of the symbolic toolbox and its behavior with symbolic variables is not defined.

Sign in to comment.


Ramtej
Ramtej on 24 Aug 2023
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!

Paul Bower
Paul Bower on 24 Aug 2023
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

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!