Writing code to call functions
2 次查看(过去 30 天)
显示 更早的评论
Given:
Find: Create a function that calculates the surface area of a sphere when provided with a radius as the input argument. Your code should work for both scalar radius values or an array of radii.
My Solution:
function A = AreaSphere(radius)
A=4*pi.*r^2
end
Code to call your function
r=randi(10,3,1);
Area=AreaSphere(r)
My Issue: I don't think I've learned how to properly call functions because I keep getting errors when trying to run function.
0 个评论
采纳的回答
Stephen23
2024-2-26
"I don't think I've learned how to properly call functions because I keep getting errors when trying to run function."
The problem is how you have written the function: you defined one input argument RADIUS which you did not use anywhere. And then inside the function you refer to a variable/function named R which you did not define anywhere.
You also need to use array operations for element-wise operations:
r=randi(10,3,1);
Area = AreaSphere(r)
function A = AreaSphere(radius)
A = 4*pi.*radius.^2;
end % ^^^^^^^^
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!