Array/Vector allocation not happening. Please Help.

Tc(1)=Tci;
Th(1)=Tho;
mcc=70 %cooling water mass flow rate
while (j>1)
massc=mass/a;
Qf=0;
hc=heat_transfer_model(Tci,P,mcc,dc); %d is channel diameter on cold side, P is pressure of cooling water
for k=1:1:n
Th(k);
if(j==10000)
cph=CoolProp.PropsSI('Cp0mass','T',Th(k),'P',phi,'CarbonDioxide');
hh=heat_transfer_model(Th(k),phi,massc,dc);
else
cph=CoolProp.PropsSI('Cp0mass','T',((Th(k)+Th(k+1))/2),'P',phi,'CarbonDioxide');
hh=heat_transfer_model(((Th(k)+Th(k+1))/2),phi,massc,dc);
end
syms Q;
syms x;
syms y;
area=l*(dc+(pi*(dc/2)));
U=1/((1/hh)+(k_t/t)+(1/hc));
a=Th(k);
b=Tc(k);
Tavgh=(a+x)/2;
Tavgc=(b+y)/2;
eqn1=Q-U*area*(Tavgh-Tavgc)==0;
eqn2=Q-massc*cph*(x-a)==0;
eqn3=Q-mcc*cpc*(y-b)==0;
[Q,x,y]=solve([eqn1,eqn2,eqn3],[Q,x,y]);
Qf=Qf+Q;
Th(k+1)=x
Tc(k+1)=y
end
j=abs(Thi-Th(n+1))
mcc=mcc+1
end
The user inputs Tho and Tci values. And these values are allocated to Th(1) and Tc(1). But the value of Tho and Tci gets allocated to all the elements of Th and Tc, which i dont want.
By using the solve command, i am getting x and y values. But these x and y values are not getting allocated to Th(k+1) and Tc(k+1). k+1 is having the initial values only that the user has put up. Not getting updated. Please help. I am a beginner in MATLAB.

Answers (2)

It should not. Put a breakpoint on the
mcc = 70;
line and look at those variables in the workspace. Only the first element should change, unless all of the elements were those prior to assignment. Set a breakpoint on the
Tc(1)=Tci;
line to see what values were in those arrays before doing the assignment.
You didn't define j

8 Comments

I did. But still the value of x and y is not getting allocated to Th(k+1) and Tc(k+1).
Do you know that if your while loop iterates a second time, all the Th and Tc values from the first iteration of the while loop will be blown away?
And please read my answer and tell us what you see.
Will the values blow away? I will give it a check then. because i did this because am acquainted with C/C++ and we can do so in those languages.
They would also get overwritten in C or any other language. You can do so in any language, and they'll all blow away the prior values. But just because you can do so doesn't mean you should do so.
Just look at your code. You have a for loop where k goes from 1 to n, and so you'll assign the first n elements of Th and Tc. Now you exit that loop, and what happens if j < 1 and you do another loop of the while loop? You will do that for loop again. And again, it will assign the very same first n elements of Th and Tc, overwriting the earlier values. Understand? Does that explain it well enough?
Thanks for your suggestions, my code is working fine now. There was a logical formula error in U. So now its working correct.
P.S. Regarding your point that the prior values of Th and Tc will get blown away in the next iteration is not correct. There are two cases of how we define a variable - globally or locally. If we define a global variable, then its value is stored for every ietration and can be used anywhere in the code. And if we define a variable locally, then we can access its value only within the loop, outside the loop it gets lost away. So yes, i have defined Th and Tc globally before this code and hence its values are stored and can be used for next iteration as well.
The concept of global and local variable applies to nearly all the languages.
Thanks.
Moreover he did not mention anything about global as was and is very clear from yor code that you haven't used global variable anywhere whatsoever... mostly try to avoid global variables
" And if we define a variable locally, then we can access its value only within the loop, outside the loop it gets lost away."
That is certainly not correct for MATLAB:
for k = 1:9
x = sqrt(k);
end
x
displays
x = 3
So x is defined in a loop and its value is accessible outside the loop, which contradicts your statement.
"Regarding your point that the prior values of Th and Tc will get blown away in the next iteration is not correct. There are two cases of how we define a variable - globally or locally. If we define a global variable, then its value is stored for every ietration and can be used anywhere in the code....So yes, i have defined Th and Tc globally before this code and hence its values are stored and can be used for next iteration as well."
A global variable will not "store" every iteration, it will get redefined on the next loop iteration, just as Image Analyst wrote. Defining a variable as global makes no difference to it getting redefined inside the loop.
"If we define a global variable, then its value is stored for every ietration..."
Declaring a variable as global does not "store" its values generated in a loop.
"...and can be used anywhere in the code"
Both the MATLAB documentation and experienced users recommend passing values between workspace as input/output arguments. This is generally more efficient, and is much more reliable than using global variables.
I created two test functions (attached), that differ only by the global variable declaration. When run, they give exactly the same result:
>> test1() % "local"
x = 3
>> test2() % "global"
x = 3
So declaring it as global made absolutely no difference to the value of the variable x after the loop has completed.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

on 12 Dec 2018

Edited:

on 14 Dec 2018

Community Treasure Hunt

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

Start Hunting!