필터 지우기
필터 지우기

Index out of bounds

조회 수: 1 (최근 30일)
bugatti79
bugatti79 2014년 8월 2일
댓글: Star Strider 2014년 8월 4일
Folks,
What is wrong with this simple code?
Fo=100;
wn=20;
k=2000;
m=5;
w=30;
x0=.1;
x0_dot=.1;
f_0=Fo/m;
for i=1:100;
X(i)=Fo/(k-m*w(i)^2);
end
figure;
plot(w,X);
xlabel('w');
ylabel('X');
I get the following error
Attempted to access w(2); index out of bounds because numel(w)=1.
Error in Example315pg326RAO (line 21)
X(i)=Fo/(k-m*w(i)^2);
Thanks

채택된 답변

Star Strider
Star Strider 2014년 8월 2일
Your w variable is defined as a constant scalar: w=30. You have to define w as a 100-element vector, because your code doesn’t make sense otherwise (specifically the loop that calculates X(i)).
  댓글 수: 2
bugatti79
bugatti79 2014년 8월 4일
Thanks
Star Strider
Star Strider 2014년 8월 4일
My pleasure!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 8월 2일
Is w a constant? Or does it change depending on what i is? If so, how? By the way, it doesn't even matter if you vectorize your code, though since you're plotting versus w it looks like it's supposed to be an array. I made it an array in the "fixed" code below:
fontSize = 24;
Fo = 100;
wn = 20;
k = 2000;
m = 5;
% Make w go from 30 to 60
w = linspace(30, 60, 100);
x0 = .1;
x0_dot = .1;
f_0= Fo / m;
X = Fo ./ (k-m*w.^2);
plot(w, X, 'b-', 'LineWidth', 3);
xlabel('w', 'fontSize', fontSize);
ylabel('X', 'fontSize', fontSize);
title('X vs. w', 'fontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
  댓글 수: 1
bugatti79
bugatti79 2014년 8월 4일
ok, guys, that makes sense. Thanks.

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by