creating a script to solve simultaneous equations using matrices

14 views (last 30 days)
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 Comments
Callum Davies
Callum Davies on 23 Dec 2022
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 on 23 Dec 2022
- 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?

Sign in to comment.

Answers (1)

Vijay
Vijay on 26 Dec 2022
Edited: Vijay on 26 Dec 2022
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

Categories

Find more on Startup and Shutdown 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!