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

MaxFrost
  • 217
  • 1
  • 6

2 Answers2

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.

rinkert
  • 6,593
  • 2
  • 12
  • 31
  • 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
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

>> A\b
ans =

  -0.022191
   0.757299
am304
  • 13,758
  • 2
  • 22
  • 40
  • 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