Calling an imported variable into a function

I have imported a numerical matrix of size 336*1, and am trying to use it within a function. When i try to call it without any additional codes, it gives me a warning to ' Explicitly initialize the variable' and am not sure how to do that without passing it as a parameter in the function. I mainly don't want to pass it as a parameter as i am working with optimization and the non-linear constraints function works only with one variable inputy.
Do let me know how i could potentially call it within a function

3 Comments

"I mainly don't want to pass it as a parameter as i am working with optimization and the non-linear constraints function works only with one variable inputy"
That is exactly what function parameterization is for:
Simply add the required input arguments to your function definition, and then call it using an anonymous function, just like the documentation shows.
I tried function parametrization however i got the following warning for the function below.
Unrecognized function or variable 'nlin'.
Error in test_4>nlinconst (line 80)
[c,ceq] = nlin(@const);
function [c,ceq] = nlinconst(C1Tin)
[c,ceq] = nlin(@const);
function [c,ceq] = const(x)
PolyCompEff = 0.85;
IsenExpo = 1.4;
PolyCompExpo = 1.503;
CompRatio = 8;
Cp_air = 1;
HxEff = 0.8;
WaterIn = 293.15;
Pch(1,1) = 2000;
Tch(1,1) = 400;
R = 0.287;
Vch = 10;
C1Tout(:,1) = C1Tin(:,1)*(CompRatio^(PolyCompExpo/(PolyCompExpo - 1)));
C2Tin(:,1) = WaterIn + HxEff*(WaterIn - C1Tout(:,1));
C2Tout(:,1) = C2Tin(:,1)*CompRatio^(PolyCompExpo/(PolyCompExpo - 1));
HXout(:,1) = WaterIn + HxEff*(WaterIn - C2Tout(:,1));
C1w(:,1) = (PolyCompEff^-1).*x(:,1).*Cp_air.*C1Tin.*(CompRatio^(PolyCompExpo/(PolyCompExpo - 1)) - 1);
C2w(:,1) = (PolyCompEff^-1).*x(:,1).*Cp_air.*C2Tin.*(CompRatio^(PolyCompExpo/(PolyCompExpo - 1)) - 1);
Mch(1,1) = IFChP*Vch/(R*Tch(1,1));
for i = 1:335
Tch(i+1,1) = Tch(i,1) + (Mch(i,1)^-1).*(1-IsenExpo^-1).*(x(i,1).*HXout(i,1) - x(i,2).*Tch(i,1)).*1800;
Pch(i+1,1) = (Mch(i,1) + (x(i,1) - x(i,2) ).*1800).*R.*x(i,1)./Vch;
Mch(i+1,1) = Pch(i,1).*Vch./(R.*Tch(i,1));
end
c(:,1) = 700 - Pch(:,1);
c(:,2) = Pch(:,1) - 6464;
c(:,3) = C1w(:,1) - 18.6;
c(:,4) = C2w(:,1) - 18.6;
c(:,6) = Mch(:,1) - 350;
ceq(1,1) = 400 - Tch(1,1);
ceq(1,2) = 2000 - Pch(1,1);
ceq(336,2) = 2000 - Pch(336,1);
end
end
"Unrecognized function or variable 'nlin'."
That error message is quite clear: where is nlin defined? Why do you think that MATLAB should know what it is?

Sign in to comment.

Answers (0)

Asked:

on 4 Jul 2020

Commented:

on 6 Jul 2020

Community Treasure Hunt

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

Start Hunting!