Write a program that K, B and M are constant and supplied by the user input. Use T as the ending time value that solutions are sought ( also entered by the user when calling the function.

2 次查看(过去 30 天)
My^''+By^'+Ky=x(t)
K = spring constant
B = dampening constant
M = mass
T = time
X = forcing function
Y = position of the mass
If you are given K, B, and M along with a table that gives values of x for a given t;
t (sec) [ 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2]
x ( kg m/s^2) [0 0 0.1 0.2 0.3 0.18 0.1 0 0 0 0]
Write a program that K, B and M are constant and supplied by the user input. Use T as the ending time value that solutions are sought ( also entered by the user when calling the function.
Initial values are t = 0 , y(0) = 0 , y’(0) = 0
The program outputs a flag ( runs without issue flag = 0) , t (vector of time values), y ( vector of position values), and yp ( vector of derivative values).

回答(2 个)

Jayant
Jayant 2023-6-18
The differential equation will look like this :
diffeq = @(t, y) [y(2); (x(t)/M) - (K/M)*y(1) - (B/M)*y(2)];
Now, solve the differential equation using ode45 ODE solver. Refer this documentation to write the diffeq solver.
Lastly, yp = y(:,2);

Image Analyst
Image Analyst 2023-6-18
This snippet to ask the user for input may help:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter values';
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
This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by