Medulla Oblongata
Medulla Oblongata

Reputation: 3961

How to miss out matrix elements in pcolor plot?

I'm solving a set of nonlinear simultaneous equations using Matlab's fsolve to find unknown parameters x1 and x2. The simultaneous equations have two independent parameters a and b, as defined in the root2d function:

function F = root2d(x,a,b)
F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(a+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - b;
end

I use the following code to solve the simultaneous equations, and plot the results as a 2d figure using pcolor.

alist = linspace(0.6,1.2,10);
blist = linspace(0.4,0.8,5);

% results
x1list = zeros(length(blist),length(alist));
x2list = zeros(length(blist),length(alist));

% solver options
options = optimoptions('fsolve','Display','None');

for ii = 1:length(blist)

    b = blist(ii);
    
    for jj = 1:length(alist)
    
        a = alist(jj);
        
        x0 = [0 0]; % init guess
        
        [xopt,yopt,exitflag] = fsolve(@(x0)root2d(x0,a,b),x0,options);
        
        % optimised values
        x1list(ii,jj) = xopt(1);
        x2list(ii,jj) = xopt(2);
        success(ii,jj) = exitflag; % did solver succeed?
        
    end
    
end

% plotting

figure
s = pcolor(alist(success>0),blist(success>0),x1list(success>0));
xlabel('a')
ylabel('b')
title('my data x_1')

figure
s = pcolor(alist(success>0),blist(success>0),x2list(success>0));
xlabel('a')
ylabel('b')
title('my data x_2')

However I only want to plot the x1 and x2 where the solver has successfully converged to a solution. This is where the success matrix element (or exitflag) has a value greater than 0. Usually you just write x1list(success>0) when using the plot function and Matlab omits any solutions where (success<=0), but pcolor doesn't have that functionality.

Is there a way around this? For example, displaying all (success<=0) solutions as a black area.

Upvotes: 0

Views: 50

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

Yes there is!

The easiest way is to make them NaN, so they are simply not drawn.

just do

x1list(~success)=NaN;
pcolor(alist,blist,x1list)

Upvotes: 1

Related Questions