first order runge kutta method

11 vues (au cours des 30 derniers jours)
Lydia Iremonger
Lydia Iremonger le 11 Mai 2021
Modifié(e) : Jan le 11 Mai 2021
PLEASE HELP!!!
Here is my code to solve the following 1st order nonlinear diiferential equation:
(dr/dt)^2=1-(1-2/r)(1+49/r^2) numerically with the intial condition r(0)=50.
I'm unsure how to combine the positive and negative sqrt solutions as i wish to fisrt take the negative root until a fixed value of t and then the positive sqrt after this value of t.
I wish to solve it numerically with small step sizes (h).
However, i havent done matlab in years and can't figure out where my errors are as its not running the code.
h = 0.1; % set the step size
t = ; % set the interval of x
r = zeros(1,length(t));
r(0) = 100; % set the intial value for y
n = length(t)-1;
r_dot =@(t,r)(sqrt(1-(1-2/r)(1+49/r^2))); %insert function to be solved
for i = 1:n
k1 = r_dot(t(i),r(i));
k2 = r_dot(t(i)+.5*h,r(i)+.5*k1*h);
k3 = r_dot(t(i)+.5*h,r(i)+.5*k2*h);
k4 = r_dot(t(i)+h,r(i)+k3*h);
r(i+1) = r(i)+((k1+2*k2+2*k3+k4)/6)*h;
end
[t,r_check] = ode45(r_dot,t,2);
plot(t,r)
title('Eulers Method')
figure
plot(t,r_check)
title('ode45 Check')
  1 commentaire
Daire Vickers
Daire Vickers le 11 Mai 2021
Your title says first order and your graph title is Eulers Method which is the same thing as first order Runge-Kutta but it seems you are implementing a fourth order Runge-Kutta method.
Another couple of things is that you need to set a range for t e.g. t=[-100:h:100] or whatever your range of values is.
Your inline function r_dot has t as an input variable but it isn't used anywhere in the inline function.
You have r(0) but it should be r(1) as MATLAB doesn't use zero indexing.

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by