Error in a function
3 ビュー (過去 30 日間)
古いコメントを表示
Good day.
I have the following function and I can't seem to be able to fix the error. This is a function that was in a pdf file I'm learning from and it told me to put it into matlab to see how it works and I'm pretty sure it was not supposed to do what it has done. This is NOT for an assignment or any other important thing that requires my own working, just so your concense is eased. You might it suspiciouse that I state this but I have been asked this question before so I answered it ahead of time.
Here is the function so you can copy it into your own Matlab.
function A = circle_area(diameter)
% help comments
% This function calculates the area of a
% circle. [Output units] = [Input units]^2
% Area = circle_area(diameter)
r = diameter/2;
A = pi*r.^2;
A = circle_area(0.2)
0 件のコメント
採用された回答
その他の回答 (2 件)
David Goodmanson
2020 年 6 月 11 日
編集済み: David Goodmanson
2020 年 6 月 11 日
Hi Morne'
Here Matlab assumed that the call to the function was part of the function itself. In a script, functions must be located below the main body of the code and each function must end with 'end'
A = circle_area(0.2)
function A = circle_area(diameter)
% help comments
% This function calculates the area of a
% circle. [Output units] = [Input units]^2
% Area = circle_area(diameter)
r = diameter/2;
A = pi*r.^2;
end
KSSV
2020 年 6 月 11 日
I suspect, you tried running the function using f5 button or run button from the editor. It is not the way to call the function. Copy this function in a folder and go to that folder, make it present working folder. And in the work space type this.
diameter = 5. ;
A = circle_area(diameter) ;
Now you will get the value of A.
4 件のコメント
KSSV
2020 年 6 月 11 日
You have named your function different. Name of the function and file name should be same.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!