# Runge Kutta 4th order, Error: Subscripted assignment dimension mismatch

5 views (last 30 days)
Dianna B on 13 Apr 2016
Edited: James Tursa on 13 Apr 2016
Hi I'm getting a Subscripted assignment dimension mismatch error when I'm running a Runge Kutta code. I have created this code:
%Define function handles
%y=[y1,y2,y3,y4]<= y(1,:),y(2:), y(3,:), y(4:)
f=@(t,y) [...
+0.95*y(1)*(1-(y(1)/3.44));
((18.73*y(1)*y(4))/(37.5+y(4)))-(0.9*1.2*y(2))
1.2*y(2);
-((18.73*y(1)*y(4))/(37.5+y(4)))];
% Initial conditions
t(1)=0;
y(:,1)=[0.5,0,0,50];
%Step size
h=0.1;
tfinal=10;
N=tfinal/h;
%Update loop
for i=1:N
%Update time
t(i+1)=t(i)+h;
%Update for y
k1 = f(t(i) ,y(:,i) );
k2 = f(t(i)+0.5*h,y(:,i)+0.5*k1*h) ;
k3 = f(t(i)+0.5*h,y(:,i)+0.5*k2*h) ;
k4 = f(t(i)+h ,y(:,i)+ k3*h) ;
y(:,i+1)=y(:,i)+1/6*(k1+2*k2+2*k3+k4)*h ;
end
When I run the code i get this error:
>> rk4_systems Subscripted assignment dimension mismatch.
Error in rk4_systems (line 11) y(:,1)=[0.5,0,0,50];
DB

Kuifeng on 13 Apr 2016
%y(:,1) is the first column, and
%[0.5,0,0,50] is a row vector.
consider y(:,1)=[0.5,0,0,50]';

#### 1 Comment

Dianna B on 13 Apr 2016
Thanks you for your help :) the code was successfully executed.