Function Handle and RK4 problem.

I'm trying to implement a variable (tau) inside the function func1 in which i need first to obtain the values from the output [ yout(:,1) or pendulum position] and update the variable each loop, and i dont know how to implement like that.
clear all
clc
%% Constants
m=1;
l=2;
g=9.81;
%% Time step
h=0.01;
tfinal = 30;
N=ceil(tfinal/h);
%% IC
x1CI=pi/2;
x2CI=0;
t=zeros(N,1);
yout = zeros(N,2);
y0 = [x1CI x2CI]'
yout(1,:)=y0;
%% Function Handle
system = @(t,y)func1(t,y,m,l,g);
%% Integrator
for i=1:N
% dt,dx,t,x
[ t(i+1), yout(i+1,:) ] = integrator_rk4(h,system,t(i),yout(i,:)');
end
%% Figure
figure
plot(t, yout(:,1))
grid
%% Functions
function dy = func1(t,y,m,l,g)
% y = [x1 x2]' = [y(1) y(2)]
dy=zeros(2,1);
%dy e dy^2
dy(1)= y(2);
dy(2)= ((-g*sin(y(1)))/l);%+Tau=0.01*yout(:,1)
end
function [tout, xout] = integrator_rk4(dt,dx,t,x)
tout = t + dt;
dt_half = 0.5*dt;
k1 = dx(t,x);
k2 = dx(t+dt_half,x+dt_half*k1);
k3 = dx(t+dt_half,x+dt_half*k2);
k4 = dx(tout,x+dt*k3);
xout = x + dt*(k1+2*k2+2*k3+k4)/6;
end

4 Comments

Can you provide more detail about what tau is supposed to be in your code?
Marcio
Marcio on 28 Jan 2021
Edited: Marcio on 29 Jan 2021
this is just a example of a simple pendulum i'm trying to learn , i have another program i'm working on (reaction wheel pendulum, almost the same program with more complex dynamics) where my tau is the torque provided by the DC motor, and i wish to apply a control to bring the pendulum to the inverted position. So briefly explained i need the outputs so i can feedback the system with the torque provided by a DC motor.
J. Alex Lee
J. Alex Lee on 29 Jan 2021
Edited: J. Alex Lee on 29 Jan 2021
here's what I'm seeing from your code: you want the derivative definition in func1() to know not just about the current value of y, but the whole time history of y.
here's what i'm hearing from your comment: you want to write a different system of equations that adjusts the dynamics accounting for an additional quantity (torque).
So is the torque supposed to react only instantaneously to soem current state, or is there some integral aspect of the controller?
Marcio
Marcio on 29 Jan 2021
Edited: Marcio on 29 Jan 2021
there's no integral aspect of the controller, he'll react to states, the problem is that i cant figure out how to use the outputs like dy(2) (acelleration) on tau in the function of the reaction wheel (real system), i need the aceleration on tau and he show up in one equation before the acelleration equation, so i need somehow obtain the values outside the func1 and inside the loop, and update him in the func1, i can't obtain the values of tau also if he's only inside the func1...

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 28 Jan 2021

Edited:

on 29 Jan 2021

Community Treasure Hunt

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

Start Hunting!