Error in loop: left and right sides have a different number of elements

1 次查看(过去 30 天)
I wanna make a loop out of the following lines:
dt=1;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs1=real(roots(p))*D
dt=2;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs2=real(roots(p))*D
dt=3;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs3=real(roots(p))*D
dt=4;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs4=real(roots(p))*D
For every Zvs i get for solutions (Zvs (4,1)). I tried like this:
dt = [1:1:4];
Zvs = NaN(length(dt),4);
for i=1:length(dt)
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs (i)=real(roots(p))*D
end
I get this Error:
Unable to perform assignment because the left and right sides have a
different number of elements.
Error in Skript (line 26)
Zvs (i)=real(roots(p))*D
I know that the problem ist the different size from Zvs (i), its 1x1 and not 1x4. But i don't know how to fix it.

采纳的回答

RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022-10-27
编辑:RAJA SEKHAR BATTU 2022-10-27
You can pre assign the variables to empty or zeros before loop to save the values efficiently. I will make some modifications please check if it works.
a0, p and Zvs variables are changing every iteration
dt = [1:1:4];
Zvs = zeros(4,length(dt));
a0 = zeros(1,length(dt));
p = zeros(length(dt),5);
for i=1:length(dt)
a0(i)= 0.01*sqrt(d/D)*Fr/D*u*dt(i);
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p(i,:)=[a4 a3 a2 a1 a0(i)];
Zvs(:,i)=real(roots(p(i,:)))*D;
end
  4 个评论
RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022-10-27
The matrices of pre allocation should match the matrices inside the loop.
Please check the size of the matrices. This is the point of pre allocation.
Please check and modify your code accordingly.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by