0

Problem: Solve linear equations

I have a 3×3 matrix and I wanted to take 3 expressions as inputs which contain matrix cells like

2*b(1,1)+3*b(1,2)+3*b(1,3)
3*b(2,1)+4*b(2,3)+3*b(2,3)

and evaluate them with different cell values in matrix

 0     1     0
 1     0     0
 1     0     0

 0     1     0
 0     1     0
 1     0     0  etc.,

I used the following code, I got the result but I can only use the cell values. When I try to give expressions with numeral, it shows the following error:

*Warning: File: pro.m Line: 5 Column: 9 The expression on this line will generate an error when executed. The error will be: Error using ==> vertcat CAT arguments dimensions are not consistent.

??? Error using ==> pro at 5 Error using ==> vertcat CAT arguments dimensions are not consistent.*

Here is my code:

clc;
clear all;
close all;

cell = ['b(1,1)+b(1,2)';'b(2,1)+b(2 ,3)';'b(3,3)+b(3,2)'];
exp = cellstr(cell);
res = [0,0,0];
display(res);

display(exp);

a = zeros(3,3);

for i = 1:1:3
    a(1,i) = 1;
    if(i>1)
    a(1,i-1) = 0;
    end
    for j = 1:1:3
        a(2,j) = 1;
        if(j>1)
        a(2,j-1) = 0;
        end    
        for k = 1:1:3
            a(3,k) = 1;
            if(k>1)
            a(3,k-1) = 0;
            end
            b = a;
            res(k) = eval(exp{k});
            if res(1) == 1 
                if res(2) == 1
                    if res(3) == 1 
                        display(res);
                        display(b);
                        break;
                    end
                end
            end
        end
        a(3,k)=0;
    end
    a(2,j) = 0;
end    
;

Help how can I input strings with numerals and matrix cells...

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Saiteja Parsi
  • 412
  • 1
  • 5
  • 15
  • Not to insult you, but that's some pretty bad code you have there...Could you ignore the implementation and the problems you have with it, and explain in an edit what your end goal is? I have a suspicion you can get to that goal in like 5 lines of code... – Rody Oldenhuis Mar 20 '14 at 10:30
  • yeah! That was initial stage of my code to check whether my logic is correct. Later I implemented it recursively and made it short. – Saiteja Parsi Aug 03 '14 at 16:40

1 Answers1

0

This is not a valid expression to initialize a cell in Matlab:

cell = ['b(1,1)+b(1,2)';'b(2,1)+b(2 ,3)';'b(3,3)+b(3,2)'];

You have to use the curly brackets { }:

cell = {'b(1,1)+b(1,2)';'b(2,1)+b(2 ,3)';'b(3,3)+b(3,2)'};

BTW, if you want to solve linear equation of the form AX+b=0, you can simply try X=-inv(A)*b

% Define system
A = [2 3 1; 7 -1 1; 4 0 5];
b = [1 0 1].';
% Solve system
X = -inv(A)*b; 
tashuhka
  • 5,028
  • 4
  • 45
  • 64
  • ...why the minus sign? And why not the faster & more accurate `X = A\b`? – Rody Oldenhuis Mar 20 '14 at 10:16
  • The minus is just because of how I defined the system (*AX+b=0*). The use of `inv()` is just because I usually deal with singular matrices, so I easily can change to `pinv()`. But, for simple linear systems, `X=A\b` is indeed better: http://www.mathworks.se/help/matlab/ref/mldivide.html – tashuhka Mar 20 '14 at 10:33
  • 1
    Ah, got it. Usually it's written like `Ax=b`, and I didn't see your equation. And the use of `inv`, well, that always bugs me: **all** uses of it I have ever encountered benefited from using `lu`, `linsolv`, `mldivide`, etc., in terms of performance, stability and accuracy. So I usually make a big fuss out of it here on SO, because lots of people come here from google, half-read the question, see some code in an answer and use it, thus perpetuating the cycle of incorrect uses of `inv()` :) – Rody Oldenhuis Mar 20 '14 at 10:42
  • I support you in your crusade :) – tashuhka Mar 20 '14 at 10:49