View Full Version : Function that computes derivative - pleaseee help!
You have water in a 10cm tall cylindrical can with a hole in it at the bottom. The flow from a hole depends on the water pressure, which is proportional to the height of the water above that hole: when water is W cm above a hole, then the flow from that hole will decrease the water height at a rate of 0.1*W cm/sec.
Write the function hprime = can1(t, h) that models the flow, then call ode45(@can1, [0 60], 10); to plot the water height over 60 secs starting full (10cm) . At roughly what time is there 1cm left?
I don't even know where to start ... ANY help would be soo appreciated!! :)
Y.F.N.C.G.
1 Dec 2008, 10:23pm
This is why I didn't go to college :confused:
shwaip
1 Dec 2008, 10:27pm
you need to start by figuring out the differential equation that applies to the situation you gave, then you can translate it into matlab.
fhm793
1 Dec 2008, 10:37pm
The differential equation would be F = -0.1*W correct? But I don't get where the height comes in? Thanks!
shwaip
1 Dec 2008, 10:44pm
you need to relate h' (the rate of change of the height of the water) to h (the current height of the water).
fhm793
1 Dec 2008, 10:55pm
oh so like ... hprime = 10 - 0.1*W ?
shwaip
1 Dec 2008, 11:17pm
close...it should be hprime = -0.1*h
so, now that you know the relation between h' and h, you can write the function:
function hprime=can1(t,h)
%use the relation above...
and then use ode45 as given
fhm793
1 Dec 2008, 11:38pm
ok so our teacher said we need to include F = -0.1 before our code so it would be ...
function hprime = can1(t,h)
F = -0.1;
hprime = -0.1*h;
... but then where does the F come in? I don't think this is right but would it need to be followed by something like W=F*hprime?
shwaip
1 Dec 2008, 11:52pm
yes.
function hprime = can1(t,h)
F=-0.1;
hprime = F*h;
fhm793
2 Dec 2008, 12:08am
Is that it though? Does W not need to be in it? I ran with ode45 like given and it gave me an error :(
(Thanks soo much for the help again)
shwaip
2 Dec 2008, 12:12am
i don't have matlab on this computer right now.
can you post the error that it gave you?
fhm793
2 Dec 2008, 12:18am
>> ode45(@can1, [0 60], 10);
??? Error using ==> feval
Undefined function or method 'can1' for input arguments of type 'double'.
Error in ==> odearguments at 111
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
shwaip
2 Dec 2008, 12:35am
is the can1 function in your path?
test it out by calling it:
can1(1,9)
it should return -0.9
shwaip
2 Dec 2008, 12:36am
also, if you don't mind letting me know, what did you search for (on google?) to get to this website?
fhm793
2 Dec 2008, 12:56am
ohh i see what you mean. i think i got it :)
there's also a second part that says modify your function to model it with another hole 5 cm above the hole at the bottom ... any suggestions about how i would go about that one?
oh and i google searched "matlab help forum" i think!
it will be similar, but you'll need to use an 'if' statement to see if the height of the water is above or below the hole that is 5cm high.
so:
function hprime = can2(t,h)
F=-0.1;
if( h > 0.5)
%it goes out through two holes
else
%it goes out through only one hole
end
thanks for letting me know how you found us
No problem thank YOU for all the help you've been amazing :)
Oh and also ... for the second part, would you just multiply the equation by 2 when there are 2 holes like this:
F = -0.1; %flow rate from 1 cm of water
if h > 0.5
hprime = 2*F*h; %it goes out through 2 holes
else
hprime = F*h; %it goes out through one hole
end
end
or am I thinking about it wrong?
remember that the rate going out depends on how high the water is above the hole. so it will go out of the top hole slower than the bottom hole.
hmm so would it be 1/2 instead or 1/h or something like that?
nope.
you know that the rate of flow out depends on the height of the water above the hole.
let's pretend that the water is 10 cm high (completely full). What's the rate of flow out of the hole that's 5cm high, and what's the rate of flow out of the bottom hole? Assume that they don't affect each other.
hmm well the flow out of the bottom hole would be the same as before right? then i know the flow out of the top hole would be slower than out of the bottom hole but i guess i dont understand how you would know exactly how fast that would be.
the flow out the bottom is the same, correct.
how high is the water above the top hole?
if we pretend that the top half of the can is its own can, how fast would the water come out of a hole if the water was 5cm above the hole?
um it would still be multiplied by -0.1 right? so -0.1*5?
right.
so you need to find how high the water is above the 5cm hole, determine the flow rate out of it, and add it to the rate out the bottom hole, and this will be the total h' when the water is > than 5cm deep
fhm793
2 Dec 2008, 10:02pm
so would it be something like this? ...
function hprime = can2(t,h)
F = -0.1; %flow rate from 1 cm of water
if h > 0.5
hprime = 1/2*F*h + F*h; %it goes out through 2 holes
else
hprime = F*h; %it goes out through 1 hole
end
... sorry, i understand the concept im just so bad at putting stuff into matlab terms
shwaip
2 Dec 2008, 10:11pm
test it for another value...
let's say the water is at 8cm.
then the water is 3cm above the top hole, and 8cm above the lower. this means:
hprime = 3*-0.1 + 8*-0.1 = -1.1
is this the same thing your formula gives?
fhm793
2 Dec 2008, 10:31pm
Ohh ok I see what you mean so ...
function hprime = can2(t,h)
F = -0.1; %flow rate from 1 cm of water
if h > 0.5
hprime = F*(h-5) + F*h; %it goes out through 2 holes
else
hprime = F*h; %it goes out through 1 hole
end
shwaip
2 Dec 2008, 10:39pm
looks good.
fhm793
2 Dec 2008, 10:42pm
yayyy thank you so much again you have no idea how long that wouldve taken me by myself haha
edcentric
3 Dec 2008, 3:49am
CODE???? what code. This is calculus. You can calculate a mathematical approximation, but until you write out the derivatives you can't start.
primesuspect
3 Dec 2008, 5:30am
ITT Shwaip teaches math :thumbsup:
haha you guys are confusing.
i think there is something wrong with the last part though because when i graph it it levels off at 2.5 and it should go down to 0 right?
shwaip
3 Dec 2008, 11:18am
you have a typo in your code.
shwaip
3 Dec 2008, 11:22am
CODE???? what code. This is calculus. You can calculate a mathematical approximation, but until you write out the derivatives you can't start.
What edcentric is saying is that you are having difficulty in two different areas. The matlab code to do this is fairly trivial - you'll get the hang of it quickly. However, you seem to be having trouble recognizing how to do the problem even outside of matlab. This is probably the simplest ODE that you'll find, and if you're having trouble with it, you need to read the book a little more or pay more attention in class. I mean, what are you going to do when you have a cone dripping into a cube, and the height of the water in the cone is decreasing by x cm/s and the question is how fast is the water in the cube rising.
This is just a basic level course. This is the hardest assignment we will have to do.
If anyone is still looking at this, do you know how you would set up a more general function based off the other one that would accept a list of hole positions? I tried to set it up like this ...
F = -0.1; %flow rate from 1 cm of water
if h > holes
hprime = F*(h-holes) + F*h; %it goes out through 2 holes
else
hprime = F*h; %it goes out through one hole
end
but realized that can't be right bc that only accounts for 2 holes when there could be any number of holes....
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.