Creating plot of values from for loop, error message when creating an array
Show older comments
I am trying to plot the value z of the following code from the for loop. When I try to put them in an array I get the following error message and don't know how to fix it.
Error using /
Matrix dimensions must agree.
Error in Marsroverwheels (line 19)
G=F/(Ca*1000) %ground pressure
the code is:
Kc=10
Kphi=850
theta=35
n=1
b=0.6 %wheel width
r=0.5 %wheel radius
z=0.01 %compression depth
for i =1:10
F=Ww*g %Force per wheel on ground
Cc=((2*3.142*2*r)/360)*acosd((r-z)/r) %contact circumference
Ca=Cc*b %contact area
G=F/(Ca*1000) %ground pressure
z(i)=(G/((Kc/b)+Kphi)).^(1/n) %new compression depth
end
2 Comments
Ahmad Moniri
on 14 Feb 2017
I believe the reason is because your compression depth is a vector (since you are doing z(i) = ...). This means that when you calculate Cc in the second iteration of the for-loop, acosd((r-z)/r) is going to return a vector. Since Cc is a vector, Ca is a vector and thus F/(Ca*1000) is not allowed since you are dividing a scalar by a vector.
This is a simple solution without changing your code much:
z=(G/((Kc/b)+Kphi)).^(1/n) %new compression depth
myarray(i) = z;
Walter Roberson
on 14 Feb 2017
Use ./ instead of /
Answers (1)
KSSV
on 14 Feb 2017
Kc=10 ;
Kphi=850 ;
theta=35 ;
n=1 ;
b=0.6 ; %wheel width
r=0.5 ; %wheel radius
Ww = rand ;
g = 9.8 ;
N = 10 ;
z = zeros(1,N) ;
z(1)=0.01 ; %compression depth
for i =2:N
F=Ww*g ;%Force per wheel on ground
Cc=((2*3.142*2*r)/360)*acosd((r-z(i-1))/r) ; %contact circumference
Ca=Cc*b ; %contact area
G=F/(Ca*1000) ; %ground pressure
z(i)=(G/((Kc/b)+Kphi)).^(1/n) ; %new compression depth
end
You have named the compression depth z outside the loop and inside the loop same. I have made few changes have a look.
Categories
Find more on Clustering in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!