# Help with non working for loop for generating a matrix

1 view (last 30 days)
Shing-Hei To on 30 Mar 2020
Edited: Shing-Hei To on 31 Mar 2020
clc;clf;clear;
A = zeros(2,2);
c = 0:0.1:2.23;
for x=(4-c)./(6+c)
y=10./(6+c);
A =[-1-2.*x(c)+y(c) x(c);-6.*y(c) 4-2.*c.*y(c)-6.*x(c)];
end
I am trying to plot a graph of trace-determinant of mmatrix A, where I am in need of the values of x and y varying with c in my matrix A.
The result is a vector of 0's, Values of y and care displayed fine but only one value of x in one iteration of c.
Array indices must be positive integers or
logical values.
Error in Untitled7 (line 8)
A =[-1-2.*x(c)+y(c) x(c);-6.*y(c)
4-2.*c.*y(c)-6.*x(c)];

James Tursa on 31 Mar 2020
c is a vector, so what is x(c) supposed to mean?
Can you just post the equations you are trying to code and we can help you code them?
Shing-Hei To on 31 Mar 2020
x and y is in terms of a variable c. A is a 2x2 matrix, A = [-1-2.*x+y x;-6.*y 4-2.*c.*y-6.*x]
x = (4-c)/(6-c), y = 10/(6-c)
And i want to plot a trace and determinant graph of A for 0<c<2.23
Sorry for the confusion with the code

Matt J on 31 Mar 2020
Edited: Matt J on 31 Mar 2020
One approach is to write x,y, and A as matrix-valued anonymous functions of c, e.g.,
x=@(c)(4-c)./(6+c);
y=@(c)10./(6+c);
A = @(c) [-1-2.*x(c)+y(c) x(c);-6.*y(c) 4-2.*c.*y(c)-6.*x(c)];
fplot(@(c) trace(A(c)), [0,2.23])
xlabel 'c'
ylabel 'Trace(A(c))' #### 1 Comment

Shing-Hei To on 31 Mar 2020