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

조회 수: 4 (최근 30일)
I am getting the message, in the title when trying to run this code. How do I sort this?
% Euler's Method
% Initial conditions and setup
h = 0.1; % step size
x = 0:h:4; % the range of x
y = zeros(size(x)); % allocate the result y
y(i) = (2); % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = y(i)*cos(x).^2;
y(i+1) = y(i) + h * f;
end

답변 (1개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 11월 19일
편집: JESUS DAVID ARIZA ROYETH 2019년 11월 19일
congratulations! ,you did very well, just a few small changes:
% Euler's Method
% Initial conditions and setup
h = 0.1; % step size
x = 0:h:4; % the range of x
y = zeros(numel(x),1); % allocate the result y
y(1) = (2); % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=2:n
f = y(i-1)*cos(x(i-1)).^2;
y(i) = y(i-1) + h * f;
end
syms Y(X)
yy=matlabFunction(dsolve(diff(Y)==Y*cos(X).^2,Y(0)==2));
figure;
plot(x,y,'r*-',x,yy(x),'b*-');
legend('Euler','Real')
tempo.png
  댓글 수: 2
Skye Cameron
Skye Cameron 2019년 11월 19일
Hi, you might be able to help me? This is the question I have been given and I thought my code would give me what I am looking for but it doesnt seem to be?
Screen Shot 2019-11-19 at 13.15.20.png
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 11월 19일
you did very well, just a few small changes:
% Euler's Method
% Initial conditions and setup
h = 0.1; % step size
x = 0:h:4; % the range of x
y = zeros(numel(x),1); % allocate the result y
y(1) = (2); % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=2:n
f = y(i-1)*cos(x(i-1)).^2;
y(i) = y(i-1) + h * f;
end
syms Y(X)
yy=matlabFunction(dsolve(diff(Y)==Y*cos(X).^2,Y(0)==2));
figure;
plot(x,y,'r*-',x,yy(x),'b*-');
legend('Euler','Real')

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by