MATLAB: Unable to perform assignment because the left and right sides have a different number of elements.

full error ishere i am trying to solve euler method but its shows error inunable to perform assignment because the left and right sides have a different number of elementsy(i))y(i+1) =y(i) + h * f(x(i)

%euler exlicit method
h = 0.1;
x = 1:h:10;
y = [0 -1];
for i = 1:0.1:10
x(i+1)=h+x(i);
y(i+1) =y(i) + h * f(x(i), y(i));
disp(x(i+1));
disp(y(i+1));
end
figure(2)
plot(x,y);
hold on
function dy = f(x,y)
d=50;
x = 1:0.1:10;
c1=-1-d^2/(d^2+1);
dy=c1*exp(-d*x)+d*sin(x)/(d^2+1)+d^2*cos(x)/(d^2+1);
%analytical solution
figure (1)
plot(x,dy);
xlabel('x');ylabel('y(x)');title('Analytical Solution');grid on
hold on
end

Best Answer

function myfunc()
%euler exlicit method
h = 0.1;
x = 1:h:10;
y = [0 -1];
for i = 1:length(x)
x(i+1)=h+x(i);
y(i+1) =y(i) + h * f(x(i), y(i));
disp(x(i+1));
disp(y(i+1));
end
figure(2)
plot(x,y);
hold on
end
function dy = f(x,y)
d=50;
% x = 1:0.1:10;
c1=-1-d^2/(d^2+1);
dy=c1*exp(-d*x)+d*sin(x)/(d^2+1)+d^2*cos(x)/(d^2+1);
%analytical solution
% figure (1)
% plot(x,dy);
% xlabel('x');ylabel('y(x)');title('Analytical Solution');grid on
% hold on
end