for loop with linspace
61 次查看(过去 30 天)
显示 更早的评论
Hi all, I'm trying to get the following for loop statement to work. I'm doing shear force calculations for different portions of a beam. I get this as error 'Subscript indices must either be real positive integers or logicals'.
b =input('Enter the length of the beam ');
u =input('Enter the load (N/m) ');
R_side = u*b/3;
R_center = R_side;
d = 0.17 * b;
l = 0.33 *b;
x = linspace(0,b,100);
for i =length(x)
if i<= d
sh(i) = -u*x;
elseif x<= d+l
sh(i) = -u*x + R_side;
elseif x<= d+2*l
sh(i) = -u*x + R_side + R_center;
else
sh(i) = -u*x + R_center + 2*R_side;
end
end
plot(x,sh(i))
xlabel(' Length of beam, [m]','FontSize',20);
ylabel(' Shear force, [N]','FontSize',20);
title('Shear Force Diagram','FontSize',20);
2 个评论
Image Analyst
2018-2-9
Not what I get:
Enter the length of the beam 4
Enter the load (N/m) 5
Subscripted assignment dimension mismatch.
Error in test4 (line 21)
sh(i) = -u*x + R_center + 2*R_side;
What values did you enter?
Walter Roberson
2018-2-9
Notice that
for i =length(x)
executes exactly once, with i = 100 (that is, the length of x)
回答(1 个)
Sebastian Castro
2018-2-9
I see 2 things here
First, the for-loop should start from the first index and stop at the length of x. Right now, your code is saying that i is only one value ( length(x) ).
for i=1:length(x)
Secondly, just as you're assigning each individual element of the sh vector as sh(i), you want to do the same with x instead of trying to perform operations on the entire array. So, for example:
sh(i) = -u*x(i)
- Sebastian
2 个评论
Sebastian Castro
2018-2-10
I think you need to remove the indexing from the plot, since you want the whole sh vector. The following command should do it:
plot(x,sh)
- Sebastian
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!