Unable to plot matrix multiples

조회 수: 2 (최근 30일)
Lukas Lehrman
Lukas Lehrman 2018년 2월 9일
댓글: Birdman 2018년 2월 9일
So I'm trying to plot the sum of a matrix at 25 different values using the code below, but matlab gives me the error " Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power. ", Even though x technically is a scalar (right?). Note that i am NOT trying to simply multiply the matrix by a constant x. For x=2 I want to compute A*A (matrix multiplication).
clear all
A = [1.9 0.025;0.1 1.225];
v = [1;0];
for i = 1:25 answer = sum((A^i)*v); end
answer
x = (1:25);
y = sum((A^x)*v);
plot (x,y)
xlabel('# of growth periods')
ylabel('Total # of bacteria')

채택된 답변

Birdman
Birdman 2018년 2월 9일
편집: Birdman 2018년 2월 9일
A = [1.9 0.025;0.1 1.225];
v = [1;0];
for i = 1:25
answer(i) = sum((A^i)*v);
end
x = 1:25;
plot (x,answer)
xlabel('# of growth periods')
ylabel('Total # of bacteria')
  댓글 수: 2
Lukas Lehrman
Lukas Lehrman 2018년 2월 9일
편집: Lukas Lehrman 2018년 2월 9일
Thank you for the answer! I've since ran into yet another problem. Now I'm trying to calculate a the value of an unknown that would generate the maximum value in a matrix, but i get the error "Subscripted assignment dimension mismatch".
pvalues = [0:0.01:1];
v = [1;0];
for i=1:length(pvalues)
p=pvalues(i)
A = [2-p 0.25*p;p (1.25-(0.25*p))];
answer(i) = (A^25)*v;
end
max(answer)
I thought i could work around it, but it seems that it hasn't worked.
Birdman
Birdman 2018년 2월 9일
pvalues = 0:0.01:1;
v = [1;0];
for i=1:length(pvalues)
A = [2-pvalues(i) 0.25*pvalues(i);pvalues(i) (1.25-(0.25*pvalues(i)))];
res = (A^25)*v;
val(i)=max(res);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by