why do I get this answer
显示 更早的评论
this is my code:
clc; clear all; close all;
% Euler's Method
a=160;
Q=400;
A=1200;
t = 0:2.5:10;
delt= t(2)-t(1);
y = zeros(size(t));
for i=1:numel(t)-1
y(i+1) = (3*Q/A*sin(t).^2 - a*1+y.^1.5/A)*delt + y(i)
end
I get this answer:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in lab4 (line 10)
y(i+1) = (3*Q/A*sin(t).^2 - a*1+y.^1.5/A)*delt + y(i)
回答(3 个)
Alan Stevens
2020-9-24
You can overcome that problem as follows
% Euler's Method
a=160;
Q=400;
A=1200;
delt = 2.5;
t = 0:delt:10;
y = zeros(size(t));
for i=1:numel(t)-1
y(i+1) = (3*Q/A*sin(t(i)).^2 - a*1+y(i).^1.5/A)*delt + y(i);
end
However, y quickly goes negative with the values of the constants you have. This means y(i).^1.5 results in complex numbers. Is that what you intended?
2 个评论
Josue Sosa
2020-9-24
Alan Stevens
2020-9-24
Do you mean something like this (where I've used a much smller timestep):
% Euler's Method
a=160;
Q=400;
A=1200;
delt = 0.1;
t = 0:delt:10;
y = zeros(size(t));
for i=1:numel(t)-1
y(i+1) = (3*Q/A*sin(t(i)).^2 - a*(1+y(i)).^1.5/A)*delt + y(i);
end
plot(t,y),grid
xlabel('t'),ylabel('y')
Alan Stevens
2020-9-24
This
% Euler's Method
a=160;
Q=400;
A=1200;
delt = 2.5;
t = 0:delt:10;
y = zeros(size(t));
for i=1:numel(t)-1
y(i+1) = (3*Q/A*sin(t(i)).^2 - a*(1+y(i)).^1.5/A)*delt + y(i);
disp(y)
end
produces
0 -0.3333 0 0 0
0 -0.3333 0.3806 0 0
0 -0.3333 0.3806 2.1387 0
0 -0.3333 0.3806 2.1387 2.4848
类别
在 帮助中心 和 File Exchange 中查找有关 Digital Filter Design 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!