The main loop is in Tacking.m and the Runge-Kutta function is in RK4.m. I know the code has been used (as is) in the past, but it's the RK4 function that I'm struggling to write properly. If someone has an idea to put me back on the rails, thanks !
RK4 with controller
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to solve a system of equations from a function ('f') using a 4th order Runge-Kutta method, taking into account a PD controller.
Here is how my code looks like:
t_f = 100; % simulation end time
h = 0.1; % sample time
K = 1; % controller gain
Td = 10; % controller derivative time
x = [...]; % initial conditions
N = round(t_f/h); % number of samples
xout = zeros(N+1,length(x)+2); % memory allocation
for i=1:N+1
time = (i-1)*h; % simulation time
a = x(1);
if (a<10)
a_ref = 1; % desired 'a' value
c = K*((a-a_ref)+Td); % controller
[x v] = RK4('f',x,[c 0],h,i);
xout(i,:) = [time,x',v]
I'm not sure how to write the RK4 function to solve the equations from 'f', making sure the controller is used.
The latest version of the code can be found in the first comment.
Any ideas would be very much appreciated.
Many thanks.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!