MATLAB: Plot 3-D Graph and overlap a function as a color gradient

3-d plotmeshgridoverlap functions

I'm using MATLAB R2013a.
%I have two IVs and one DV: x1,x2 (IV) and x3 (1-x1-x2, DV)
m=[];
for x1 = .1:.01:.8
for x2 = .1:.01:.8
x3 = 1-x1-x2;
%disp([i j k]);
m(end+1,:,:) = [x1;x2;x3];
end
end
condition_sum = m(:,3)<.099;
m(condition_sum,:) = [];
x1 = m(:,1);
x2 = m(:,2);
x3 = m(:,3);
%These x values are then used to determine gamma values as follows
A12 = -.59;
A13 = .72;
A23 = 1.35;
A21 = A12;
A31 = A13;
A32 = A23;
g1 = exp(2*A12*x1.*x2.^2 + 2*A13*x1.*x3.^2 - 3*A12*x1.^2.*x2.^2 - 3*A23*x2.^2.*x3.^2 - 3*A13*x1.^2.*x3.^2);
g2 = exp(2*A23*x2.*x3.^2 + 2*A21*x2.*x1.^2 - 3*A23*x2.^2.*x3.^2 - 3*A31*x3.^2.*x1.^2 - 3*A21*x2.^2.*x1.^2);
g3 = exp(2*A31*x3.*x1.^2 + 2*A32*x3.*x2.^2 - 3*A31*x3.^2.*x1.^2 - 3*A12*x1.^2.*x2.^2 - 3*A32*x3.^2.*x2.^2);
%Then, total pressure is calculated
P1sat = .193;
P2sat = .085;
P3sat = .061;
p = x1.*g1*P1sat + x2.*g2*P2sat + x3.*g3*P3sat;
%Unacceptable values are removed
m = [x1,x2,x3,p];
condition_pressure = m(:,4)>0.1500;
m(condition_pressure,:)= [];
%re-define x1,x2,x3,p
x1 = m(:,1);
x2 = m(:,2);
x3 = m(:,3);
p = m(:,4);
At this point, I wish to create a graphical representation of my two IVs and total pressure(p): (x1,x2,p) I used plot3(x1,x2,p) but that doesn't really show what I was picturing. I was picturing a type of meshgrid (I couldn't figure it out from their webpage: http://www.mathworks.com/help/matlab/ref/meshgrid.html). What is the best method to use?
Secondly, there is a cost function associated with x1, x2 and x3. It is as follows:
%cost function
c1x1 + c2x2 + c3x3 + penalty = cost
c1 = 0.04
c2 = 0.11
c3 = 0.17
penalty = 2.5p^2 %where p is p from before
I wished to overlap a cost function in the form of a color gradient. In other words, I wanted the previous meshgrid to have a shade of red to indicate high cost and a shade of blue to indicate low cost. What sort of function should I use to do this?
Any tips, suggestions, or useful resources would be much appreciated!
Thank you,
Matt
P.S.
Sorry for the obnoxious formatting…

Best Answer

x1vals = .1:.01:.8;
x2vals = .1:.01:.8;
[x1, x2] = ndgrid(x1vals, x2vas); %a mesh!


x3 = 1 - x1 - x2; %a mesh!
condition_sum = x3 < 0.099; %a mesh!
A12 = -.59;
A13 = .72;
A23 = 1.35;
A21 = A12;
A31 = A13;
A32 = A23;
%fastest to apply to whole mesh and throw away values later
g1 = exp(2*A12*x1.*x2.^2 + 2*A13*x1.*x3.^2 - 3*A12*x1.^2.*x2.^2 - 3*A23*x2.^2.*x3.^2 - 3*A13*x1.^2.*x3.^2);
g2 = exp(2*A23*x2.*x3.^2 + 2*A21*x2.*x1.^2 - 3*A23*x2.^2.*x3.^2 - 3*A31*x3.^2.*x1.^2 - 3*A21*x2.^2.*x1.^2);
g3 = exp(2*A31*x3.*x1.^2 + 2*A32*x3.*x2.^2 - 3*A31*x3.^2.*x1.^2 - 3*A12*x1.^2.*x2.^2 - 3*A32*x3.^2.*x2.^2);
%Then, total pressure is calculated
P1sat = .193;
P2sat = .085;
P3sat = .061;
p = x1.*g1*P1sat + x2.*g2*P2sat + x3.*g3*P3sat;
%cost function
% c1x1 + c2x2 + c3x3 + penalty = cost
c1 = 0.04;
c2 = 0.11;
c3 = 0.17;
penalty = 2.5*p.^2; %where p is p from before
%Unacceptable values are rejected
p_ok = condition_sum & p <= 0.1500;
goodp = p;
goodp(~p_ok) = NaN; %make the others NaN so they do not appear
goodpenalty = penalty;
goodpenalty(~p_ok) = NaN;
surf(x1, x2, goodp, goodpenalty);