changing values of RHS with each time step in ODE

5 views (last 30 days)
i have a set of values for the force term "F" (in the equation my"+cy'+ky=F) saved in an excel file which i have recalled using:
F=xlsread('l&d.xlsx','O1:O300');
My main code looks something like:
y0=initial conditions
[tsol ysol]=ode15s('beam_function',[1:dt:T],y0);
plot(tsol,ysol);
whereas, my function code looks something like:
function [dy]=beam_function(t,y)
dy=[y(1:u-1);
inv(M)*(F-K*y-C*y)]
The problem is that i want to recall each value of F at different time steps but i dont know how to do that, can anyone guide me?

Answers (2)

Wan Ji
Wan Ji on 18 Aug 2021
Is F time dependent ? If so, just write a function named Force
function F = Force(t)
t_array = []; % This is t array from xls file
f_array = []; % This is F array from xls file
F = interp1(t_array,f_array,t);
end
And the odefun becomes
function [dy]=beam_function(t,y)
dy=[y(1:u-1);
inv(M)*(Force(t)-K*y-C*y)];
end
  9 Comments
Bhanu Pratap Akherya
Bhanu Pratap Akherya on 21 Aug 2021
understood... one more thing why did you use that equation for f_array?
Wan Ji
Wan Ji on 24 Aug 2021
Use
F = interp1(t_array,f_array,t);
is only for obtaining the force as time given. t_array and f_array is from your excel, whilst I didnot even see its format.

Sign in to comment.


Steven Lord
Steven Lord on 18 Aug 2021
Use the "ODE with Time-Dependent Terms" example on the documentation page for the ode45 function as a model. The f and g variables in that example correspond to the data from your spreadsheet, with ft and gt being the times associated with that data. Then inside the myode function in the example you would interpolate your spreadsheet data.
Passing the data that you read from the spreadsheet in as additional parameters rather than reading it in every time the ODE solver calls your ODE function will save time (potentially a lot of time if reading the data takes a while or the ODE solver needs to call your ODE function many times.)
  8 Comments
Bhanu Pratap Akherya
Bhanu Pratap Akherya on 19 Aug 2021
Here is the complete code:
Function code:
function [dy]=beam_function(t,y, t_array, f_array)
F = interp1(t_array,f_array,t);
dy=[y(1:u-1);
inv(M)*(F-K*y(u:end)-C*y(1:u-1))]
Main code:
clear all
clc
global K M C u;
Ne=6;
l=1; %length
t=0.02; %thickness
b=0.02; %width
modulus=2e11; %(E)
area=b*t;
imoment=(b*((t)^3))/12;
Le=l/Ne; %length of element
Rho=7850; %density
%Element stiffness matrix
K1=(modulus*imoment/(Le^3))*[12,6*Le,-12,6*Le; ...
6*Le,4*Le*Le,-6*Le,2*Le*Le; ...
-12,-6*Le,12,-6*Le; ...
6*Le,2*Le*Le,-6*Le,4*Le*Le];
Kglobal=zeros(2*(Ne+1),2*(Ne+1));
M1=[156 22*Le 54 -13*Le;...
22*Le 4*Le*Le 13*Le -3*Le*Le;...
54 13*Le 156 -22*Le;...
-13*Le -3*Le*Le -22*Le 4*Le*Le]*(Rho*Le*b*t)/420;
Mglobal=zeros(2*(Ne+1),2*(Ne+1));
for ii=1:Ne
Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+K1;
Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+M1;
end
K=Kglobal;
K(1:2,:)=[];
K(:,1:2)=[];
M=Mglobal;
M(1:2,:)=[];
M(:,1:2)=[];
C=0.05*Kglobal;
C(1:2,:)=[];
C(:,1:2)=[];
K
M
C
u=(2*Ne)+1;
dt=0.001;
T=300;
%Displacement initials
y0=zeros(2*(2*(Ne+1))-4,1);
y0(end-1,1)=0.5;
%ODE function
t_array = xlsread('l&d.xlsx','Q1:Q300'); % This is t array from xls file
f_array = xlsread('l&d.xlsx','O1:O300'); % This is F array from xls file
[tsol ysol]=ode15s(@(t, y) beam_function(t, y, t_array, f_array),[1:dt:T],y0);
plot(tsol,ysol(:,Ne))
Bhanu Pratap Akherya
Bhanu Pratap Akherya on 20 Aug 2021
in the excel sheet i have bunch of numbers that have been taken from a cfd software.

Sign in to comment.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!