Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Miriam
Miriam le 27 Oct 2022
Commenté : Miriam le 27 Oct 2022
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.

Réponse acceptée

RAJA SEKHAR BATTU
RAJA SEKHAR BATTU le 27 Oct 2022
Modifié(e) : RAJA SEKHAR BATTU le 27 Oct 2022
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 commentaires
RAJA SEKHAR BATTU
RAJA SEKHAR BATTU le 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.
Miriam
Miriam le 27 Oct 2022
I got it, now it works. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by