Error in loop: left and right sides have a different number of elements
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
RAJA SEKHAR BATTU
on 27 Oct 2022
Edited: RAJA SEKHAR BATTU
on 27 Oct 2022
Hi @Miriam
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 Comments
RAJA SEKHAR BATTU
on 27 Oct 2022
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!