I am doing an optimization and need to include a look up table as constraint. A variable is another's lookup table value. Is there a way I can do that?
MATLAB: Is it possible to use a lookup table for optimization
look up tableoptimization
Related Solutions
If you want to remove them then use [] instead of NaN
xi1 = hits1./tot1yi1=log(xi1./(1-xi1));xplot1 = xi1yplot1 = yi1;mask = (xplot1==0) | (xplot1==1) | (isinf(xplot1));xplot1(mask) = [];yplot1(mask) = [];figure(1)plot(xplot1, yplot1,'linestyle','none','marker','o')hold onp1 = 4.8422;p2 = -2.4067;y = (p1.*xi1) + p2plot(xi1, y)hold offp = 1/(1+exp(-((p1.*xi1) + p2)))figure(3)plot(xi1, p)
I haven't written a separate mask for yplot1 because I assume both have invalid elements at matching locations. If not, then you can similarly write a mask for yplot1
I do not encounter that error. However, the code was pretty slow, so I cleaned it up and told it to optimize the calculations.
Caution: every one of your results is complex valued because of the values that you are taking ^2.5 are mostly negative. In the below code, I plot abs() of the values.
The Q() you see are converting the numbers to symbolic rational numbers. This makes the calculations more consistent and gives greater optimization opportunities (floating point calculations are not commutative but rational calculations are.)
You were using the same floating point constants over 100 times in some cases. I gave names to them.
Next time that you are generating code like you did this time, put in variables in place of the constants and generate the code with the variables and then you can insert assignments of values to the variables. This will make the generated code easier to read and less error prone.
X = linspace(0, 5, 25);syms xQ = @(v) sym(v);C603_ = Q(603);C997_ = 1/Q(997);C1603_ = Q(1603);C4163_ = Q(4163863);C4164_ = Q(4164863);C4166_ = 1/Q(4166463);C0199 = Q(.1992256641);C0204 = Q(.2040892228);C0220 = Q(.2205657803);C0230 = Q(.2306233919);C0298 = Q(.2988384962);C0306 = Q(.3061338342);C0441 = Q(.4411315605);C0461 = Q(.4612467837);C1260 = Q(1.260902791);C1291 = Q(1.291684341);C1487 = Q(1.498354984);C1566 = Q(1.566678694);C1891 = Q(1.891354186);C1937 = Q(1.937526512);C2500 = Q(2.5);C2787 = Q(2.787032085);C2820 = Q(2.820688929);C2844 = Q(2.844015460);C2878 = Q(2.878360445);C2996 = Q(2.996709968);C3133 = Q(3.133357388);P43_46 = C4163_ .* C4166_;P44_46 = C4164_ .* C4166_;P1_9 = C1603_ .* C997_;P6_9 = C603_ .* C997_;f1 = (-(C2844*(1+(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500))).*hypergeom(((-C2787)*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 2-(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C2844*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500)+C2787.*hypergeom(-1-(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 1-(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C2844*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x).*(-1+(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500))./((1+P1_9.*x).*(1-x).^C2500))./(-(C2844*(1+(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500))).*hypergeom(((-C2787)*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 2-(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C2844*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500)+hypergeom(-1-(C2787.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 1-(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C2844*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(-1+(C2787*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).^2);F1 = matlabFunction(f1, 'File', 'F1.m', 'optimize', true);y1 = F1(X);plot(X, abs(y1), '-','Color','[0 0 0]','DisplayName','\fontname {Helvetica} \fontsize{18} s=-0.5','LineWidth', 2.0, 'HandleVisibility','off') xlabel('\fontname{Times New Roman} Values of x','FontSize',22)ylabel('\fontname{Times New Roman} Values of y', 'FontSize', 22)hold on f2 = (((-C1260).*(1+(C0199).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500))).*hypergeom(((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 2+((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C1260.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500)+C0199.*hypergeom(-1+((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 1+((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C1260.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x).*(-1+(C0199.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500))./((1+P1_9.*x).*(1-x).^C2500))./(-C1891.*hypergeom(((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 2+(-C0199).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500), (C1260.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1+(C0199.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500)+hypergeom(-1+((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 1+((-C0199).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), (C1260.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(-1+(C0199.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(-1.+(C0298.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)));F2 = matlabFunction(f2, 'File', 'F2.m', 'optimize', true);y2 = F2(X); plot(X, abs(y2), '-','Color','[0 0 0]','DisplayName','\fontname {Helvetica} \fontsize{18} s=-0.5','LineWidth', 2.0, 'HandleVisibility','off') f3 = ((C0220.*(1-(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500))).*hypergeom((C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 2+(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), ((-C0220).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500)-C1487.*hypergeom(-1+(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 1+(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), ((-C0220).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x).*(-1-(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500))./((1+P1_9.*x).*(1-x).^C2500))./(C0441.*hypergeom((C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 2+(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), ((-C0220).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(1-P43_46.*x)./((1+P1_9.*x).*(1-x).^C2500)+hypergeom(-1+(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), 1+(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500), ((-C0220).*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(-1-(C1487.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)).*(-1-(C2996.*(1-P43_46.*x))./((1+P1_9.*x).*(1-x).^C2500)));F3 = matlabFunction(f3, 'File', 'F3.m', 'optimize', true);y3 = F3(X);plot(X, abs(y3), '-','Color','[0 0 0]','DisplayName','\fontname {Helvetica} \fontsize{18} s=-0.5','LineWidth', 2.0, 'HandleVisibility','off') f4 = (-(C2878.*(1+(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))).*hypergeom(((-C2820).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 2-(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C2878.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x)./((1+P6_9.*x).*(1-x).^C2500)+C2820.*hypergeom(-1-(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 1-(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C2878.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x).*(-1+(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))./((1+P6_9.*x).*(1-x).^C2500))./(-(C2878.*(1+(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))).*hypergeom(((-C2820).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 2-(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C2878.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x)./((1+P6_9.*x).*(1-x).^C2500)+hypergeom(-1-(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 1-(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C2878.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(-1+(C2820.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).^2);F4 = matlabFunction(f4, 'File', 'F4.m', 'optimize', true);y4 = F4(X);plot(X, abs(y4), '-.','Color','[0 0 0]','DisplayName','\fontname {Helvetica} \fontsize{18} MWCNT','LineWidth', 2.0)f5 = (((-C1291).*(1+(C0204.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))).*hypergeom(((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 2+((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C1291.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x)./((1+P6_9.*x).*(1-x).^C2500)+C0204.*hypergeom(-1+((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 1+((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C1291.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x).*(-1+(C0204.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))./((1+P6_9.*x).*(1-x).^C2500))./(-C1937.*hypergeom(((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 2+((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C1291.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1+(C0204.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x)./((1+P6_9.*x).*(1-x).^C2500)+hypergeom(-1+((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 1+((-C0204).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), (C1291.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(-1+(C0204.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(-1.+(C0306.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)));F5 = matlabFunction(f5, 'File', 'F5.m', 'optimize', true);y5 = F5(X);plot(X, abs(y5), '-.','Color','[0 0 0]','DisplayName','\fontname {Helvetica} \fontsize{18} s=1.0','LineWidth', 2.0, 'HandleVisibility','off') f6 =((C0230.*(1-(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))).*hypergeom((C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 2+(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), ((-C0230).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x)./((1+P6_9.*x).*(1-x).^C2500)-C1566.*hypergeom(-1+(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 1+(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), ((-C0230).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x).*(-1-(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500))./((1+P6_9.*x).*(1-x).^C2500))./(C0461.*hypergeom((C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 2+(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), ((-C0230).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(1-P44_46.*x)./((1+P6_9.*x).*(1-x).^C2500)+hypergeom(-1+(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), 1+(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500), ((-C0230).*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(-1-(C1566.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)).*(-1-(C3133.*(1-P44_46.*x))./((1+P6_9.*x).*(1-x).^C2500)));F6 = matlabFunction(f6, 'File', 'F6.m', 'optimize', true);y6 = F6(X);plot(X, abs(y6), '-.','Color','[0 0 0]','DisplayName','\fontname {Helvetica} \fontsize{18} s=1.0','LineWidth', 2.0, 'HandleVisibility','off')ax = gca; % current axes
ax.FontSize = 22;set(gca,'XLim',[0 5]);set(gca,'YLim',[-1 12]); box offhold off
Best Answer