How to plot all contour lines in one plot?
1 view (last 30 days)
Show older comments
Jorge Arturo Clares Pastrana
on 15 Mar 2022
Commented: Star Strider
on 15 Mar 2022
Giiven a function f(x) I need to Plot the level sets contour lines where 𝑓(x) = −4.34, −4.3, −4.2, −4.1, −4, −3, −2, −1, 0.
Plot all of them in one graph
0 Comments
Accepted Answer
Star Strider
on 15 Mar 2022
Specify them as a single levels vector —
[X, Y, Z] = peaks(50);
figure
contour(X, Y, Z, [-4.34, -4.3, -4.2, -4.1, -4, -3, -2, -1, 0], 'ShowText',1)
colormap(turbo)
.
2 Comments
Star Strider
on 15 Mar 2022
First, the function needs to be vectorised so that it can do element-wise calculations. Otherwise, it could be because for the values of ‘x’ and ‘y’ you are using, ‘z’ has no values in the range of the desired contours. Also, ‘x’ and ‘y’ must be matrices, not vectors, although I suspect that would throw a specific error.
Use your values for ‘x’ and ‘y’ in something like this example code —
z = @(x,y) -4*x - 2*y - x.^2 + 2*x.^4 - 2*x.*y + 3*y.^2; % Vectorised Anonymous Function For 'z'
x = linspace(-5, 5, 150);
y = linspace(-5, 5, 150);
[X,Y] = ndgrid(x, y);
Z = z(X,Y);
figure
contour(X, Y, Z, [-4.34, -4.3, -4.2, -4.1, -4, -3, -2, -1, 0], 'ShowText',1)
colormap(turbo)
axis([-1 2 -1 2])
.
More Answers (1)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!