Why doesn't my script work? It comes back with Error: File: equTriPrismSurfArea.m Line: 6 Column: 1 This statement is not inside any function.

1 回表示 (過去 30 日間)
function [SA]=equTriPrismSurfArea(s,h)
SA=((sqrt(3)/2)*(s^2))+(3*s*h);
end
s = [1 2 3 4 5];
h = [1 2 3 4 5];
for i = 1:5
s1 = s(i);
h1 = h(i);
SA = ((sqrt(3)/2) * s1^2 + 3*s1*h1);
fprintf ("\nTotal surface area of equilateral triangular prism is %f\n",SA)
end

採用された回答

John D'Errico
John D'Errico 2021 年 8 月 29 日
編集済み: John D'Errico 2021 年 8 月 29 日
READ THE CODE YOU WROTE.
function [SA]=equTriPrismSurfArea(s,h)
SA=((sqrt(3)/2)*(s^2))+(3*s*h);
end
s = [1 2 3 4 5];
... (MORE)
What is that end doing there?
What happens when MATLAB tries to use this function? What does MATLAB do when it sees the end?
What did it say?
Line: 6 Column: 1 This statement is not inside any function.
So after the end, is the rest of the code you wrote inside the function? NO? What will MATLAB do then? In fact, it gets upset, and it gives up.
What that code is supposed to do there, we don't know. Maybe that end statement is supposed to be there. How can we know? Only you know that.
When MATLAB tells you there is a problem in line 6, you might look at line 5, or line 4. Did you do something strange there?

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by