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)

Réponses (3)

Alan Stevens
Alan Stevens le 24 Sep 2020

0 votes

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
Josue Sosa le 24 Sep 2020
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')

Connectez-vous pour commenter.

Josue Sosa
Josue Sosa le 24 Sep 2020

0 votes

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
Alan Stevens
Alan Stevens le 24 Sep 2020

0 votes

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!

Translated by