%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

I'm sorry, but I don't understand your problem? I tried your code and I had only problems with this line:
dips(a(i));
Abdul Burhan
Abdul Burhan 2019년 12월 5일
my main issue is right now is the fact that my program is meant to warn the issue when they put in a wrong value such as a negative mass hence the: if m<=0
disp('Redo the value for m'). however all it does when i put in a negative mass such as -5 for m is display this: Enter value of m: -5
Index exceeds array bounds.
Error in u1828202 (line 34)
disp('Redo the value for m')
Walter Roberson
Walter Roberson 2019년 12월 5일
Yes, and I gave you the solution to that already. You are assigning to a variable named disp .
Abdul Burhan
Abdul Burhan 2019년 12월 6일
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
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
Walter Roberson 2019년 12월 5일

1 개 추천

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 2019년 12월 5일

0 개 추천

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 2019년 12월 6일

0 개 추천

#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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 12월 5일

댓글:

2019년 12월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by