Simulataneous equation solver code

1 次查看(过去 30 天)
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=zeros;
for i=1:x
s(i)=str2double(inputdlg('Enter coefficients: '));
end
end
% script that calls on the above function
g=msgbox('Equation is in the form: x1*a1 + x2*a2 + an*xn= b ');
waitfor(g)
n=str2double(inputdlg('Enter the number of simultaneous equations to be solved: '));
x=str2double(inputdlg('Enter the number of coefficients in the equation: '));
A=zeros;
B=zeros;
s=zeros;
for j=1:n
z(j)=myfunc_Q2a(x);
end
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.
  1 个评论
Ghazwan
Ghazwan 2022-10-11
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
Gowthami 2022-10-14
Hi Tadiwa,
Here at
z(j)=myfunc_Q2a(x);
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,
z=zeros(n,x);
for j=1:n
z(j,:)=myfunc_Q2a(x);
end
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by