Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID


We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need Help on Matlab code PLEASE

clayp105clayp105 Posts: 1Member
This code is using the 4th Order Runge-Kutta Method to solve for the orbit of Halley's comet over time. I don't know why it won't work. I'm given the initial x0 vector and h is the step size. Any help would be appreciated.

function [A] = matrix(x1, x3, x5)

r = sqrt((x1)^2 + (x3)^2 + (x5)^2)

m = 4*pi^2

b = -m/r^3

A = [0 1 0 0 0 0;
b 0 0 0 0 0;
0 0 0 1 0 0;
0 0 b 0 0 0;
0 0 0 0 0 1;
0 0 0 0 b 0];

function [Y] = rk4(x0, h)

T = 30;

n = T/h - 1;

x(1) = x0(:,1);

for i = 2:n

A = matrix(x(i-1,1), x(i-1,3), x(i-1,5));

k1 = A.*x(:,i-1);
k2 = A.*(x(:,i-1) + .5*h*k1);
k3 = A.*(x(:,i-1) + .5*h*k2);
k4 = A.*(x(:,i-1) + h*k3);

phi = (1/6)*(k1 + 2*k2 + 2*k3 + k4);

x(:,i) = x(:,i-1) + phi*h;


Y = x
Sign In or Register to comment.