Plotting P-v graph using function file

Hi everyone.
I am new to Matlab.
I have been assigned to a project where I have to plot the pressure-specific volume graph of steam. I am to obtain my values of specific volume using a pre existing function file named SteamIC.m (what is to be entered into the main module is shown below)
v= SteamIC('vV_p', In1), where In1 is the input argument (in this case pressure)
I have defined my range of pressure values as linspace(0.0061248,220.64)
My issue is that the input argument (In1) must be a single value (scalar)
How am I to find out all of the specific volume values using the pressure range above? As I can only put in one pressure value at a time
Any help appreciated

 Accepted Answer

TADA
TADA on 15 Dec 2018
Edited: TADA on 15 Dec 2018
v = zeros(1, length(In1));
for i = 1:length(In1)
v(i) = SteamIC('vV_p', In1(i));
end
or the shorter version using arrayfun:
v = arrayfun(@(a) SteamIC('vV_p', a), In1);

4 Comments

Roopali Hothi
Roopali Hothi on 15 Dec 2018
Edited: Roopali Hothi on 15 Dec 2018
Thank you, the first solution worked great!
Just one question about the zeros function. Is the purpose of this to create a sort of 'blank' array (of the correct size) that we can start populating with each value of v that is calculated in the loop?
Is there a function that can go into the loop which stores each value in an array right after it is calculated?
TADA
TADA on 15 Dec 2018
Edited: TADA on 15 Dec 2018
Yes The Zeros Preallocates The Array. If You Don't Do It, It Will Still Work But Matlab Will Reallocate The Entire Array Each Iteration, Which Will Affect Your Performance If You Have A Pretty Big Array.
For Small Array Of A Few Hundreds Of Values And Maybe Thousands, I Doubt You Will Fill The Difference. But The Total Runtime of Your Software Is Added Up, so a Few Milliseconds Here And There May Add Up To A Lot Of Time Eventually. Besides It Is Better Practice To do Things "the Right Way"
Im Not Sure I Understand Your Last Question Though.
Ah I see.
My other question was whether there was a way of storing the value of v calculated in each loop, just before the loop is ended and restarts to calculate a new value of v.
In other words, you used an external function (that was outside the loop) to store the v value. Is there something I can put into the loop itself to store the value of v?
TADA
TADA on 15 Dec 2018
Edited: TADA on 15 Dec 2018
If I Follow, then By function Mean the vector (or Array) v?
I That Case You Can Define Varriables of Any Type Inside Any Control Block (loop, If/Else, Switch, Try/Catch/Finally, etc.)
Bear In Mind That By Declaring Something inside A Loop, It Will Be Overwritten In the next Iteration Of The Loop .

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 15 Dec 2018

Edited:

on 15 Dec 2018

Community Treasure Hunt

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

Start Hunting!