Function Handle and RK4 problem.
Show older comments
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
James Tursa
on 28 Jan 2021
Can you provide more detail about what tau is supposed to be in your code?
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?
Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!