Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Contour question in matlab

  1. Feb 9, 2015 #1
    Hello everyone
    If I have a plot of a function of two variables and I want the whole curve over the whole x and y intervals but in addition I want to highlight with a black colour a specific region when the function is between two values
    F between -8 and 0 or what ever
    How can I do that !???????
    Thank you
     
  2. jcsd
  3. Feb 10, 2015 #2

    kreil

    User Avatar
    Gold Member

    This question is poorly presented, so I'll just take a guess as to what you mean.

    This sounds like it has nothing to do with contours, but that you just want to plot a certain interval of the function in a different color. In that case you just split the domains up and make separate plots overlaid.

    Code (Text):
    [x1,y1] = meshgrid(-8:0.25:-5,-8:0.25:8);
    [x2,y2] = meshgrid(-5:0.25:5,-8:0.25:8);
    [x3,y3] = meshgrid(5:0.25:8,-8:0.25:8);
    f = @(x,y) x.^2 - 3*y.^3;
    figure
    hold on
    plot3(x1,y1,f(x1,y1),'b','LineWidth',2)
    plot3(x2,y2,f(x2,y2),'k','LineWidth',2)
    plot3(x3,y3,f(x3,y3),'b','LineWidth',2)
    xlabel('x')
    ylabel('y')
    zlabel('z')
    view(-142,14)
     

    Attached Files:

  4. Feb 10, 2015 #3

    kreil

    User Avatar
    Gold Member

    I reread your question and realized you might have meant you want to only change the color of the function when the function value is in some interval (instead of the function being evaluated in an interval as in my last post). In this case, you can just use logical indexing to pick out the relevant function values.

    Notice the use of surfc here, which plots the surface and also adds a contour plot underneath.

    Code (Text):
    [x,y] = meshgrid(-8:0.25:8);
    f = @(x,y) x.^2 - 3*y.^3;
    z = f(x,y);
    q = find(z>0 & z<2);
    figure
    hold on
    surfc(x,y,z)
    plot3(x(q),y(q),z(q),'*r')
    xlabel('x')
    ylabel('y')
    zlabel('z')
    view(-143,42)
     

    Attached Files:

  5. Feb 11, 2015 #4
    Thank you very much for your time
    The second part is what I want where I want to highlight a specific region.
    I would love to ask some questions :
    1-Can I add the color of the region to the color bar or not?
    2- Can I change the steps of The numbers of the color bar instead of 10 20 30 I want them to take steps of 5 or some variable ?

    Thank you for your time and I hope the questions make a bit more sense than last time
     
  6. Feb 11, 2015 #5

    kreil

    User Avatar
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Contour question in matlab
  1. Matlab contour plot (Replies: 2)

Loading...