User defined function for volume of a fuel tank

25 views (last 30 days)
Write a user-defined function (for the function name and argument, use V= Volfuel(y)) that gives the volume f fuel in the tank (in gallons) as a function of the height y (measured from the bottom). use the function to make a plot of the volume as a function of h for 0<=h<=40 in.
I have no clue where to even start on this problem. Any help would be awesome
  4 Comments
john barnes
john barnes on 13 Apr 2015
I am not sure where to start it even on on paper. I also do not completely understand what it means to write a user-defined function. I have a decent understanding on how to implement problems in Matlab, but I just don't really understand this one.
john barnes
john barnes on 13 Apr 2015
Also whenever I try to run it in Matlab, it says either "undefined variable y" or "not enough input arguments." The code I am using is below.
function v= volfuel(y)
r=20 R=2*r
v=(pi*y*R^2+R*r+r^2)/3

Sign in to comment.

Answers (2)

Chad Greene
Chad Greene on 12 Apr 2015
Where to start? The same way you'd solve the problem on paper. Actually, it's probably a good idea to solve the problem on paper before writing any code. I've found that getting equations into simplest form on paper makes the equations easy to code, easy to troubleshoot typos, and simple equations make for efficient computing.
On your paper and on your computer screen, you'll probably start with the knowns, then there will be some math, then a plot.
  1 Comment
john barnes
john barnes on 13 Apr 2015
I am not sure where to start it on paper. I also do not completely understand what it means to write a user-defined function

Sign in to comment.


Star Strider
Star Strider on 13 Apr 2015
Start with the shape of the tank. Is it a cylinder, sphere, or something else? If a cylinder for instance, is its long axis oriented vertically or horizontally? It’s essentially geometry from there.
The ‘user-defined function’ is a function you write to your specifications to do what you want it to do. See Function Basics for how functions work in MATLAB, and how to write them and use them.
  4 Comments
john barnes
john barnes on 13 Apr 2015
Should the equation for volume be inside the brackets after function? I think this might be where I am having trouble.
The volume equation is v=(pi*y(R^2+R*r+r^2))/3
Chad Greene
Chad Greene on 13 Apr 2015
Anything inside those brackets is an output of the function. I left it empty, because I don't know exactly what you want as output. Here's an example of a function that lets a user enter any radius and color of a circle. It calculates the area of the circle and plots the circle using the circles function.
The output h is simply a handle for the plotted graphics object(s). It's not necessary to include h, but it gives users the ability to tinker with properties after plotting.
function [area,h] = circlearea(radius,color)
% math:
area = pi*radius.^2;
% make a plot:
h = circles(1,1,radius,'facecolor',color);
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!