Construct ternary grid, evaluate a function on the grid and contour plot in Matlab

后端 未结 3 890
南笙
南笙 2021-01-07 11:01

I need to evaluate a function (say) Fxy = 2*x.^2 +3 *y.^2; on a ternary grid x-range (0 - 1), y-range (0-1) and 1-x-y (0 - 1). I am unable to construct the ternary grid on w

3条回答
  •  不要未来只要你来
    2021-01-07 11:27

    I am the author of ternplot. As you have correctly surmised, ternpcolor does not do what you want, as it is built to grid data automatically. In retrospect, this was not a particularly wise decision, I've made a note to change the design. In the mean time this code should do what you want:

    EDIT: I've changed the code to find the intersection of two curves rather than just one.

    N = 10;
    x = linspace(0, 1, N);
    y = x;
    
    % The grid intersections on your diagram are actually rectangularly arranged,
    % so meshgrid will build the intersections for us
    [xx, yy] = meshgrid(x, y);
    zz = 1 - (xx + yy);
    
    % now that we've got the intersections, we can evaluate the function
    f1 = @(x, y) 2*x.^2 + 3*y.^2 + 0.1;
    Fxy1 = f1(xx, yy);
    Fxy1(xx + yy > 1) = nan;
    
    f2 = @(x, y) 3*x.^2 + 2*y.^2;
    Fxy2 = f2(xx, yy);
    Fxy2(xx + yy > 1) = nan;
    
    f3 = @(x, y) (3*x.^2 + 2*y.^2) * 1000; % different order of magnitude
    Fxy3 = f3(xx, yy);
    Fxy3(xx + yy > 1) = nan;
    
    subplot(1, 2, 1)
    % This constructs the ternary axes
    ternaxes(5);
    
    % These are the coordinates of the compositions mapped to plot coordinates
    [xg, yg] = terncoords(xx, yy);
    % simpletri constructs the correct triangles
    tri = simpletri(N);
    
    hold on
    % and now we can plot
    trisurf(tri, xg, yg, Fxy1);
    trisurf(tri, xg, yg, Fxy2);
    hold off
    view([137.5, 30]);
    
    subplot(1, 2, 2);
    ternaxes(5)
    % Here we plot the line of intersection of the two functions
    contour(xg, yg, Fxy1 - Fxy2, [0 0], 'r')
    axis equal
    

    EDIT 2: If you want to find the point of intersection between two contours, you are effectively solving two simultaneous equations. This bit of extra code will solve that for you (notice I've used some anonymous functions in the code above now, as well):

    f1level = 1;
    f3level = 1000;
    intersection = fsolve(@(v) [f1(v(1), v(2)) - f1level; f3(v(1), v(2)) - f3level], [0.5, 0.4]);
    % if you don't have the optimization toolbox, this command works almost as well
    intersection = fminsearch(@(v) sum([f1(v(1), v(2)) - f1level; f3(v(1), v(2)) - f3level].^2), [0.5, 0.4]);
    
    ternaxes(5)
    hold on
    contour(xg, yg, Fxy1, [f1level f1level]);
    contour(xg, yg, Fxy3, [f3level f3level]);
    ternplot(intersection(1), intersection(2), 1 - sum(intersection), 'r.');
    hold off
    

提交回复
热议问题