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')
Josue Sosa
2020 年 9 月 24 日
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!