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 个)

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 个评论

thanks I use ;
a*(1+y(i)).^1.5 so it does not to negative
how do I do to show the sum(y) of each interation
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')

请先登录,再进行评论。

I meant this is the answer:
how can i get the answer of each iteration
1 iteration y= -0.333
2 iteration y= -0.333 + 0.3806
3 iteration y= -0.333+0.3806+2.1387
.... and so on
y =
0 -0.3333 0 0 0
y =
0 -0.3333 0.3806 0 0
y =
0 -0.3333 0.3806 2.1387 0
y =
0 -0.3333 0.3806 2.1387 2.4848
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 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by