MATLAB Figure file - matlab

I have a problem with contour figure, when I run my syntax I get error like:
Error using contourf (line 66)
Z must be size 2x2 or greater.
Error in example (line 660)
contourf (x,y,c); colorbar;
and this is my syntax:
%---------------------stack1--------------------------------------------
v = 0.5; % velocity
lambda = 0; % decay rate
Q = 1; % emission rate(s)
u = 3; % wind speed
P = 1; % Ambient Pressure
D = 6; % inside diameter
V = 22.4; % volumetric flow rate of stack gas
Ts = 400; % temperature of stack gas
Ta = 283; % temperature of ambient air
xstack = 0; ystack = 60; % stack location(s)
xmin = 0.5; xmax = 3.5; % x-axis interval
ymin = 0; ymax = 100; % y-axis interval (used only for d>1))
h = 50; % physical stack height
z = 0; % height of observation (=0 for ground surface)
gplot = 1; % plot option (=1 yes; =0 no)
gcont = 2; % contour plot option (=2 filled; =1 yes; =0 none)
%----------------------------------execution-------------------------------
[x,y] = meshgrid (linspace(xmin,xmax,100),linspace(ymin,ymax,100));
c = zeros (size(x)); e = ones(size(x));
Dy = linspace(1,100,100); % in meters
Dz = Dy'; % in meters
[Dy,Dz] = meshgrid(Dz,Dy);
for i=1:100
for j=1:100
%...Pasquill-gifford for Dy
c = 24.167; d = 2.5334;%...Pasquill Stability Category is A
if x(i,j)<0.10; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 18.3330; d = 1.8096;%...Pasquill Stability Category is B
if x(i,j)<0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 12.5000; d = 1.0857;%...Pasquill Stability Category is C
if x(i,j) == 4; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 8.3330; d = 0.72382;%...Pasquill Stability Category is D
if x(i,j)== 0.30 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 6.2500; d = 0.54287; %...Pasquill Stability Category is E
if x(i,j) < 0.10 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 4.1667; d = 0.36191; %...Pasquill Stability Category is F
if x(i,j) < 0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
%...Pasquill-gifford for Dz
if x(i,j)<0.10 % x in kilometers
a = 122.8; b = 0.9447;%...stabilitas is A
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.100 <= x(i,j) && x(i,j) < 0.150
a = 158.08; b = 1.0542;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.150 <= x(i,j) && x(i,j) < 0.200
a= 170.22 ; b= 1.0932 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.200 <= x(i,j) && x(i,j) < 0.250
a= 179.52 ; b= 1.1262 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.250 <= x(i,j) && x(i,j) < 0.300
a= 217.41 ; b= 1.2644 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.300 <= x(i,j) && x(i,j) < 0.400
a= 258.89 ; b= 1.4094 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.400 <= x(i,j) && x(i,j) < 0.500
a= 346.75 ; b= 1.7283 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.500 <= x(i,j) && x(i,j) < 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif x(i,j) >= 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
if x(i,j)<0.20 % x in kilometers
a = 90.673; b = 0.93198;%...stabilitas is B
Dz(i,j)= a.*x(i,j).^b;
end
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.21 <= x(i,j) && x(i,j) < 0.40
a = 98.483; b = 0.98332;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
elseif x(i,j)>= 0.40
a = 109.400; b = 1.09710;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j) == all % x in kilometers
a = 61.141; b = 0.91465;%...stabilitas is C
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j)== 0.30 % x in kilometers
a = 34.459; b = 0.86974;%...stabilitas is D
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 32.093; b = 0.81066;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 3.00
a = 32.093; b = 0.64403;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 10.00
a = 33.504; b = 0.60486;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 30.00
a = 36.650; b = 0.56589;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 30.00
a = 44.053; b =0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.10 % x in kilometers
a = 24.260; b = 0.83660; %...stabilitas is E
Dz(i,j)= a.*x(i,j).^b;
end
if 0.10 <= x(i,j) && x(i,j) < 30.00
a = 23.331; b = 0.81956;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 21.628; b = 0.75660;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 21.628; b = 0.63077;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 4.00
a = 22.534; b = 0.57154;
Dz(i,j)= a.*x(i,j).^b;
end
if 4.01 <= x(i,j) && x(i,j) < 10.00
a = 24.703; b = 0.50527;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 20.00
a = 26.970; b = 0.46713;
Dz(i,j)= a.*x(i,j).^b;
end
if 20.01 <= x(i,j) && x(i,j) < 40.00
a = 35.420; b = 0.37615;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) >=40.00
a = 44.053; b = 0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.20 % x in kilometers
a = 15.209; b = 0.81558;%...stabilitas is F
Dz(i,j)= a.*x(i,j).^b;
end
if 0.21 <= x(i,j) && x(i,j) < 0.70
a = 14.457; b = 0.78407;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.71 <= x(i,j) && x(i,j) < 1.00
a = 13.953; b = 0.68465;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 13.953; b = 0.63227;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 3.00
a = 14.823; b = 0.54503;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 7.00
a = 16.187; b = 0.46490;
Dz(i,j)= a.*x(i,j).^b;
end
if 7.01 <= x(i,j) && x(i,j) < 15.00
a = 17.836; b = 0.41507;
Dz(i,j)= a.*x(i,j).^b;
end
if 15.01 <= x(i,j) && x(i,j) < 30.00
a = 22.651; b = 0.32681;
Dz(i,j)= a.*x(i,j).^b;
end
if 30.01 <= x(i,j) && x(i,j) < 60.00
a = 27.074; b = 0.27436;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 60.00
a = 34.219; b = 0.21716;
Dz(i,j)= a.*x(i,j).^b;
end
end
end
end
end
for i = 1:size(Q,2)
xx = x - xstack(i);
yy = y - ystack(i);
end
deltah(i,j) = V.*D./u.*1.5 + 2.68.*(10^(-3)).*P.*(Ts-Ta).*D./Ts;
H = h + deltah;
c1 = c + Q(i).*e./(4.*pi.*xx.*sqrt(Dy.*Dz)).*exp(-v.*yy.*yy./(4.*Dy.*xx)).*...
(exp(-v.*(z-H(i)).*(z-H(i)).*e./(4.*Dz.*xx))+exp(-v.*(z+H(i)).*(z+H(i)).*e./(4.*Dz.*xx)))...
.*exp(-lambda.*xx./v);
%--------------------------stack2---------------------------------------------
v = 0.5; % velocity
lambda = 0; % decay rate
Q = 1; % emission rate(s)
u = 3; % wind speed
P = 1; % Ambient Pressure
D = 6; % inside diameter
V = 22.4; % volumetric flow rate of stack gas
Ts = 400; % temperature of stack gas
Ta = 283; % temperature of ambient air
xstack = 0; ystack = 40; % stack location(s)
xmin = 0.5; xmax = 3.5; % x-axis interval
ymin = 0; ymax = 100; % y-axis interval (used only for d>1))
h = 50; % physical stack height
z = 0; % height of observation (=0 for ground surface)
gplot = 1; % plot option (=1 yes; =0 no)
gcont = 2; % contour plot option (=2 filled; =1 yes; =0 none)
%----------------------------------execution-------------------------------
[x,y] = meshgrid (linspace(xmin,xmax,100),linspace(ymin,ymax,100));
c = zeros (size(x)); e = ones(size(x));
Dy = linspace(1,100,100); % in meters
Dz = Dy'; % in meters
[Dy,Dz] = meshgrid(Dz,Dy);
for i=1:100
for j=1:100
%...Pasquill-gifford for Dy
c = 24.167; d = 2.5334;%...Pasquill Stability Category is A
if x(i,j)<0.10; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 18.3330; d = 1.8096;%...Pasquill Stability Category is B
if x(i,j)<0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 12.5000; d = 1.0857;%...Pasquill Stability Category is C
if x(i,j) == 4; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 8.3330; d = 0.72382;%...Pasquill Stability Category is D
if x(i,j)== 0.30 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 6.2500; d = 0.54287; %...Pasquill Stability Category is E
if x(i,j) < 0.10 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 4.1667; d = 0.36191; %...Pasquill Stability Category is F
if x(i,j) < 0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
%...Pasquill-gifford for Dz
if x(i,j)<0.10 % x in kilometers
a = 122.8; b = 0.9447;%...stabilitas is A
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.100 <= x(i,j) && x(i,j) < 0.150
a = 158.08; b = 1.0542;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.150 <= x(i,j) && x(i,j) < 0.200
a= 170.22 ; b= 1.0932 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.200 <= x(i,j) && x(i,j) < 0.250
a= 179.52 ; b= 1.1262 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.250 <= x(i,j) && x(i,j) < 0.300
a= 217.41 ; b= 1.2644 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.300 <= x(i,j) && x(i,j) < 0.400
a= 258.89 ; b= 1.4094 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.400 <= x(i,j) && x(i,j) < 0.500
a= 346.75 ; b= 1.7283 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.500 <= x(i,j) && x(i,j) < 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif x(i,j) >= 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
if x(i,j)<0.20 % x in kilometers
a = 90.673; b = 0.93198;%...stabilitas is B
Dz(i,j)= a.*x(i,j).^b;
end
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.21 <= x(i,j) && x(i,j) < 0.40
a = 98.483; b = 0.98332;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
elseif x(i,j)>= 0.40
a = 109.400; b = 1.09710;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j) == all % x in kilometers
a = 61.141; b = 0.91465;%...stabilitas is C
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j)== 0.30 % x in kilometers
a = 34.459; b = 0.86974;%...stabilitas is D
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 32.093; b = 0.81066;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 3.00
a = 32.093; b = 0.64403;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 10.00
a = 33.504; b = 0.60486;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 30.00
a = 36.650; b = 0.56589;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 30.00
a = 44.053; b =0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.10 % x in kilometers
a = 24.260; b = 0.83660; %...stabilitas is E
Dz(i,j)= a.*x(i,j).^b;
end
if 0.10 <= x(i,j) && x(i,j) < 30.00
a = 23.331; b = 0.81956;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 21.628; b = 0.75660;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 21.628; b = 0.63077;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 4.00
a = 22.534; b = 0.57154;
Dz(i,j)= a.*x(i,j).^b;
end
if 4.01 <= x(i,j) && x(i,j) < 10.00
a = 24.703; b = 0.50527;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 20.00
a = 26.970; b = 0.46713;
Dz(i,j)= a.*x(i,j).^b;
end
if 20.01 <= x(i,j) && x(i,j) < 40.00
a = 35.420; b = 0.37615;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) >=40.00
a = 44.053; b = 0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.20 % x in kilometers
a = 15.209; b = 0.81558;%...stabilitas is F
Dz(i,j)= a.*x(i,j).^b;
end
if 0.21 <= x(i,j) && x(i,j) < 0.70
a = 14.457; b = 0.78407;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.71 <= x(i,j) && x(i,j) < 1.00
a = 13.953; b = 0.68465;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 13.953; b = 0.63227;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 3.00
a = 14.823; b = 0.54503;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 7.00
a = 16.187; b = 0.46490;
Dz(i,j)= a.*x(i,j).^b;
end
if 7.01 <= x(i,j) && x(i,j) < 15.00
a = 17.836; b = 0.41507;
Dz(i,j)= a.*x(i,j).^b;
end
if 15.01 <= x(i,j) && x(i,j) < 30.00
a = 22.651; b = 0.32681;
Dz(i,j)= a.*x(i,j).^b;
end
if 30.01 <= x(i,j) && x(i,j) < 60.00
a = 27.074; b = 0.27436;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 60.00
a = 34.219; b = 0.21716;
Dz(i,j)= a.*x(i,j).^b;
end
end
end
end
end
for i = 1:size(Q,2)
xx = x - xstack(i);
yy = y - ystack(i);
end
deltah(i,j) = V.*D./u.*1.5 + 2.68.*(10^(-3)).*P.*(Ts-Ta).*D./Ts;
H = h + deltah;
c2 = c + Q(i).*e./(4.*pi.*xx.*sqrt(Dy.*Dz)).*exp(-v.*yy.*yy./(4.*Dy.*xx)).*...
(exp(-v.*(z-H(i)).*(z-H(i)).*e./(4.*Dz.*xx))+exp(-v.*(z+H(i)).*(z+H(i)).*e./(4.*Dz.*xx)))...
.*exp(-lambda.*xx./v);
%----------------------------------output----------------------------------
if gplot
for i = 10:10:100
plot (c1(:,i)); hold on;
plot (c2(:,i)); hold on;
end
end
if gcont
figure;
if gcont > 1
contourf (x,y,c); colorbar;
else
contour (x,y,c);
end
end
Anyone can help me please?!

