MATLAB: Dimensions of arrays being concatenated are not consistent.

lengthMATLABmatlab functionvector

Heres my code:
[x,y] = meshgrid(-1:0.02:1, -1:0.02:1);
x0 = [0.46,-0.25];
u = @(x,y)3*(1-3*x).^2.*exp(-x.^2 - (3*y+1).^2) ...
- 10*(3*x/5 - 27*x.^3 - 243*y.^5).*exp(-2*x.^2-9*y.^2) ...
- 1/3*exp(-(3*x+1).^2 - 9*y.^2) + (x.^2+y.^2) - 1;
[xq,yq]=meshgrid(-1:0.1:1, -1:0.1:1);
[px,py]=gradient(u(xq,yq));
u = @(x,y)3*(1-3*x).^2.*exp(-x.^2 - (3*y+1).^2) ...
- 10*(3*x/5 - 27*x.^3 - 243*y.^5).*exp(-2*x.^2-9*y.^2) ...
- 1/3*exp(-(3*x+1).^2 - 9*y.^2) + (x.^2+y.^2) - 1;
upx = @(x,y)-(18*(1-3*x)).*exp(-x.^2-(3*y+1).^2) ...
- 6*(1-3*x).^2.*x.*exp(-x.^2-(3*y+1).^2) ...
- (10*(3/5-81*x.^2)).*exp(-2*x.^2-9*y.^2) ...
+ (20*((3/5)*x-27*x.^3-243*y.^5)).*2.*x.*exp(-2*x.^2-9*y.^2) ...
- (1/3*(-18*x-6)).*exp(-(3*x+1).^2-9*y.^2)+2*x;
upy = @(x,y)3*(1-3*x).^2.*(-18*y-6).*exp(-x.^2-(3*y+1).^2) ...
+ 12150*y.^4.*exp(-2*x.^2-9*y.^2) ...
+ (20*((3/5)*x-27*x.^3-243*y.^5)).*9.*y.*exp(-2*x.^2-9*y.^2) ...
+ 6*y.*exp(-(3*x+1).^2-9*y.^2)+2*y;
gradient_u = @(t,r) -[upx(r(1),r(2)); upy(r(1),r(2))];
domain = [0:0.001:5.0];
[t, r] = ode45(gradient_u,domain,x0);
xu = r(length(r),1);
yu = r(length(r),2);
zu = u(xu,yu);
lenpath = 0;
for i=1:length(r)-1
discpath = [r(i+1,1)-r(i,1);r(i+1,2)-r(i,2);u(r(i+1,1),r(i+1,2)) u(r(i,1),r(i,2))];
lenpath = lenpath + norm(discpath);
end
lenpath
——————————————–
It is essentially finding the length of a 3d line. I can't figure out why the error is coming up. For reference u is a given function, and upx and upy are the given partial derivatives in x and y.

Best Answer

discpath = [r(i+1,1)-r(i,1);r(i+1,2)-r(i,2);u(r(i+1,1),r(i+1,2)); u(r(i,1),r(i,2))];
% semicolon missing ^