MATLAB: How to make a function to plot values of a Matrix

functionlinearsystemMATLABmatrix

For a linear system A*x = B where A and B are known
Yv = [-Y 0 0; 0 Y 0; 0 0 Y];
A = [5 -4 4; -5 4 3; 5 3 1];
AY = A + Y; % ALSO TRIED USING: Instead of AY Y= [(5-Y) -4 4; -5 (4+Y) 3; 5 3 (Y+1)]
B = [1; 4; -2;];
inverseA = inv(AY);
x = inverseA*B; %vector x must satisfy
for n = 1:size(B)
xr = x(n);
% resultxt = 'Value %2.0f of vector x is %6.3f \n' ;
% fprintf(resultxt, n, xr)
a = x(1);
b = x(2);
c = x(3);
This returns me x1 x2 and x3 for the matrix in forms of a b and c respectively. I am trying to plot the values of x1 for
Y = linspace (1,12)
which does not work given that the (vector) sizes don't match. Any ideas? Thanks

Best Answer

Y = linspace(1,12) ;
a = zeros(size(Y)) ;
b = zeros(size(Y)) ;
c = zeros(size(Y)) ;
for i = 1:length(Y)
Yv = [-Y(i) 0 0; 0 Y(i) 0; 0 0 Y(i)];
A = [5 -4 4; -5 4 3; 5 3 1];
AY = A + Y(i); % ALSO TRIED USING: Instead of AY Y= [(5-Y) -4 4; -5 (4+Y) 3; 5 3 (Y+1)]
B = [1; 4; -2;];
inverseA = inv(AY);
x = inverseA*B; %vector x must satisfy
% x = A\B ;
for n = 1:size(B)
xr = x(n);
% resultxt = 'Value %2.0f of vector x is %6.3f \n' ;
% fprintf(resultxt, n, xr)
a(i) = x(1);
b(i) = x(2);
c(i) = x(3);
end
end
hold on
plot(Y,a,'r')
plot(Y,b,'b')
plot(Y,c,'g')
legend([{'a'}, {'b'},{'c'}])
Related Question