make function to plot countour and 3D

3 Ansichten (letzte 30 Tage)
nirwana
nirwana am 17 Mär. 2023
Beantwortet: Walter Roberson am 17 Mär. 2023
Hi, I want to make automatic function to plot 3D mesh and contour to given any function, but it turn doesn't work, can you help me ? here the function that I wrote
function plot3dfunc(f,Xmin,Xmax,Ymin,Ymax)
Z=f;
x=linspace(Xmin,Xmax,50);
y=linspace(Ymin,Ymax,50);
[X,Y] = meshgrid(x,y);
subplot(1,2,1);
cs=contour(X,Y,Z);clabel(cs);
xlabel('x_1');ylabel('x_2');
title('(a) Contour plot');grid;
subplot(1,2,2);
cs=surf(X,Y,Z);
zmin=floor(min(Z));
zmax=ceil(max(Z));
xlabel('x_1');ylabel('x_2');zlabel('f(x_1,x_2)');
title('(b) Mesh plot');
When I put
f=@(X,Y) 2+X-Y+2*X.^2+2*X.*Y+Y.^2;
plot3dfunc(f,-2,0,0,3)
it says :
Error using contour
Input arguments must be numeric or objects which can be converted to double.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Mär. 2023
f=@(X,Y) 2+X-Y+2*X.^2+2*X.*Y+Y.^2;
plot3dfunc(f,-2,0,0,3)
function plot3dfunc(f,Xmin,Xmax,Ymin,Ymax)
x=linspace(Xmin,Xmax,50);
y=linspace(Ymin,Ymax,50);
[X,Y] = meshgrid(x,y);
Z = f(X,Y);
subplot(1,2,1);
cs=contour(X,Y,Z);clabel(cs);
xlabel('x_1');ylabel('x_2');
title('(a) Contour plot');grid;
subplot(1,2,2);
cs=surf(X,Y,Z);
zmin=floor(min(Z));
zmax=ceil(max(Z));
xlabel('x_1');ylabel('x_2');zlabel('f(x_1,x_2)');
title('(b) Mesh plot');
end

Weitere Antworten (0)

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by