index exceeds array bounds
조회 수: 5 (최근 30일)
이전 댓글 표시
%Program for simple
this is my code but when i run it this comes up:
Enter value of m: -5
Index exceeds array bounds.
Error in u1828202 (line 34)
disp('Redo the value for m')
mass spring damper system
m=20; %Default mass in kg
k1=100; %default spring constant in N/m
k2=50; %default spring constant in N/m
b1=2; %Damping coefficient in N sm^-1
b2=2; %Damping coefficient in N sm^-1
ttotal=50; %total integration time in s
d=0.1; %Gap in m
dt=0.01; %Integration step in s
e=-1.0;%Initial displacement in s
t=[0:dt:ttotal];
n=length(t);
x=zeros(n,1);
v=zeros(n,1);
a=zeros(n,1);
x(1)=e;
v(1)=0;
a(1)=-(k1*x(1)+b1*v(1))/m;
for i=2:n
v(i)=a(i-1)*dt+v(i-1);
x(i)=v(i)*dt+x(i-1);
if x(i)<d
a(i)=-k1*x(i)+b1*v(i)/m;
else
end
end
%prompt for user-defined parameters
m=input('Enter value of m: ');
if (m<=0)
disp('Redo the value for m')
end
k1=input('Enter value of k1: ');
k2=input('Enter value of k2: ');
b1=input('Enter value of b1: ');
b2=input('Enter value of b2: ');
ttotal=input('Enter value for ttotal: ');
d=input('Enter value for gap: ');
dt=input('Enter value for integration step: ');
x=input('Enter value for initial displacement: ');
disp=("Acceleration is now equal to: ")
disp(a(i));
disp=('Velocity is now equal to: ');disp(v(i));
댓글 수: 5
Walter Roberson
2019년 12월 7일
Change
disp=("Acceleration is now equal to: ")
to
disp("Acceleration is now equal to: ")
Change
m=input('Enter value of m: ');
if (m<=0)
disp('Redo the value for m')
end
to
while true
m=input('Enter value of m: ');
if m > 0
break;
end
disp('Redo the value for m')
end
답변 (3개)
Walter Roberson
2019년 12월 5일
disp=("Acceleration is now equal to: ")
That line redefines disp from being a function into being a scalar string object. You need
disp("Acceleration is now equal to: ")
댓글 수: 0
Image Analyst
2019년 12월 5일
See these snippets. It lets you ask user for interger or floating point number(s) with default values, and warning if the value is not good.
Adapt as needed.
% Ask user for one integer number.
defaultValue = 45;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nTry replacing the user.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
%=======================================================================================================
% % Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
댓글 수: 0
KALYAN ACHARJYA
2019년 12월 6일
#Is this one, please do check & confirm?
while(1)
% mass spring damper system
m=20; %Default mass in kg
k1=100; %default spring constant in N/m
k2=50; %default spring constant in N/m
b1=2; %Damping coefficient in N sm^-1
b2=2; %Damping coefficient in N sm^-1
ttotal=50; %total integration time in s
d=0.1; %Gap in m
dt=0.01; %Integration step in s
e=-1.0;%Initial displacement in s
t=[0:dt:ttotal];
n=length(t);
x=zeros(n,1);
v=zeros(n,1);
a=zeros(n,1);
x(1)=e;
v(1)=0;
a(1)=-(k1*x(1)+b1*v(1))/m;
for i=2:n
v(i)=a(i-1)*dt+v(i-1);
x(i)=v(i)*dt+x(i-1);
if x(i)<d
a(i)=-k1*x(i)+b1*v(i)/m;
else
end
end
%prompt for user-defined parameters
disp("Acceleration is now equal to: ")
disp(a);
disp('Velocity is now equal to: ');
disp(v);
run=input('Do you want to continue, Y/N [Y]:','s')
if m=='N'
break
end
k1=input('Enter value of k1 (spring constant): ');
k2=input('Enter value of k2 (spring constant in N/m): ');
b1=input('Enter value of b1 (Damping coefficient in N sm^-1): ');
b2=input('Enter value of b2 (Damping coefficient in N sm^-1): ');
ttotal=input('Enter value for ttotal(integration time in s): ');
d=input('Enter value for gap: ');
dt=input('Enter value for integration step: ');
x=input('Enter value for initial displacement: ');
m=input('Enter value of m (Gap): ');
while m<=0
disp('Alert ## Redo the value for m')
m=input('Again Enter value of m (Gap): ');
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!