Simulataneous equation solver code
4 views (last 30 days)
Show older comments
Tadiwa on 11 Oct 2022
Commented: Tadiwa on 14 Oct 2022
Hi, I am trying to come up with a simultaneous equation solver that will ask a user for the number of simultanous equations to be solved and the number of coefficients in the simultaneous equations after which a for loop will be used to enter the number of coeffiecients for the number of equations.
Attached is the matlab code
% function for coefficient entry
function s = myfunc_Q2a(x)
s(i)=str2double(inputdlg('Enter coefficients: '));
% script that calls on the above function
g=msgbox('Equation is in the form: x1*a1 + x2*a2 + an*xn= b ');
n=str2double(inputdlg('Enter the number of simultaneous equations to be solved: '));
x=str2double(inputdlg('Enter the number of coefficients in the equation: '));
I am receiving an error that states left and right hand side have different number of elements, however when I choose
n=2 and x=1 I dont receive an error.
Ghazwan on 11 Oct 2022
is it maybe x=1 is the only integer you are entering?
You have a counter i=1:x. x has to be an integer.
Gowthami on 14 Oct 2022
You are assigning the 'x'(number of coefficients) length elements array to 'z(j)' which is 1 element of a vector. Because of this you are encountering the error 'Unable to perform assignment because the left and right sides have a different number of elements.'
For example, if n = 2 and x = 3, The left side of this line of code refers to 1 element of z. The right side refers to a 3-element array.
As you mentioned you didn't receive any error when n = 2 and x = 1, because the left side of this line of code refers to 1 element of z and the right side refers to only 1 element. So, you are not encountering any error messages.
For resolve this issue, you may use a 2-D matrix. Please find the following code snippet for the same,
You may create a 2-D matrix where each row contains one of these 'x' length arrays or you could create a cell array.
I hope it helps.
More Answers (0)
Find more on Mathematics 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!