如何摆脱代码中的下标索引错误?

时间:2015-10-22 01:49:18

标签: matlab

length = 31 ;
while length < 0 
    length = input('enter a value greater than 0:')
end

pounds = 26905;

elasticity = 45941267 ;

width = 4.3  
while width < 0 
    width = input('enter a value greater than 0:')
end
height = 1.2 
while height < 0 
    height = input('enter a value greater than 0:');
end

I = (width*height^3)/12;
a = linspace(1,200)';
b = length - a ;


if a >= 0

maximum = (-pounds*b(length.^2-(b.^2))^(3/2))/(9*sqrt(3)*elasticity*I*length)'



elseif b >= 0

  maximum = (-pounds*a(length.^2-(a.^2))^(3/2))/(9*sqrt(3)*elasticity*I*length)'  

end 

它出现在这一行:

maximum = (-pounds*b(length.^2-(b.^2))^(3/2))/(9*sqrt(3)*elasticity*I*length)'

我需要我的代码来提供这些数字,以便我可以在表格中打印它们并完成我的代码。

1 个答案:

答案 0 :(得分:0)

正如@rayryeng所说,你干的问题是你要索引到b。
如果你检查length^2 - (b.^2),你会得到:

61    120    177    232    285    336    385    432    477    520
561    600    637    672    705    736    765    792    817    840
861    880    897    912    925    936    945    952    957    960
961    960    957    952    945    936    925    912    897    880
861    840    817    792    765    736    705    672    637    600
561    520    477    432    385    336    285    232    177    120
61      0    -63   -128   -195   -264   -335   -408   -483   -560
... snipped

第四个值是:232,它大于b的长度(在这种情况下为200)。这会触发第一个错误,但即使你能够继续,你最终也会得到负值(对于索引!还有另一个错误)