index exceeds array bounds

2 vues (au cours des 30 derniers jours)
Abdul Burhan
Abdul Burhan le 5 Déc 2019
Commenté : Walter Roberson le 7 Déc 2019
%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 commentaires
Abdul Burhan
Abdul Burhan le 6 Déc 2019
try to enter a negative value for mass when my program asks you too and you'll see what it says.thats what my issue is now i`ll focus on the other one later
Walter Roberson
Walter Roberson le 7 Déc 2019
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

Connectez-vous pour commenter.

Réponses (3)

Walter Roberson
Walter Roberson le 5 Déc 2019
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: ")

Image Analyst
Image Analyst le 5 Déc 2019
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

KALYAN ACHARJYA
KALYAN ACHARJYA le 6 Déc 2019
#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

Catégories

En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by