![tempo.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/249000/tempo.png)
Unable to perform assignment because the left and right sides have a different number of elements
1 次查看(过去 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
0 个评论
回答(1 个)
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/249000/tempo.png)
2 个评论
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 Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!