why do I get this answer
Afficher commentaires plus anciens
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)
Réponses (3)
Alan Stevens
le 24 Sep 2020
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 commentaires
Josue Sosa
le 24 Sep 2020
Alan Stevens
le 24 Sep 2020
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
le 24 Sep 2020
Alan Stevens
le 24 Sep 2020
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
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!