How do i fix my Runge Kutta (4th order) method to solve a 2nd order ODE?

1 次查看(过去 30 天)
Hi,
I have a code for a Runge Kutta 2th order analysis of a 2nd order ODE but it has not give me the right answer.
% d2a/dt2=(16*a^(1/2))/35 - 18/(35*a) - (6*a^3)/35
%initial condition: da/dt(x=-∞)=0 و a(x=0)=0.705
Here is my code below:
clear all
clc
h=0.2;
t=-2:h:0;
Nt=numel(t);
A=zeros(2,Nt);
A(:,1)=[0.705,0];
f = @(t, A) [A(2);(16*A(1).^(1./2))./35 - 18./(35.*A(1)) - (6.*A(1).^3)./35];
for i=1:Nt-1
k1 = h.*f(t(i), A(:,i));
k2 = h.*f(t(i) + 0.5.*h, A(:,i) + 0.5.*k1);
A(:,i+1) = A(:,i) + (1.0./6.0).*(k1 + 2.*k2 );
end
figure(1)
plot(t, A, '-o');
xlabel('x/hu')
ylabel('h/hu')
grid on

回答(1 个)

Mathieu NOE
Mathieu NOE 2020-11-10
hello
attached a zip for fixed time steps ODE solvers - compare with your own implementation
  4 个评论
Mj
Mj 2020-11-10
thanks. but i dont want to use ode45 function. i want to solve this by rung kutta method.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by