Subscript indices error when plotting

조회 수: 1 (최근 30일)
Vidhan Malik
Vidhan Malik 2016년 3월 20일
댓글: Vidhan Malik 2016년 3월 20일
Running a relatively simple code to plot 2 variables against each other.
clear;clc;
syms A1 B2
R=0.5;
Cp=0.5;
Cv=0.3;
i = 0;
while Cv<1.11
i = i + 0.01;
Cv= Cv + 0.01;
R = Cv^2*((1/Cv-tan(A1))^2 - (tan(B2))^2)/2/(1-Cv*tan(A1) - Cv*tan(B2));
Cp = 1 - (tan(B2)/(1/Cv - tan(A1)))^2;
[A1,B1] = vpasolve([R, Cp], [A1,B2]);
SWR = 1 - Cv*(tan(A1) + tan(B2));
X(i,2) = Cv;
Y(i,2) = SWR;
end
plot(X(:,2),Y(:,2),'g-');
hold on
And the error I get is as following
Subscript indices must either be real positive integers or logicals.
Error in gasturbine_hw7 (line 22)
x(i,2) = Cv;
I think the error is due to Cv not being an integer but how do I make a matrix and call individual values from it that aren't integers without receiving that error message?
My end goal here is to plot Cv on the X axis and SWR on the Y axis.

채택된 답변

Roger Stafford
Roger Stafford 2016년 3월 20일
This is your problem:
X(i,2) = Cv;
Y(i,2) = SWR;
Matlab is objecting to the fact that 'i' is being used as an index, but its value is not a positive integer. You need to revise your code so that the index value in X and Y is a positive integer.
  댓글 수: 1
Vidhan Malik
Vidhan Malik 2016년 3월 20일
ah it was having problems with the index not the value assigned to it, okay makes sense, thanks!

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

추가 답변 (1개)

John D'Errico
John D'Errico 2016년 3월 20일
편집: John D'Errico 2016년 3월 20일
Why should CV be a problem here? What variable is a subscript? i is a variable. What values does i take on?
i = 0;
...
i = i + 0.01;
X(i,2) = Cv;
Y(i,2) = SWR;
Now read the error message again. You cannot use a non-integer number (or any zero or negative integer) as an index.
Think of it like this: Where would MATLAB store the 0.01 element of a vector? It might get a little tight between elements at times. :)
  댓글 수: 2
Vidhan Malik
Vidhan Malik 2016년 3월 20일
thanks! Yeah for some reason I was associating the word indices with some else entirely.
Vidhan Malik
Vidhan Malik 2016년 3월 20일
Just wondering if you could continue your help from before. How do you return a variable back to being a variable after you previously have defined it?
As in if A1=54 in the first loop and now for the second iteration of the loop, I want it being a variable again.
I think one way would be to move where I defined the equations to before the loop, but what if I had some reason not to? How would I go about un-defining a variable?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by