Why do I receive Not enough input arguments?

3 ビュー (過去 30 日間)
ARAH MIZORI
ARAH MIZORI 2021 年 8 月 29 日
回答済み: dpb 2021 年 8 月 29 日
function [SA]=equTriPrismSurfArea(s,h)
SA=((sqrt(3)/2)*(s^2))+(3*s*h);
s = 1;
h = 1;
for i = 1:5
s = s(i);
h = h(i);
SA = ((sqrt(3)/2) * s^2 + 3*s*h);
fprintf ("\nTotal surface area of equilateral triangular prism is %f\n",SA)
end
end

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 29 日
編集済み: Wan Ji 2021 年 8 月 29 日
I just give a minor correction to your code
function [SA]=equTriPrismSurfArea(s,h)
SA=((sqrt(3)/2)*(s^2))+(3*s*h);
end
Save the code as a m-file with name 'equTriPrismSurfArea.m'
IF you do not want to use function equTriPrismSurfArea, then copy these lines to command
s_arr = 1:5;
h_arr = 1:5;
for i = 1:5
s = s_arr(i);
h = h_arr(i);
SA = ((sqrt(3)/2) * s^2 + 3*s*h);
fprintf ("\nTotal surface area of equilateral triangular prism is %f\n",SA)
end
You can also call the function to do the work if you want to use it
s_arr = 1:5;
h_arr = 1:5;
for i = 1:5
s = s_arr(i);
h = h_arr(i);
SA = equTriPrismSurfArea(s,h);
fprintf ("\nTotal surface area of equilateral triangular prism is %f\n",SA)
end

その他の回答 (2 件)

Yongjian Feng
Yongjian Feng 2021 年 8 月 29 日
Try this:
function [SA]=equTriPrismSurfArea(sIn,hIn)
for i = 1:5
s1 = sIn(i);
h1 = hIn(i);
SA = ((sqrt(3)/2) * s1^2 + 3*s1*h1);
fprintf ("\nTotal surface area of equilateral triangular prism is %f\n",SA)
end
end
Then
sIn = randi(10, 1, 5);
hIn = randi(10, 1, 5);
equTriPrismSurfArea(sIn, hIn);

dpb
dpb 2021 年 8 月 29 日
The error about not enough input arguments will come from how you called the function -- which you didn't show us. Whatever that was, it won't have passed two arrays in as the function expects/requires.
BUT, your function is fatally flawed in several ways -- first the initial line undoubtedly needs the "dot" element-wise operators .* and .^ in place of the matrix operators * and ^.
It would appear that's all your function would need; the rest would simply sum those areas.
As you've written it, however, you overwrite the input variables s and h and so the remainder of the code is totally bogus.
function [SA]=equTriPrismSurfArea(s,h)
SA=sqrt(3)/2*sum(s.^2 + 3.*s.*h);
end
If the function is designed for equilateral triangles as the comment says, then you don't need to pass the array of dimensions for each side -- and there would be only four surfaces, not five, anyways.
Then instead of the sum() you would just multiply the area of one side by 4.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by