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

1 view (last 30 days)
suketu vaidya on 4 Feb 2021
Commented: Walter Roberson on 4 Feb 2021
%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
##### 1 CommentShowHide None
Walter Roberson on 4 Feb 2021
Do not assign to x inside f

KSSV on 4 Feb 2021
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