When I run the code you have posted, I find that the 'c' variable is a single value, while 'x' and 'y' are matrices. So your problem is in the dimensions of the variables you try to plot together.
To debug your code try inserting a breakpoint in the code where the error occurs. Then when you run the code, it will stop at the breakpoint and you can check the values and dimensions of your variables.

Related

matlab system of equations with one unchanging solution ('q')

I am trying to solve a model of 4 equations, where everything is real. It is essentially a pipe branch problem with "1" being upstream of the pump (with some head on it), and "2" and "3" are downstream w/o head. The units are English.
Anyway, I'm just solving the system of 4 equations and 4 unknowns and looking at a plot of head (ha) vs. total flow rate (Q1). Across this space though I only am collecting points in a vertical line, i.e. the head changes but the flowrate doesn't.
I was thinking the problem may be some variables are being held onto, but I treat "ha" the same as "Q1" so that doesn't seem likely... Realistically, this is matching a system curve to a pump curve, and of course that point moves when the system changes (both flowrate and head), so it seems I am doing something wrong....
Here is the script:
clear sol1 sol2 sol3 sol4 S q h
clc; clearvars; close all;
syms Q1 Q2 Q3 ha
D1 = 1.25/12; Z1 = 0; P1 = 62.4*10; A1 = pi*(1/4)*D1*D1; f1 = 0.011; L1 = 50;
D2 = 0.5/12; Z2 = 25; P2 = 0; A2 = pi*(1/4)*D2*D2; f2 = 0.011; L2 = 100; %V2 = Q2 / A2;
D3 = 0.5/12; Z3 = 10; P3 = 0; A3 = pi*(1/4)*D3*D3; f3 = 0.011; L3 = 100; %V3 = Q3 / A3;
gamma = 62.468;
origPumpData = [0 150; 0.4 148; 0.8 145; 1.2 143; 1.6 141; 2 139; 2.4 138; 2.8 136; 3.2 134; 3.6 132; 4 130; 4.4 128; 4.8 127; 5.2 125; 5.6 124; 6 121; 6.4 118; 6.8 117; 7.2 115; 7.6 113; 8 111; 8.4 110; 8.8 108; 9.2 106; 9.6 104; 10 102; 10.4 100; 10.8 98; 11.2 97;
11.6 94; 12.0 93; 12.4 91; 12.8 89; 13.2 87; 13.6 86; 14 84; 14.4 82; 14.8 80; 15.2 78; 15.6 76; 16 74; 16.4 72; 16.6 70];
pumpData(:,1) = origPumpData(:,1)/448.8312;
pumpData(:,2) = origPumpData(:,2);
polyFit = fit(pumpData(:,1),pumpData(:,2),'poly2');
assume(in(Q1,'real') & Q1 > 0.0001 & Q1 < 1 );
assume(in(Q2,'real') & Q2 > 0.0001 & Q2 < 1 );
assume(in(Q3,'real') & Q3 > 0.0001 & Q3 < 1 ) ;
assume(in(ha,'real') & ha > 0.0001 & ha < 1000.0);
eq1 = #(Q1) polyFit.p1*Q1*Q1 + polyFit.p2*Q1 + polyFit.p3;
eq2 = #(P1,Z1,F1,Q1,A1,L1,D1,P2,Z2,F2,Q2,A2,L2,D2) P1/gamma + ((Q1/A1)^2)/64.4 + Z1 - hf(F1,L1,D1,Q1,A1) - hf(F2,L2,D2,Q2,A2) + ha == P2/gamma + ((Q2/A2)^2)/64.4 + Z2;
eq3 = #(P1,Z1,F1,Q1,A1,L1,D1,P3,Z3,F3,Q3,A3,L3,D3) P1/gamma + ((Q1/A1)^2)/64.4 + Z1 - hf(F1,L1,D1,Q1,A1) - hf(F3,L3,D3,Q3,A3) + ha == P3/gamma + ((Q3/A3)^2)/64.4 + Z3;
eq4 = Q1 == Q2 + Q3;
guesses = [0.07116936917086152675558661517184;
0.035125850031368916048762418970837;
0.036043519139492610706824196201003;
303.02361035523126099594683749354];
for i = 1:1:2
disp(i)
disp(Z1)
Z1 = Z1-5
f1 = f1+0.01
f3 = f3 + 0.01
S(i) = vpasolve([eq1(Q1) eq2(P1,Z1,f1,Q1,A1,L1,D1,P2,Z2,f2,Q2,A2,L2,D2) eq3(P1,Z1,f1,Q1,A1,L1,D1,P3,Z3,f3,Q3,A3,L3,D3) eq4], [Q1 Q2 Q3 ha], guesses )
S(i).Q1
S(i).Q2
S(i).Q3
S(i).ha
q(i) = S(i).Q1
h(i) = S(i).ha
guesses = [S(i).Q1 S(i).Q2 S(i).Q3 S(i).ha]'
clear S
end
plot( q, h, '*b');
function hf = hf(f,L,D,Q,A)
hf = f*(L/D)*(1/64.4)*(Q/A)^2;
end
you're solving eq1 == 0(default) and computing Q1, independent of other equations and parameters. that's why you get constant Q1 in every iteration.

