How to substitute each value of the for loop to the ode which is in the function file

1 次查看(过去 30 天)
Thank you in advance.
I want to substitute each value obtained in the for loop one by one to the ODE as input value which is written in the function file. The below code is written for the spring mass system subjected to triangular input signal for 5 cycles.
Please help me.
----------------------------------------------Function file---------------------
function f = slider_1eqn(t,x)
% vs=.001;
n = 5; % Number of cycles.
k=20; % Stiffness in N/m
c = 70*10^-3; % Damping factor in Ns/m.
m2 = 1.2*10^-3 ; % Inertial mass in Kg.
m1 = 37*10^-3 ; % Mass of the actuator with semi-circular ball in Kg.
g = 9.81 ; % Acceleration due to gravity in m/s^2.
F = m2*g; % Friction force in N.
mu = .25; % Co-efficient of friction.
tr = 2*10^-3 ; % Rise time of the input signal.
tf = 4*10^-6 ; % Fall time of the input signal.
td = 1*10^-3 ; % Dwell time or time interval between cycles.
Vo = 200*10^-9; % Net displacement
tt = tr + tf + td; %Time period
tspan = 0:0.0001:n*tt; %time vector;
%-------------------------------Input signal generation -------------%
Y = zeros; %Initialization
for i = 1: numel(tspan)
if mod(tspan(i),(tt)) < tr
Y(i) = Vo*mod(tspan(i),tt)/tr;
sdot(i) = (Vo/tr);
elseif mod(tspan(i),tt) < tr+tf
Y(i) = Vo*(tr-mod(tspan(i),tt))/tf + Vo;
sdot(i) = Vo*(tr-1)/tf + Vo;
else
Y(i) = 0;
sdot(i) = 0;
end
end
% --------------------------
% EQUATION OF MOTION
% --------------------------
f = zeros(4,1); % a column vector
f(1) = x(2);
f(2) = (-c/m1)*(x(2) - (sdot))+ (-k/m1)*(x(1) - Y) - (F/m1);
f(3) = x(4);
f(4) = (c/m2)*(x(4) - sdot) + (k/m2)*(x(3) - Y);
-------------------------------------------------------------------------script file of the above function file--------------------------------------------------------
clear all
clc;
clf;
tic;
mu = 0.2;
n = 5;
Vo = 200*10^-9; % Net displacement
tr = 2*10^-3 ; % Rise time of the input signal.
tf = 4*10^-6 ; % Fall time of the input signal.
td = 1*10^-3 ; % Dwell time or time interval between cycles.
tt = tr + tf + td; %Time period
tspan = 0:0.0001:n*tt; %time vector;
[t,x]=ode45(@slider_1eqn,tspan,[0 0 0 0]);
yy = x(:,3);
yd = x(:,2);
figure(1);
plot(t,yy,'r','linewidth',2);
hold on
plot(t,x(:,1))
plot(t,z, 'k')
grid on

回答(1 个)

Abhas
Abhas 2025-5-29
编辑:Abhas 2025-5-29
Your current 'slider_1eqn.m' regenerates the entire time vector 'tspan' and input signal arrays every time it’s called by 'ode45' for a single 't' value. You should rather precompute the input signals ('Y' and 'sdot') once in the script file, interpolate them inside the function for the current 't', and then plug those values into the ODE equations.
Here's the updated MATLAB code:
clear; clc;
% Parameters
n = 5;
Vo = 200e-9;
tr = 2e-3;
tf = 4e-6;
td = 1e-3;
tt = tr + tf + td;
tspan = 0:1e-4:n*tt;
% Input signal generation
Y = zeros(size(tspan));
sdot = zeros(size(tspan));
for i = 1:numel(tspan)
modt = mod(tspan(i), tt);
if modt < tr
Y(i) = Vo * modt / tr;
sdot(i) = Vo / tr;
elseif modt < tr + tf
Y(i) = Vo * (tr - modt) / tf + Vo;
sdot(i) = -Vo / tf;
else
Y(i) = 0;
sdot(i) = 0;
end
end
% Pack Y and sdot into a struct
input.Y = Y;
input.sdot = sdot;
input.tspan = tspan;
% ODE solver call with extra input
[t, x] = ode45(@(t, x) slider_1eqn(t, x, input), tspan, [0 0 0 0]);
% Plot results
figure;
plot(t, x(:,3), 'r', 'LineWidth', 2); hold on;
plot(t, x(:,1), 'b');
grid on;
legend('y_2', 'y_1');
xlabel('Time (s)');
ylabel('Displacement');
function f = slider_1eqn(t, x, input)
% System parameters
k = 20;
c = 70e-3;
m2 = 1.2e-3;
m1 = 37e-3;
g = 9.81;
F = m2 * g;
% Interpolate Y and sdot for current t
Y_now = interp1(input.tspan, input.Y, t);
sdot_now = interp1(input.tspan, input.sdot, t);
% System of equations
f = zeros(4,1);
f(1) = x(2);
f(2) = (-c/m1)*(x(2) - sdot_now) + (-k/m1)*(x(1) - Y_now) - (F/m1);
f(3) = x(4);
f(4) = (c/m2)*(x(4) - sdot_now) + (k/m2)*(x(3) - Y_now);
end
You may refer to the below MathWorks documentation links to know more about the same:
  1. Parameterizing Functions: https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
  2. ode45: https://www.mathworks.com/help/matlab/ref/ode45.html
I hope this helps!

类别

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