creating a script to solve simultaneous equations using matrices

12 次查看(过去 30 天)
i need help to Write a script file to solve a system of simultaneous equations that is written in the form 𝐴𝑋 = 𝐵 where 𝐴 is a square matrix of dimensions 𝑛 × 𝑛 The script file must • Ask for the definition of matrices 𝐴 and 𝐵 • Check that matrix 𝐴 is square and display an appropriate message if it is not square. • Check whether a solution exists and display an appropriate message if a solution does not exist. • Calculate the solution for 𝑋 if it exists and display the value of 𝑋 with some appropriate text.
I have started but wasnt able to get very far as i am completely stuck and dont know where to go from here. Any help would be greatly appreciated. Here is how far i have gotten with the code.
% Reset the script
clc
clear all
close all
% Get input of the variables of the simultanious equations
a=input("First equation co-efficient of x: ");
b=input("First equation co-efficient of y: ");
c=input("Second equation co-efficient of x: ");
d=input("Second equation co-efficient of y: ");
e=input("First equation content term: ");
f=input("Second equation content term: ");
%set up the matrices
A=[a,b;c,d];
X=[x;y];
B=[e;f];
  2 个评论
Callum Davies
Callum Davies 2022-12-23
i know how to do the maths between the matrices but im not sure how to define x and y as their values are unknown. Im also unsure what format to present the output
Dyuman Joshi
Dyuman Joshi 2022-12-23
- How would you check if a matrix is square?
For the equation A*X=B, the solution is X = (A inverse) * B.
- So, to check if a solution exists or not, what is the condition?

请先登录,再进行评论。

回答(1 个)

Vijay
Vijay 2022-12-26
编辑:Vijay 2022-12-26
You can use the program below to solve the equations.
% code section
% Reset the script
clc
clear all
close all
% Get input of the variables of the simultanious equations
a=input("First equation co-efficient of x: ");
b=input("First equation co-efficient of y: ");
c=input("Second equation co-efficient of x: ");
d=input("Second equation co-efficient of y: ");
e=input("First equation content term: ");
f=input("Second equation content term: ");
%set up the matrices
A=[a,b;c,d];
%X=[x;y];
B=[e;f];
X = A\B;
%X will be a matrix of 2 x 1;
In case the solution does not exist you will have "Inf" values in matrix X. you can check that using isfinite method and provide a response a suitable response.
Hope that helps

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by