Convert aggregated measures to dimensions in tableau

I have created a calculated field [Segment] using the below codes. CalculationZ is also a calculated field. I need to convert the calculated field Segment to
a dimension. Is it possible in tableau. Please share the codes or formula/ methods to achieve this
IF [CalculationZ] < 0.10 THEN
"A1"
ELSEIF [CalculationZ] >= 0.10 and [CalculationZ] < 0.20 THEN
"A2"
ELSEIF [CalculationZ] >= 0.20 and [CalculationZ] < 0.30 THEN
"A3"
ELSEIF [CalculationZ] >= 0.30 and [CalculationZ] < 0.40 THEN
"A4"
ELSEIF [CalculationZ] >= 0.40 and [CalculationZ] < 0.50 THEN
"A5"
ELSEIF [CalculationZ] >= 0.50 and [CalculationZ] < 0.60 THEN
"A6"
ELSEIF [CalculationZ] >= 0.60 and [CalculationZ] < 0.70 THEN
"A7"
ELSEIF [CalculationZ] >= 0.70 and [CalculationZ] < 0.80 THEN
"A8"
ELSEIF [CalculationZ] >= 0.80 and [CalculationZ] < 0.90 THEN
"A9"
ELSEIF [CalculationZ] >= 0.90 and [CalculationZ] <= 1.00 THEN
"A10"
else "A0"
END
Just drag the field to dimensions.
You can even use a field as measure and dimension in a single view. See how calculationZ is used as a dimension and measure in below view.

