Passing a function value to another function

To whom it may concern:
I am aware that there is documentation to support this question, I haven't been able to implement a simple solution that isn't beyond my abilities. I have a function that calculates the fluid height inside a reservoir. I would like to pass this value to another function where I define the fluid height as a constant. I need to keep two seperate function files, actualise_level and intialise_level. What might you suggest? Thanks
FIRST FUNCTION:
function [ reservoir ] = actualise_level( reservoir, vol_in, vol_out)
constants;
reservoir(VOLUME) = VOLUME + (vol_in - vol_out) * DT
if reservoir(VOLUME) < 0
reservoir(VOLUME) = reservoir(VOLUME) + abs(min(reservoir(VOLUME)))
end
x = sqrt(4*BASE_INF^2*HEIGHT^2+8*(((BASE_SUP-BASE_INF)*HEIGHT*VOLUME)./LENGTH)) y = 2*HEIGHT*BASE_INF
if x >= y
h1 = (x-y)./(2*(BASE_SUP-BASE_INF)) %h1 is the fluid level as calculated
else error('No solution')
end
end
SECOND FUNCTION:
function [ reservoir ] = initialise_reservoir()
constants;
reservoir = zeros(1,NB_PARAMETERS_RESERV);
reservoir(BASE_INF) = 5; reservoir(BASE_SUP) = 7.5; reservoir(HEIGHT) = 25; reservoir(LENGTH) = 5; reservoir(VOLUME) = 350; reservoir(LEVEL) = h1? %this is where I would like to pass the value in
reservoir = [reservoir(BASE_INF) reservoir(BASE_SUP) reservoir(HEIGHT) ... reservoir(LENGTH) reservoir(VOLUME) reservoir(LEVEL)];
end

3 Kommentare

Stephen23
Stephen23 am 27 Feb. 2015
Bearbeitet: Stephen23 am 27 Feb. 2015
I would suggest that you store your parameters in a structure , which would be much clearer for you to work with. Rather than that very awkward
reservoir(index) = ...
you can simply do this:
params.base_ind = 5;
params.base_sup = 7.5
params.height = 25
etc. Doing this creates one convenient variable to pass around, but with the advantage of clear labeling of what each value means.
PR
PR am 28 Feb. 2015
Good suggestion. However, I have assignment constraints that prevent me from creating structures. I'd really like to be able to call up the result from one function and attribute the result to a constant in the other.
Image Analyst
Image Analyst am 28 Feb. 2015
I tagged it as homework for you.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rick Rosson
Rick Rosson am 27 Feb. 2015

0 Stimmen

You could write a script that calls each of the functions one or more times in the desired sequence, passing the output of one function as the input to the other function.

3 Kommentare

PR
PR am 28 Feb. 2015
Effectively, this would be a good option. However I must keep the two functions separate. Thanks for your suggestion.
Rick Rosson
Rick Rosson am 28 Feb. 2015
That's exactly what I am suggesting. Maybe you should try it before you simply reject it as inadequate.
PR
PR am 1 Mär. 2015
I will give it a try. Much obliged

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Hilfe-Center und File Exchange

Gefragt:

PR
am 27 Feb. 2015

Kommentiert:

PR
am 1 Mär. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by