2015-12-04 3 views
1

Je voudrais tracer la surface de cette fonction dans Matlab:Comment tracer un graphique d'une surface personnalisée dans Matlab?

w=(m*(1-p))/(h-1+m*(1-p)) 

h=0.98; 
P=[0.05:0.001:0.98]; 
M=[0:0.001:1]; 

Le graphique devrait ressembler à ceci:

enter image description here

J'ai essayé de mettre en œuvre la fonction graphique en tant que:

h=0.98; 
P=[0.05:0.001:0.98]; 
M=[0:0.001:1]; 
W=[]; 
for i=1:size(P, 2) 
    for j=1:size(M, 2) 
     p=P(i); 
     m=M(j); 
     w=(m*(1-p))/(h-1+m*(1-p)); 
     if w>1000 
      w=1000; 
     end 
     if w<0 
      w=0; 
     end 
     W(i, j) = w; 
    end 
end 
mesh(M, P, W); 

Mais puisque la fonction change rapidement autour de la condition aux limites, mon graphique a fini par ressembler à ceci:

enter image description here

Comment puis-je lisser la courbe et lui donner une grille agréable, uniforme comme dans le première image?

+1

En bref: [limite de l'axe z] (http://www.mathworks.com/help/matlab/ref/zlim.html) à 10 lors du traçage , et je suggère 'meshgrid' au lieu de boucles. –

Répondre

0

Voici une façon de faire ce que vous voulez. Voir les commentaires dans le code pour plus d'informations.

%% //Definitions 
w = @(h,m,p)(m.*(1-p))./(h-1+m.*(1-p)); %// w as a function handle 
w_min = 0; w_max = 10; %// For plots 
cMap = hot(200); cMap = cMap(floor(0.5*length(cMap)):ceil(0.7*length(cMap)),:); 

h = 0.98; 
m_start = 0; m_end = 1; m_step = [0.02 0.01 0.001]; 
p_start = 0.05; p_end = 0.98; p_step = [0.1 0.01 0.001]; 
%// Definitions of the mesh used in plotting (same for all charts) 
mesh_m_step = 0.05; mesh_p_step = 0.05; 
[meshM,meshP] = meshgrid(m_start:mesh_m_step:m_end,p_start:mesh_p_step:p_end); 
meshW = w(h,meshM,meshP); 
%% //Evaluation and plotting: 
figure(); set(gcf,'Position',[240,500,1391,376]); subplot(1,3,1); 
for ind1 = 1:3 
    %% // Evaluate equation 
    [MM,PP] = meshgrid(m_start:m_step(ind1):m_end,p_start:p_step(ind1):p_end); 
    WW = w(h,MM,PP); 
    %% // Plot 
    subplot(1,3,ind1); 
    surf(MM,PP,WW,'EdgeColor','none'); view([45,45]); 
    hold on; mesh(meshM,meshP,meshW,'EdgeColor','k','FaceColor','none');   
    colormap(cMap); %// Fix the colormap 
    zlim([w_min,w_max]); caxis(zlim) %// Modify zlim 
end 

qui se traduit par: Approximately what was required