writing optimization constraints in MATLAB involving function calls

I am trying to solve following optimization problem:
I am using calculating θ1, θ2 and θ3 for every value of phi discretized between 30 deg. to 150 deg.
function thetas = inverse_kinematics_1(l1,l2,l3,phi)
x = 100;
y = 0;
x1 = x - (l3*cos(phi));
y1 = y - (l3*sin(phi));
a = sqrt(x1^2 + y1^2);
y2 = -y1/a;
x2 = -x1/a;
gamma = atan2(y2,x2);
c = (- x1^2 - y1^2 - l1^2 + l2^2)/(2*l1*a);
d = acos(c);
theta1 = gamma + d;
if theta1 < 0
theta1 = theta1 + 2*pi;
end
e = (y1 - l1*sin(theta1))/l2;
f = (x1 - l1*cos(theta1))/l2;
theta2 = atan2(e,f) - theta1;
if theta2 < 0
theta2 = theta2 + 2*pi;
end
theta3 = (phi)- (theta1 + theta2);
if theta3 < 0
theta3 = theta3 + 2*pi;
end
thetas = [theta1,theta2,theta3].*180/pi;
end
How can I write the constraints in this situation ?

how to select wanted ( needed situations) dual elements which are randomly generated by using random sampling in matlab?

I have 2 different random selected variables. The first one is number of bedrooms which is r1 and the second one is number of people in the dwelling which is r2. there are different certain constant values, which are going to be used for selection of house size.Wanted combinations
There are 24 different combinations that both of random generators can produce by using number of bedrooms and number of people in the house. there is no problem if the random generators produce wanted combinations.If not, there is a problem come out which is unwanted combinations.
How can get rid of this unwanted combinations or how to solve this problem in another way?
My code is as follows:
R1 = randsample('xyzq',1,true,[0.1 0.2 0.43 0.27]); % Probability of number of bedrooms in dwellings
r1=R1;
R2 = randsample('abcdef',1,true,[0.283 0.358 0.163 0.134 0.044 0.018]); %Probability of Household size in UK
r2=R2;
if (r1 == 'x' && r2 == 'a') % 37m2 1 bed, 1 per
A_roof = 37;
A_floor = 37;
A_wall = 35;
A_door = 3;
A_windows = 3;
elseif (r1 == 'x' && r2 == 'b') % 50m2 1 bed, 2 per
A_roof = 50;
A_floor = 50;
A_wall = 47;
A_door = 6;
A_windows = 6;
elseif (r1 == 'y' && r2 == 'c') % 61m2 2 bed, 3 per
A_roof = 61;
A_floor = 61;
A_wall = 42;
A_door = 5;
A_windows = 5;
elseif (r1 == 'y' && r2 == 'd') % 70m2 2 bed, 4 per
A_roof = 70;
A_floor = 70;
A_wall = 50;
A_door = 5;
A_windows = 5;
elseif (r1 == 'y' && r2 == 'd') % 74m2 3 bed, 4 per
A_roof = 74;
A_floor = 74;
A_wall = 51;
A_door = 6;
A_windows = 6;
elseif (r1 == 'z' && r2 == 'e') % 86m2 3 bed, 5 per
A_roof = 86;
A_floor = 86;
A_wall = 55;
A_door = 6;
A_windows = 6;
elseif (r1 == 'z' && r2 == 'f') % 95m2 3 bed, 6 per
A_roof = 95;
A_floor = 95;
A_wall = 70;
A_door = 7;
A_windows = 7;
elseif (r1 == 'q' && r2 == 'e') % 90m2 4 bed, 5 per
A_roof = 90;
A_floor = 90;
A_wall = 68;
A_door = 7;
A_windows = 7;
elseif (r1 == 'q' && r2 == 'f') % 99m2 4 bed, 6 per
A_roof = 99;
A_floor = 99;
A_wall = 74;
A_door = 8;
A_windows = 8;
elseif (r1 == 'y' && r2 == 'd') % 83m2 2 bed, 4 per
A_roof = 40;
A_floor = 83;
A_wall = 105;
A_door = 8;
A_windows = 8;
elseif (r1 == 'z') && (r2 == 'd') % 87m2 3 bed, 4 per
A_roof = 42;
A_floor = 87;
A_wall = 105;
A_door = 8;
A_windows = 8;
elseif (r1 == 'z' && r2 == 'e') % 96m2 3 bed, 5 per
A_roof = 46;
A_floor = 96;
A_wall = 150;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'e') % 100m2 4 bed, 5 per
A_roof = 50;
A_floor = 100;
A_wall = 180;
A_door = 10;
A_windows = 10;
elseif(r1 == 'q' && r2 == 'f') % 107m2 4 bed, 6 per
A_roof = 55;
A_floor = 107;
A_wall = 125;
A_door = 10;
A_windows = 10;
elseif (r1 == 'z' && r2 == 'e') % 102m2 3 bed, 5 per
A_roof = 50;
A_floor = 102;
A_wall = 200;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'e') % 106m2 4 bed, 5 per
A_roof = 55;
A_floor = 106;
A_wall = 200;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'f') % 113m2 4 bed, 6 per
A_roof = 60;
A_floor = 113;
A_wall = 200;
A_door = 10;
A_windows = 10;
end

How to specify a range and perform a function accordingly in matlab?

I need to perform the following function in matlab.
I had tried the following code but somehow my if statement is wrong. I'd like to know how to use the if statement efficiently here. If there is any other method in which i could perform the function please do help. My code is as follows
if (y(i,j) < -0.5, y(i,j) >= -1)
f(i,j) = 0
elseif (y(i,j) < 0, y(i,j) >= -0.5)
f(i,j) = 1
elseif (y(i,j) < 0.75, y(i,j) >= 0)
f(i,j) = 2
elseif (y(i,j) < 1, y(i,j) >= 0.75)
f(i,j) = 3
end
Here y(i,j) is a 1 x 256 matrix. Thanks
You need to use the logical AND operator to tie two Boolean expressions together. You are using a comma which is not correct:
if (y(i,j) < -0.5 && y(i,j) >= -1)
f(i,j) = 0
elseif (y(i,j) < 0 && y(i,j) >= -0.5)
f(i,j) = 1
elseif (y(i,j) < 0.75 && y(i,j) >= 0)
f(i,j) = 2
elseif (y(i,j) < 1 && y(i,j) >= 0.75)
f(i,j) = 3
end
However, it looks like you're using this in a for loop and I wouldn't perform the above in a loop. Use logical indexing instead:
f(y < -0.5 & y >= 1) = 0;
f(y < 0 & y >= -0.5) = 1;
f(y < 0.75 & y >= 0) = 2;
f(y < 1 & y >= 0.75) = 3;
This is assuming that f is the same size as y.