I am trying to runge-kutta order 2 and I keep getting this error
Unable to perform assignment because the left and right sides have a different number of elements.
Error in RungeKutta2 (line 24)
Y(1) = [t(1);y(1)]
Error in prob1 (line 11)
[y] = RungeKutta2(f, n, x1, x2,t0,y0,c2);
f = @(y) 0.5*sin(y(1,:)) + (cos(y(2,:)+y(1,:)))*(atan(y(2,:)) - 0.5*cos(y(2,:)));
n = 10;
x1 = 0;
x2 = 5;
t0 = 0;
y0 = 1;
c2 = 0.5;
[y] = RungeKutta2(f, n, x1, x2,t0,y0,c2);
function [y] = RungeKutta(f, n, x1, x2,t0,y0,c2)
c1 = 1-c2;
a = 0.5/c2;
b=a;
h=(x2-x1)/n;
t = linspace(x1,x2,n+1)
y = zeros(1,n+1)
y(1) = y0;
Y = [t;y]
Y(1) = [t(1);y(1)]
k1 = f(Y)
k1(1) = f(Y(1));
Y2 = [t + a*h; y + h*b*k1];
Y2(1) = [t(1) + a*h; y(1) + h*b*k1(1)];
k2 = f(Y2);
k2(1) = f(Y2(1));
for ii = 2:length(t)
y(ii) = y(ii-1) + h * (c1*k1(ii-1) +c2*k2(ii-1));
end
end