How do I make a user defined function in MATLAB to determine the volume of a revolving solid using the disk method?

9 ビュー (過去 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!

回答 (1 件)

Geoff Hayes
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')
? Are they supposed to be anonymous functions? Or are they radii?
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 件のコメント
whyamiincollege
whyamiincollege 2019 年 4 月 29 日
I have no idea what I'm doing I just know I want a function that calculates this equation: integral(pi*(outer radius function)^2-(inner radius function)^2) the bounds of the integral being min x and max x and the inner and outer radius functions are functions of x for ex: one could be (x-2) ans the other could be x^2 then i want to be able to recall this user defined function whenever i want in my actual code.
Geoff Hayes
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);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by