File Exchange

## Runge Kutta 4th order ode

version 1.4.0.0 (1.32 KB) by Judah S

### Judah S (view profile)

solves ode using 4th order Runge Kutta method

401 Downloads

Updated 16 Jan 2013

View License

This code defines an existing function and step size which you can change as per requirement.

P.S: This code has no new feature compared to existing codes available online. Intention behind posting this very simple code is to help students understand the concept and solve assignments.

### Cite As

Judah S (2020). Runge Kutta 4th order ode (https://www.mathworks.com/matlabcentral/fileexchange/29851-runge-kutta-4th-order-ode), MATLAB Central File Exchange. Retrieved .

Soumyadeep

Yaman Yucel

### Yaman Yucel (view profile)

It does not work for y(0) = 0 initial condition

Nidhi Menon

Abraham

### Abraham (view profile)

Great work! What about a code for Runge Kutta method for second order ODE. Something of this nature:
d^2y/dx^2 + 0.6*dy/dx 0.8y = 0

Thank you

math16

Sumith YD

Dogba Djaze

masoud rahmani

Tareque Hossain

### Tareque Hossain (view profile)

how can i solve SIR model using RK4 method in matlab? can you write the code please

faiz islam

### faiz islam (view profile)

sir can you assist me ,that how we can apply 4th order Runge kutta method for 4 coupled equation?
dx/dt=−ax − eω + yz
dy/dt= by + xz
dz/dt= cz + fω − xy
dω/dt = dω – gz
a = 50, b =−16, c = 10, d = 0.2, e = 10, f = 16, g = 0.5
Step size 0.001 .
regards
faiz

Ali Abbas

### Ali Abbas (view profile)

@Shahzaib Asif Very helpful program.JazakAllah

Stephanie Valerio

### Stephanie Valerio (view profile)

How do I run/call to this code?

Hamza Fokraoui

### Hamza Fokraoui (view profile)

for this function : f'''' - f*f''' + 4*g = 0
where i need to insert it in this code?
thank you

Shahzaib Asif

### Shahzaib Asif (view profile)

function RK4(f,a,x0,y0,h)

% Runge Kutta Method 4th Order
% function @(x,y) e.g. f=@(x,y)(x+y);
% a = the point up to which you obtain the results
% x0 = initial condition of x
% y0 = initial condition of y
% step size

x = x0:h:a;
y(1) = y0;

for i=1:(length(x)-1)

k1 = f(x(i),y(i));
k2 = f(x(i)+0.5*h,y(i)+0.5*h*k1);
k3 = f((x(i)+0.5*h),(y(i)+0.5*h*k2));
k4 = f((x(i)+h),(y(i)+k3*h));

y(i+1) = y(i) + (1/6)*(k1+2*k2+2*k3+k4)*h;

end

y(:)

%Shahzaib Asif (zaibi7402)
%shahzaib.7402@gmail.com

Chris FUNG

clear coding

Christoph

Ying

Ying

### Ying (view profile)

Very good to learn. Thanks.

Arun

Pi Ting

excellent work

Ido

### Ido (view profile)

Excellent program,
very helpful.

### Updates

 14 Jan 2013 1.4.0.0 Just an update 25 Jan 2012 1.1.0.0 Nothing much
##### MATLAB Release Compatibility
Created with R2010a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux