MATLAB Answers

Help with non working for loop for generating a matrix

1 view (last 30 days)
Shing-Hei To
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)];

  2 Comments

James Tursa
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
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

Sign in to comment.

Accepted Answer

Matt J
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
Shing-Hei To on 31 Mar 2020
Thanks for the quick reply!
By any chance, could i plot a graph of trace(A) against det(A)?
edit: I have solved it now. Thank you so much!

Sign in to comment.

More Answers (0)

Sign in to answer this question.