using ODE45 when parameters in my function change in time?

6 Ansichten (letzte 30 Tage)
Cristina Rossi
Cristina Rossi am 21 Okt. 2014
Bearbeitet: Ced am 21 Okt. 2014
Hi,
I am using ode45 to simulate a model of a mass linked to a pivot through a spring. Radial force and torque are applied and can change in time.
I used previously ode45 with equations that do not change in time, by creating a function eg dtheta= fn (t, theta) as suggested on Mathworks. I am not sure how to do with variable parameters though.
This is my code:
function [ dtheta ] = leg( t,theta)
% theta (1) is theta, Theta (2) is angular velocty
m=1;
g=9.8;
tau=0;
f=0;
k=500;
r0=0.01;
dtheta=zeros(2,1);
dtheta(1)=theta(2);
dtheta(2)=tau/m+g*sin(theta(1))*( (f - m*g*cos(theta(1)))/k +r0 );
end
which I will call with
close all
t0=0;
tf=10;
theta0=pi/2;
angvel0=0;
[T,THETA] = ode45(@leg,[t0 tf],[theta0 angvel0]);
plot(T,THETA(:,1))
xlabel('time')
ylabel('theta (radians)');
Right now I am simulating the model with 0 torque and 0 radial force. As well I could simulate it with constant torque and force. But I need them to change in time (eg being vectors in time). How do I do that?
Thanks!!! Cristina

Akzeptierte Antwort

Ced
Ced am 21 Okt. 2014
Bearbeitet: Ced am 21 Okt. 2014
Hi Cristina
It depends a bit on how your torque and force trajectories are defined, but the easiest way in my opinion: If torque and force are functions of (t,theta) as well, then simply write two functions, e.g. tau = compute_torque(t,theta) and f = compute_force(t,theta), and replace the expressions in your leg function, i.e.
function tau = compute_torque(t,theta)
tau = (1-exp(-t^2); % here comes your tau function
end
function f = compute_force(t,theta)
f = sin(t)*theta(2); % here comes your force function
end
function [ dtheta ] = leg( t,theta)
% theta (1) is theta, Theta (2) is angular velocty
m=1;
g=9.8;
tau=compute_torque(t,theta);
f=compute_force(t,theta);
k=500;
r0=0.01;
dtheta=zeros(2,1);
dtheta(1)=theta(2);
dtheta(2)=tau/m+g*sin(theta(1))*( (f - m*g*cos(theta(1)))/k +r0 );
end
All clear?

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by