Unable to perform assignment because the left and right sides have a different number of elements(funtion in a while loop).

13 views (last 30 days)
I am trying to run a function that i've made inside a while loop to get answers out of the funtion multiple times, but i get the following error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in spere_derive_2d_call (line 21)
[slope_xg(i),slope_vxg(i),slope_yg(i),slope_vyg(i)]=spere_derive_2d(vx,vy,Cd,Dsg,msg,rho);
After trying to debug i found that it happens on my second time through the loop. I'm not even sure what the left and right side are in relation to being in a funtion call, nor why they would need to have the same number of elements.
v0=25;
theta=30;
dt=.001;
rho=1.225;
Cd=0.5;
Dsg=0.04;
msg=0.045;
vx=v0*cosd(theta);
vy=v0*sind(theta);
i=2;
xg(1)=0;
yg(1)=0;
check=1;
while check>=0
[slope_xg(i),slope_vxg(i),slope_yg(i),slope_vyg(i)]=spere_derive_2d(vx,vy,Cd,Dsg,msg,rho); %this is the problem line
vx=slope_yg;
vy=slope_yg;
yg(i)=slope_yg(i)*dt+yg(i-1);
xg(i)=xg(i-1)+slope_xg(i)*dt;
check=yg(i);
i=i+1;
end

Accepted Answer

Voss
Voss on 16 Apr 2024 at 19:38
I don't have the function spere_derive_2d, so I can't run the code, but I guess that spere_derive_2d returns at least 4 outputs, which you are attempting to store as slope_xg(i), slope_vxg(i), slope_yg(i), and slope_vyg(i), respectively.
The problem (and what the error message is saying) is that at least one of those outputs from spere_derive_2d is not a scalar (i.e., 1-by-1 in size), so it cannot be stored as a single element of an array, e.g., slope_xg(i).

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!