How can I use values from datafile as a time dependent function in ODE system?

Hi and thanks in adcance for the help.
I'm trying to solve an ODE system in which one equation takes a torque function as an input. However I do not have the torque as a function but as a dataset with measured values from a test bench (26600 values, timestep is 1/100 s). When I pass a simple time dependent function as the torque everything works as expected. I'm just having trouble figuring out how to define my values as a proper function.
My code for the ODE looks somewhat like this (simplified for simplicities sake):
function dy = f(t,y)
global a b c;
dy = zeros(4,1);
dy(1) = y(2);
dy(2) = a.* y(2) - b .* y(1) - y(4) .* cos(y(3));
dy(3) = y(4);
dy(4) = - c .* y(4) .*cos(y(3))^2 - (a+b) .* sin(y(3)) .* cos(y(3)) - myTorque(t)./b;
end
When I define a function myTorque(t) like the following I get the results I want:
function torque = myTorque(t)
torque = 100*exp(t/50);
end
However I don't know how to use the values from a datafile. I read the values like this:
load('D:\path\datafile.mat');
t_VL = datafile.act_torque_1.data;
t_VR = datafile.act_torque_2.data;
t_HL = datafile.act_torque_3.data;
t_HR = datafile.act_torque_4.data;
global torque; torque = t_VL + t_VR + t_HL + t_HR;
This leaves me with torque as a 26600x1 double. How can I make this work as a time dependent function for the ODE in place of the myTorque(t) function?

 채택된 답변

Use interp1 to interpolate values between data points.
doc interp1

댓글 수: 3

Hi, thank you for your answer. I think I do need further explanation. I understand interp1 would help me fill in even closer steps than 1/100 s which might or might not be necessary. However that still leaves me with a somethingX1 matrix, right? I't doesnt create an actual function that I could pass on to the ODE. I'm struggeling with that part.
Do the following
torquedata = ....; % Insert your known torque values
times = ....; % Insert the corresponding times
% Define the function
myTorque = @(t) interp1(times,torquedata,t);
You can now pass this to your ode solver in the same way you did with your previously defined torque function.
interp1 will just determine the appropriate value of torque corresponding to the value of t called for by the ode solver.
Wonderful, that was exactly what I did not understand. It all works as intended now. Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by