Solving a mass-spring-damper system with ode45

Hi guys
This code is for a with mass-spring-damper system
? = 50 kg
? = 100 N/m
? = 0.1 Ns/m
F = @(t, x) [x(2); -0.1/50*x(2) - 2*x(1)];
T = 0:0.001:10;
S = [0.5 0];
[t, y] = ode45(F, T, S);
plot(t,y)
legend({'Position', 'Speed'});
ylabel('Position / Speed [m / m/s]')
xlabel('Time [s]')
title(['mass-spring-damper system']);
But now the springforce is changed to ?? = 1250?3 − 1125?2 + 350?
How do i change this in the code?
Can you guys help me out
Greets Jeroen

3 件のコメント

darova
darova 2020 年 3 月 24 日
What do those indices mean?
Kebels3
Kebels3 2020 年 3 月 24 日
X^3 and x^2 and you need top put T in as variable so Fv(T)
darova
darova 2020 年 3 月 24 日
I think those formulas are connected somehow

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日

1 投票

If you are considering a mass-spring system vibrating under the influence of an external force Fv, then try the following code
T = 0:0.001:10;
S = [0.5 0];
[t, y] = ode45(@odefun, T, S);
plot(t,y)
legend({'Position', 'Speed'});
ylabel('Position / Speed [m / m/s]')
xlabel('Time [s]')
title('mass-spring-damper system');
function dydt = odefun(~,x)
F = 1*x(1)^3 - 1*x(1)^2 + 1*x(1);
dydt = [x(2);
- F - 0.1/50*x(2) - 2*x(1)];
end
Output

3 件のコメント

Francescogiuseppe Morabito
Francescogiuseppe Morabito 2020 年 5 月 2 日
編集済み: Francescogiuseppe Morabito 2020 年 5 月 3 日
Hi Ameer, I have a stupid question. I am not too confident with matlab embedded functions sometimes and this time I am having a problem is setting an analysis with ode45. I am analysing a mass spring damper system too, but mine has multiple degrees of freedom. I have a function that creates a column vector of dydt once time and a state vector are provided and it works fine. if I use it as funcion in ode45 it has problems. Searching on the web I have found this thread and since it is exactly what I need I tried running your code for comparisson... I tried to mach the two but results wewren't satisfying. the function I am using is this:
function [dydt]=eqfunODE(n,M22,K22,state_vec,time,TotalGlobForceVecReduced)
% M22 and K22 are nxn matrices (constant in time)
y1=zeros(n,1);
y2=zeros(n,1);
dydt_1=zeros(n,1);
dydt_2=zeros(n,1);
dydt=zeros(2*n,1);
y1=state_vec(1:n);
y2=state_vec(n+1:2*n);
dydt_1=y2;
dydt_2=-M22\K22*y1+M22\TotalGlobForceVecReduced(time);
%% TotalGlobForceVecReduce is an anonymous function that generates a column vector
% once time is provided
dydt = [dydt_1; dydt_2];
end
if I run it as a f=@(state_vec,time) eqfunODE(...) and I provide a time and a state vec it generates a column vector of doubles, but when I run it as you it give me this error:
Index exceeds the number of array elements (1).
Error in Dynamic_Explicit_Solver001>eqfunODE (line 117)
y1=state_vec(1:n);
Any suggestion will be very appreciated thank you !!
Ameer Hamza
Ameer Hamza 2020 年 5 月 2 日
I cannot run the code because I don't have the values of the variables. And it is difficult to diagnose an issue without that. I recommend you to start a new question and include the code used to call ode45 along with the value of variables. You can then post the link in the next comment so that I will get a notification.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2019b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by