Hello guys! I need a little help with 3d plotting. I need to do the coloring for the surface like this, but I cant get the yellowy parts to their correct positions.

This is what I have done so far (first is the function calling code, it wasnt made by me, only the function):
x=-4:0.1:4;
y=-4:0.1:4;
[X, Y] = meshgrid(x,y);
Z = 2*(peaks(X, Y)-3);
[C,U,V,W,fig1,plt1,plt2,fig2,plt3,cl] = tengerpart(X,Y,Z)
function [C,U,V,W,fig1,plt1,plt2,fig2,plt3,cl] = tengerpart(X,Y,Z)
C=gradient(Z);
[U, V, W]=surfnorm(X, Y, Z);
U=U;
V=V;
W=W;
fig1=figure(1);
plt1=surf(X, Y, Z, C);
xlim([-5 5]);
ylim([-5 5]);
zlim([-20 15]);
hold on;
plt2=quiver3(X, Y, Z, U, V, W, 'r');
end

 Accepted Answer

ok... what do you mean by "correct position" for the yellow parts? where is the correct?
strange things I noticed:
1) the "U=U; V=V; W=W" part means nothing, you can take that out.
2) when you do "C=gradient(Z);" you only get the x-component of the gradient, was that really your intention? to get both you shoud do "[Cx,Cy]=gradient(Z);".

4 Comments

This is how it looks by me, and this is not the one I need to create. By the correct position, I just want to achieve the coloring that I originally posted above, but all I could do was this (what Im sending now).
  1. The U=U and other parts are just there because those variables are fixed thanks to the task, and they are needed for the check.
  2. If thats all it needs to it, Im gonna check and write if it is solved.
ok, you are coloring acording to variable C, which is just the x-component of gradient, but by the pic I'm gonna guess it should be the magnitude of gradient. your code should be:
[Cx,Cy]=gradient(Z);
C=sqrt(Cx.^2+Cy.^2);
this way C is now the gradient magnitude, coloring should be ok now.
Thank you! It works properly now! Ive actually managed to pull off something with abs(Cy), but it wasnt good. I tried this magnitude thing, and it worked. Ive only used Matlab for like 2-3 months now, and the final is coming soon. Thank you once again!
@Tamás Zsombor Tolvaj, since it worked could you click the Accept this answer link to award @Daniel Vieira his "reputation points"? Thanks in advance. 🙂

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!