How do I make a user defined function in MATLAB to determine the volume of a revolving solid using the disk method?
7 次查看(过去 30 天)
显示 更早的评论
So first off, I'm not quite sure how to use user defined functions in matlab so my code is going to look ridiculous.
Second, in case you don't know, the equation for a revolving solid using the disk method integral of
pi*((outerradius^2)-(innerradius)^2)
Here is my code. Very wrong, but you get the idea.
clc;
clear all;
function=diskvolume(x)
outerradius = ('Enter function for the outer radius')
innerradius = input('Enter function for the inner radius')
minx = input('Enter lower bound x')
maxx = input('Enter upper bound x')
for x = minx:maxx
diskvolume = int(pi*((outerradius^2)-(innerradius)^2))
end
If someone could please briefly explain how to use a user defined function in their overall base code as well as explain how to write the code for the equation im trying to achieve that would be amazing!
0 个评论
回答(1 个)
Geoff Hayes
2019-4-29
By user-defined function, I suspect that they are just asking you to create a function with a specific signature (input and output parameters). Something like
function [volume] = diskvolume(param1, param2)
I don't know what your input parameters should be perhaps none perhaps one or more. The above are just example inputs. Note how we define the output parameter volume. Since we have defined a function called diskvolume, then it gets saved to a file called diskvolume.m.
Now in your code, you precede the function signature with two lines of code. This will create an error as you can only include comments before the function signature. So you can delete these two lines from your code.
Note also the input parameter (for your function) is an x but then you overwrite it with the for loop. Do you mean to do this? Perhaps you don[t need an input parameter at all (especially since your code prompts the user for some inputs).
What are
outerradius = ('Enter function for the outer radius')
innerradius = input('Enter function for the inner radius')
Also, for
for x = minx:maxx
diskvolume = int(pi*((outerradius^2)-(innerradius)^2))
end
you are setting a local variable to the same name as the function. This will cause an error and so use the output parameter of volume instead. And please clarify what you are doing here. You have a for loop with a x iterating variable but you don't use it in the calculation of the volume. How should they be used? Do you want to be summing up a volume on each iteration? Or do you just calculate one volume per iteration?
2 个评论
Geoff Hayes
2019-4-29
编辑:Geoff Hayes
2019-4-29
From integral, I think that you need to define an anonymous function and integrate that given the min and maximum limits of x. Once you figure out the function, f, then your code might be just
volume = integral(f, minx, maxx);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!