Why does my plot say undefined variable or function for X2?

조회 수: 2 (최근 30일)
Ash Maxwell
Ash Maxwell 2020년 4월 30일
댓글: Steven Lord 2020년 4월 30일
Lv = 4.5;
Lh = 2.5;
Pmax = 83;
MaxXcvalue = 0.75;
Xc = 0.25:0.01:0.75;
Areaofvertical = (Lv*X);
Areaofhorizontal = (Lh*X);
Areaofsoil = (4.5)*(1.5);
for X = 1:Xc ;
Ww = (Unitweight)*(((Lv)*(X))+((Lh)*(X)))*1;
Leverarmofvertical = (0.5+(X/2));
Leverarmofhorizontal = (Lh/2);
Leverarmofsoil = (0.5 + (X) + (1.5/2));
Moment = ((Areaofvertical)*(Leverarmofvertical))+((Areaofhorizontal)*(Leverarmofhorizontal))+((Areaofsoil)*(Leverarmofsoil));
Totalarea = (Areaofvertical)+(Areaofhorizontal)+(Areaofsoil);
X1 = (Moment)/(Totalarea);
X2 = X1 - ((Pmax)/(Ww))*((Lv + X)/(3));
end;
plot(X, X2);
grid on;
[SL: Formatted code as code]

답변 (2개)

Jeremy
Jeremy 2020년 4월 30일
Your code returns an undefined function or variable X error, because you try to define
Areaofvertical = (Lv*X);
before you have defined X. Maybe you meant Areaofvertical to use Xc?
  댓글 수: 2
Ash Maxwell
Ash Maxwell 2020년 4월 30일
I have tried that but it still does not seem to like it. When I put it in the while loop it comes up with the graph but with no values plotted.
Jeremy
Jeremy 2020년 4월 30일
well, I think this is your problem:
Xc = 0.25:0.01:0.75;
for X = 1:Xc
I do not believe the line X = 1:Xc is doing what you think it's doing, since Xc is an array of values, not a single value. Perhaps instead, you should try
for X = Xc

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


Steven Lord
Steven Lord 2020년 4월 30일
There are several problems with your code, but the main one is that your for loop body never executes.
for X = 1:Xc
Since Xc is a non-scalar, MATLAB will only use the first element of that vector when constructing the vector of values over which X iterates. So the loop will run once per element of this vector:
iterates = 1:0.25
You probably want to have X iterate from 1 to the numel of Xc, operating on element X of Xc in turn and assigning into element X of X2.
  댓글 수: 2
Ash Maxwell
Ash Maxwell 2020년 4월 30일
편집: Ash Maxwell 2020년 4월 30일
I understand how it iterates one, but I don't quite understand what you mean for the solution. Could you perhaps explain it in Layman's terms? Thank you so much for the suggestion!
Steven Lord
Steven Lord 2020년 4월 30일
x = randperm(10, 5); % random selection (without replacement) of 5 elements in 1:10
y = x.^2;
for k = 1:numel(x)
fprintf("%d squared is %d.\n", x(k), y(k))
end
If I'd iterated over x, I could have asked for say the 10th element of x and y, but they'll only have 5 elements.

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

카테고리

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