error in arrays line 7

function V = fuelvol2(h)
global r H L
validInput = true; %to test each value in the vector h is valid
for i = 1:length(h)
Nh = h(i);
if (Nh < 0 && (Nh > h + 2*r))
validInput = false;
break;
end
end
if validInput == true % if it is valid vector the calculate volume using vectorized operations
d = h-H;
V = 2*r*L*H + (r^2 * acos((r-d)/r) - (r-d).*sqrt(2*r.*d -d.^2))*L;
else
disp("one of more values in vector his/are valid ")
end
I keep getting tis error each time i try to run the code
Not enough input arguments.
Error in fuelvol2 (line 7)
for i = 1:length(h)

6 件のコメント

Adam Danz
Adam Danz 2019 年 9 月 29 日
It sounds like you're running the function without an input. You must provide an input "h", to the function.
Walter Roberson
Walter Roberson 2019 年 9 月 29 日
When you are running the function by clicking the green Run button, what is your assumption about how MATLAB should know what the value of h is ?
Ronald Aono
Ronald Aono 2019 年 9 月 29 日
編集済み: Adam Danz 2019 年 9 月 29 日
hese are my inputs from the command window
% we will now define our terms
r = 0.95; H = 2.0; L = 5.0; % this are all in ft
h = 0:(H + 2*r); Nh = Length(h);
V = fuelvol1(h);
vol1 = zeros(V,Nh);
for j = 1:Nh
vol1(:, j) = fuelvol1(h);
end
still getting the same error
main_fuelvol12
Undefined function or variable 'Length'.
Error in main_fuelvol12 (line 6)
h = 0:(H + 2*r); Nh = Length(h);
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 29 日
Nh=length(h);
%....^ small letter
Adam Danz
Adam Danz 2019 年 9 月 29 日
Function calls are case sensitive. There is no matlab function named Length() but there is a function with the lower case length(). However, it's recommended to use numel() instead of length().
Ronald Aono
Ronald Aono 2019 年 9 月 29 日
編集済み: Adam Danz 2019 年 9 月 29 日
Thank you for the length error, i appriciate it . how do i clear this error to, it keeps creeping up
Operands to the || and && operators must be convertible to logical scalar values.
Error in fuelvol1 (line 7)
if (h>=0) && (h <= (H + 2*r))
Error in main_fuelvol12 (line 7)
V = fuelvol1(h);

サインインしてコメントする。

回答 (2 件)

Adam Danz
Adam Danz 2019 年 9 月 29 日

0 投票

It's getting hard to follow the discussion because we're jumping around between different similarly named functions. fuelvol1, fuelvol2, main_fuelvol12.
This error appears to be in fuelvol1 but we don't have access to that function. However, I think the error can be fixed by replacing && with &.
Walter Roberson
Walter Roberson 2019 年 9 月 30 日
編集済み: Walter Roberson 2019 年 9 月 30 日

0 投票

for i = 1:length(h)
So you are expecting h to be a non-scalar.
if (Nh < 0 && (Nh > h + 2*r))
You add the non-scalar h to something and compare it to the scalar Nh, producing a non-scalar result on the right hand side of the && . However, the && operator can only be used when both sides are scalars.
Changing to & is not the correct solution. The correct solution is to stop using single-letter variable names that differ only in upper versus lower case, because it is too easy to get the wrong variable name. You do not want h there, you want H .
In your other question, H and 2*r were both positive, and Nh > some_positive_value cannot be simultaneously true with Nh < zero.

1 件のコメント

Adam Danz
Adam Danz 2019 年 9 月 30 日
Just curious, from where did you get "for i = 1:length(h)"? I see it in fuelvol2() but the error is in fuelvol1() which we don't have access to, unless I'm missing something.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

タグ

質問済み:

2019 年 9 月 29 日

コメント済み:

2019 年 9 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by