Solving system of equations on MATLAB, when a constant exists in variable matrix?












0















How do I solve the following system of equations on MATLAB when one of the elements of the variable vector is a constant? Please do give the code if possible.



More generally, if the solution is to use symbolic math, how will I go about generating large number of variables, say 12 (rather than just two) even before solving them?



enter image description here










share|improve this question





























    0















    How do I solve the following system of equations on MATLAB when one of the elements of the variable vector is a constant? Please do give the code if possible.



    More generally, if the solution is to use symbolic math, how will I go about generating large number of variables, say 12 (rather than just two) even before solving them?



    enter image description here










    share|improve this question



























      0












      0








      0








      How do I solve the following system of equations on MATLAB when one of the elements of the variable vector is a constant? Please do give the code if possible.



      More generally, if the solution is to use symbolic math, how will I go about generating large number of variables, say 12 (rather than just two) even before solving them?



      enter image description here










      share|improve this question
















      How do I solve the following system of equations on MATLAB when one of the elements of the variable vector is a constant? Please do give the code if possible.



      More generally, if the solution is to use symbolic math, how will I go about generating large number of variables, say 12 (rather than just two) even before solving them?



      enter image description here







      matlab equation-solving






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:23







      MaxFrost

















      asked Nov 13 '18 at 12:55









      MaxFrostMaxFrost

      215




      215
























          2 Answers
          2






          active

          oldest

          votes


















          2














          For example, create a number of symbolic variables using syms, and then make the system of equations like below.



          syms a1 a2

          A = [matrix]
          x = [1;a1;a2];
          y = [1;0;0];

          eqs = A*x == y

          sol = solve(eqs,[a1, a2])

          sol.a1
          sol.a2




          In case you have a system with many variables, you could define all the symbols using syms, and solve it like above.



          You could also perform a parameter optimization with fminsearch. First you have to define a cost function, in a separate function file, in this example called cost_fcn.m.



          function J = cost_fcn(p)

          % make sure p is a vector
          p = reshape(p, [length(p) 1]);

          % system of equations, can be linear or nonlinear
          A = magic(12); % your system, I took some arbitrary matrix
          sol = A*p;

          % the goal of the system of equations to reach, can be zero, or some other
          % vector
          goal = zeros(12,1);

          % calculate the error
          error = goal - sol;

          % Use a cost criterion, e.g. sum of squares
          J = sum(error.^2);

          end


          This cost function will contain your system of equations, and goal solution. This can be any kind of system. The vector p will contain the parameters that are being estimated, which will be optimized, starting from some initial guess. To do the optimization, you will have to create a script:



          % initial guess, can be zeros, or some other starting point
          p0 = zeros(12,1);

          % do the parameter optimization
          p = fminsearch(@cost_fcn, p0);


          In this case p0 is the initial guess, which you provide to fminsearch. Then the values of this initial guess will be incremented, until a minimum to the cost function is found. When the parameter optimization is finished, p will contain the parameters that will result in the lowest error for your system of equations. It is however possible that this is a local minimum, if there is no exact solution to the problem.






          share|improve this answer


























          • Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

            – MaxFrost
            Nov 13 '18 at 14:21













          • @MaxFrost see my edit.

            – rinkert
            Nov 13 '18 at 16:06



















          2














          Your system is over-constrained, meaning you have more equations than unknown, so you can't solve it. What you can do is find a least square solution, using mldivide. First re-arrange your equations so that you have all the constant terms on the right side of the equal sign, then use mldivide:



          >> A = [0.0297 -1.7796; 2.2749 0.0297; 0.0297 2.2749]
          A =

          0.029700 -1.779600
          2.274900 0.029700
          0.029700 2.274900

          >> b = [1-2.2749; -0.0297; 1.7796]
          b =

          -1.274900
          -0.029700
          1.779600

          >> Ab
          ans =

          -0.022191
          0.757299





          share|improve this answer
























          • I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

            – MaxFrost
            Nov 13 '18 at 14:12













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281491%2fsolving-system-of-equations-on-matlab-when-a-constant-exists-in-variable-matrix%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          For example, create a number of symbolic variables using syms, and then make the system of equations like below.



          syms a1 a2

          A = [matrix]
          x = [1;a1;a2];
          y = [1;0;0];

          eqs = A*x == y

          sol = solve(eqs,[a1, a2])

          sol.a1
          sol.a2




          In case you have a system with many variables, you could define all the symbols using syms, and solve it like above.



          You could also perform a parameter optimization with fminsearch. First you have to define a cost function, in a separate function file, in this example called cost_fcn.m.



          function J = cost_fcn(p)

          % make sure p is a vector
          p = reshape(p, [length(p) 1]);

          % system of equations, can be linear or nonlinear
          A = magic(12); % your system, I took some arbitrary matrix
          sol = A*p;

          % the goal of the system of equations to reach, can be zero, or some other
          % vector
          goal = zeros(12,1);

          % calculate the error
          error = goal - sol;

          % Use a cost criterion, e.g. sum of squares
          J = sum(error.^2);

          end


          This cost function will contain your system of equations, and goal solution. This can be any kind of system. The vector p will contain the parameters that are being estimated, which will be optimized, starting from some initial guess. To do the optimization, you will have to create a script:



          % initial guess, can be zeros, or some other starting point
          p0 = zeros(12,1);

          % do the parameter optimization
          p = fminsearch(@cost_fcn, p0);


          In this case p0 is the initial guess, which you provide to fminsearch. Then the values of this initial guess will be incremented, until a minimum to the cost function is found. When the parameter optimization is finished, p will contain the parameters that will result in the lowest error for your system of equations. It is however possible that this is a local minimum, if there is no exact solution to the problem.






          share|improve this answer


























          • Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

            – MaxFrost
            Nov 13 '18 at 14:21













          • @MaxFrost see my edit.

            – rinkert
            Nov 13 '18 at 16:06
















          2














          For example, create a number of symbolic variables using syms, and then make the system of equations like below.



          syms a1 a2

          A = [matrix]
          x = [1;a1;a2];
          y = [1;0;0];

          eqs = A*x == y

          sol = solve(eqs,[a1, a2])

          sol.a1
          sol.a2




          In case you have a system with many variables, you could define all the symbols using syms, and solve it like above.



          You could also perform a parameter optimization with fminsearch. First you have to define a cost function, in a separate function file, in this example called cost_fcn.m.



          function J = cost_fcn(p)

          % make sure p is a vector
          p = reshape(p, [length(p) 1]);

          % system of equations, can be linear or nonlinear
          A = magic(12); % your system, I took some arbitrary matrix
          sol = A*p;

          % the goal of the system of equations to reach, can be zero, or some other
          % vector
          goal = zeros(12,1);

          % calculate the error
          error = goal - sol;

          % Use a cost criterion, e.g. sum of squares
          J = sum(error.^2);

          end


          This cost function will contain your system of equations, and goal solution. This can be any kind of system. The vector p will contain the parameters that are being estimated, which will be optimized, starting from some initial guess. To do the optimization, you will have to create a script:



          % initial guess, can be zeros, or some other starting point
          p0 = zeros(12,1);

          % do the parameter optimization
          p = fminsearch(@cost_fcn, p0);


          In this case p0 is the initial guess, which you provide to fminsearch. Then the values of this initial guess will be incremented, until a minimum to the cost function is found. When the parameter optimization is finished, p will contain the parameters that will result in the lowest error for your system of equations. It is however possible that this is a local minimum, if there is no exact solution to the problem.






          share|improve this answer


























          • Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

            – MaxFrost
            Nov 13 '18 at 14:21













          • @MaxFrost see my edit.

            – rinkert
            Nov 13 '18 at 16:06














          2












          2








          2







          For example, create a number of symbolic variables using syms, and then make the system of equations like below.



          syms a1 a2

          A = [matrix]
          x = [1;a1;a2];
          y = [1;0;0];

          eqs = A*x == y

          sol = solve(eqs,[a1, a2])

          sol.a1
          sol.a2




          In case you have a system with many variables, you could define all the symbols using syms, and solve it like above.



          You could also perform a parameter optimization with fminsearch. First you have to define a cost function, in a separate function file, in this example called cost_fcn.m.



          function J = cost_fcn(p)

          % make sure p is a vector
          p = reshape(p, [length(p) 1]);

          % system of equations, can be linear or nonlinear
          A = magic(12); % your system, I took some arbitrary matrix
          sol = A*p;

          % the goal of the system of equations to reach, can be zero, or some other
          % vector
          goal = zeros(12,1);

          % calculate the error
          error = goal - sol;

          % Use a cost criterion, e.g. sum of squares
          J = sum(error.^2);

          end


          This cost function will contain your system of equations, and goal solution. This can be any kind of system. The vector p will contain the parameters that are being estimated, which will be optimized, starting from some initial guess. To do the optimization, you will have to create a script:



          % initial guess, can be zeros, or some other starting point
          p0 = zeros(12,1);

          % do the parameter optimization
          p = fminsearch(@cost_fcn, p0);


          In this case p0 is the initial guess, which you provide to fminsearch. Then the values of this initial guess will be incremented, until a minimum to the cost function is found. When the parameter optimization is finished, p will contain the parameters that will result in the lowest error for your system of equations. It is however possible that this is a local minimum, if there is no exact solution to the problem.






          share|improve this answer















          For example, create a number of symbolic variables using syms, and then make the system of equations like below.



          syms a1 a2

          A = [matrix]
          x = [1;a1;a2];
          y = [1;0;0];

          eqs = A*x == y

          sol = solve(eqs,[a1, a2])

          sol.a1
          sol.a2




          In case you have a system with many variables, you could define all the symbols using syms, and solve it like above.



          You could also perform a parameter optimization with fminsearch. First you have to define a cost function, in a separate function file, in this example called cost_fcn.m.



          function J = cost_fcn(p)

          % make sure p is a vector
          p = reshape(p, [length(p) 1]);

          % system of equations, can be linear or nonlinear
          A = magic(12); % your system, I took some arbitrary matrix
          sol = A*p;

          % the goal of the system of equations to reach, can be zero, or some other
          % vector
          goal = zeros(12,1);

          % calculate the error
          error = goal - sol;

          % Use a cost criterion, e.g. sum of squares
          J = sum(error.^2);

          end


          This cost function will contain your system of equations, and goal solution. This can be any kind of system. The vector p will contain the parameters that are being estimated, which will be optimized, starting from some initial guess. To do the optimization, you will have to create a script:



          % initial guess, can be zeros, or some other starting point
          p0 = zeros(12,1);

          % do the parameter optimization
          p = fminsearch(@cost_fcn, p0);


          In this case p0 is the initial guess, which you provide to fminsearch. Then the values of this initial guess will be incremented, until a minimum to the cost function is found. When the parameter optimization is finished, p will contain the parameters that will result in the lowest error for your system of equations. It is however possible that this is a local minimum, if there is no exact solution to the problem.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 16:06

























          answered Nov 13 '18 at 13:09









          rinkertrinkert

          1,434417




          1,434417













          • Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

            – MaxFrost
            Nov 13 '18 at 14:21













          • @MaxFrost see my edit.

            – rinkert
            Nov 13 '18 at 16:06



















          • Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

            – MaxFrost
            Nov 13 '18 at 14:21













          • @MaxFrost see my edit.

            – rinkert
            Nov 13 '18 at 16:06

















          Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

          – MaxFrost
          Nov 13 '18 at 14:21







          Your solution works. I had to make slight modifications to the matrix equations as they were over-constrained. But any idea how to solve for 12 such variables because the constant at the top still remains and I am forced to use symbolic math to generate the variables a1 a2... a12 using syms ?

          – MaxFrost
          Nov 13 '18 at 14:21















          @MaxFrost see my edit.

          – rinkert
          Nov 13 '18 at 16:06





          @MaxFrost see my edit.

          – rinkert
          Nov 13 '18 at 16:06













          2














          Your system is over-constrained, meaning you have more equations than unknown, so you can't solve it. What you can do is find a least square solution, using mldivide. First re-arrange your equations so that you have all the constant terms on the right side of the equal sign, then use mldivide:



          >> A = [0.0297 -1.7796; 2.2749 0.0297; 0.0297 2.2749]
          A =

          0.029700 -1.779600
          2.274900 0.029700
          0.029700 2.274900

          >> b = [1-2.2749; -0.0297; 1.7796]
          b =

          -1.274900
          -0.029700
          1.779600

          >> Ab
          ans =

          -0.022191
          0.757299





          share|improve this answer
























          • I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

            – MaxFrost
            Nov 13 '18 at 14:12


















          2














          Your system is over-constrained, meaning you have more equations than unknown, so you can't solve it. What you can do is find a least square solution, using mldivide. First re-arrange your equations so that you have all the constant terms on the right side of the equal sign, then use mldivide:



          >> A = [0.0297 -1.7796; 2.2749 0.0297; 0.0297 2.2749]
          A =

          0.029700 -1.779600
          2.274900 0.029700
          0.029700 2.274900

          >> b = [1-2.2749; -0.0297; 1.7796]
          b =

          -1.274900
          -0.029700
          1.779600

          >> Ab
          ans =

          -0.022191
          0.757299





          share|improve this answer
























          • I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

            – MaxFrost
            Nov 13 '18 at 14:12
















          2












          2








          2







          Your system is over-constrained, meaning you have more equations than unknown, so you can't solve it. What you can do is find a least square solution, using mldivide. First re-arrange your equations so that you have all the constant terms on the right side of the equal sign, then use mldivide:



          >> A = [0.0297 -1.7796; 2.2749 0.0297; 0.0297 2.2749]
          A =

          0.029700 -1.779600
          2.274900 0.029700
          0.029700 2.274900

          >> b = [1-2.2749; -0.0297; 1.7796]
          b =

          -1.274900
          -0.029700
          1.779600

          >> Ab
          ans =

          -0.022191
          0.757299





          share|improve this answer













          Your system is over-constrained, meaning you have more equations than unknown, so you can't solve it. What you can do is find a least square solution, using mldivide. First re-arrange your equations so that you have all the constant terms on the right side of the equal sign, then use mldivide:



          >> A = [0.0297 -1.7796; 2.2749 0.0297; 0.0297 2.2749]
          A =

          0.029700 -1.779600
          2.274900 0.029700
          0.029700 2.274900

          >> b = [1-2.2749; -0.0297; 1.7796]
          b =

          -1.274900
          -0.029700
          1.779600

          >> Ab
          ans =

          -0.022191
          0.757299






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 13:18









          am304am304

          12.1k21431




          12.1k21431













          • I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

            – MaxFrost
            Nov 13 '18 at 14:12





















          • I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

            – MaxFrost
            Nov 13 '18 at 14:12



















          I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

          – MaxFrost
          Nov 13 '18 at 14:12







          I missed that. Yes, you are right, actual problem was supposed to have only the two zeros on RHS. And size of the coefficient matrix was supposed to be 2 x 3

          – MaxFrost
          Nov 13 '18 at 14:12




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281491%2fsolving-system-of-equations-on-matlab-when-a-constant-exists-in-variable-matrix%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python