Plotting multivariable function given a range of x and y to be plotted at given values of dependent var Z

68 次查看(过去 30 天)
Hello. Would like to ask for some help regarding my plot below. I can only incorporate the range of x and y but find it hard to plot at values of dependent variable z of 10,20,30 and 40. Please refer to my below code: Thank you
clc;
clear;
close all;
z=@(x,y)(x./(x.^2+y.^2));
x = 0:0.1:1;
y = 0:0.1:1;
[X, Y] = meshgrid(x, y);
surf(X, Y, z(X,Y));
xlabel('x');
ylabel('y');
zlabel('z');
figure;
contourf(X, Y, z(X,Y));

采纳的回答

DGM
DGM 2021-11-14
编辑:DGM 2021-11-14
If you're having trouble getting results closer to the singularity, that's because the value of z at the corner is NaN. Z is generally very large in that vicinity, but it's undefined (and not drawn) at exactly 0,0. The closer you can get the neighboring points, the more of the peak gets represented.
z = @(x,y)(x./(x.^2+y.^2));
x = linspace(0,0.1,100); % just use a finer mesh
y = linspace(0,0.1,100);
[X, Y] = meshgrid(x, y);
surf(X, Y, z(X,Y));
view(-16,18)
colormap(parula)
shading flat
xlabel('x');
ylabel('y');
zlabel('z');
Same would work for a contour map
clf;
contourf(X, Y, z(X,Y),[0 10 20 30 40]); % specific levels
colormap(parula)
shading flat
xlabel('x');
ylabel('y');
One way to avoid needing to use such a large number of points to represent features near the edge or corner of a selected domain is to use logspace instead of linspace. Both these examples use the same number of points.
clf;
% linear mesh spacing
subplot(1,2,1)
x = linspace(0,0.5,20);
y = linspace(0,0.5,20);
[X, Y] = meshgrid(x, y);
h = pcolor(X, Y, z(X,Y));
h.EdgeAlpha = 0.5;
caxis([0 81])
colormap(parula)
xlabel('x');
ylabel('y');
% log mesh spacing
subplot(1,2,2)
n = 20;
x = logspace(0,1.0414,n)/n-1/n;
y = logspace(0,1.0414,n)/n-1/n;
[X, Y] = meshgrid(x, y);
h = pcolor(X, Y, z(X,Y));
h.EdgeAlpha = 0.5;
caxis([0 81])
colormap(parula)
xlabel('x');
ylabel('y');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by