Trying to add labels to a contour map of a given function

5 views (last 30 days)
Keaton Looper on 11 Jan 2023
Edited: Voss on 11 Jan 2023
The function uses two varying X1 and X2 variables and I want the function to be mapped as a contour plot given the varying X1 and X2 values. I also want to label the contour lines with their respective value.

Voss on 11 Jan 2023
X1 = 1:10;
X2 = 5:20;
F = X1.^2 + X1.*X2(:) - X2(:).^2;
contour(X1,X2,F,'ShowText','on')
xlabel('X1')
ylabel('X2')
Keaton Looper on 11 Jan 2023
l1 = 10
l2 = 10
K1 = 8
K2 = 1
P1= 5
P2 = 5
X = [-10:2:10]
Y = [-10:2:10]
Z = 1/2*(K1)*(sqrt(X.^2+(l1-Y).^2)-l1).^2+1/2*(K2)*(sqrt(X.^2+(l2+Y).^2)-l2).^2-P1*X-P2*Y
this above is the code i have so far. I am wanting to plot the contour plot of this function based on the varying X and Y values which are ranges of numbers. I then want to label the contour map
Voss on 11 Jan 2023
Edited: Voss on 11 Jan 2023
Use the transpose of Y (i.e., Y.') or Y(:) in the calculation of Z, because Z must be a matrix:
l1 = 10
l1 = 10
l2 = 10
l2 = 10
K1 = 8
K1 = 8
K2 = 1
K2 = 1
P1= 5
P1 = 5
P2 = 5
P2 = 5
X = -10:2:10
X = 1×11
-10 -8 -6 -4 -2 0 2 4 6 8 10
Y = -10:2:10
Y = 1×11
-10 -8 -6 -4 -2 0 2 4 6 8 10
Z = 1/2*(K1)*(sqrt(X.^2+(l1-Y(:)).^2)-l1).^2+1/2*(K2)*(sqrt(X.^2+(l2+Y(:)).^2)-l2).^2-P1*X-P2*Y(:)
Z = 11×11
711.1456 624.7473 561.5510 520.3138 500.0199 500.0000 480.0199 480.3138 501.5510 544.7473 611.1456 538.7188 457.7206 398.8612 360.1515 338.8541 328.0000 318.8541 320.1515 338.8612 377.7206 438.7188 394.8597 319.4738 264.8484 228.0377 205.3174 192.0000 185.3174 188.0377 204.8484 239.4738 294.8597 279.0089 210.0388 160.6235 127.0714 105.3836 92.0000 85.3836 87.0714 100.6235 130.0388 179.0089 190.2976 129.0865 86.6874 58.6284 40.2959 28.0000 20.2959 18.6284 26.6874 49.0865 90.2976 127.2078 75.4376 42.4287 22.6703 10.1765 0 -9.8235 -17.3297 -17.5713 -4.5624 27.2078 87.2951 46.6813 25.8359 17.9671 14.6478 8.0000 -5.3522 -22.0329 -34.1641 -33.3187 -12.7049 67.0012 38.7548 32.8620 41.5096 52.6142 52.0000 32.6142 1.5096 -27.1380 -41.2452 -32.9988 61.6940 45.5728 56.2317 86.5274 120.9840 132.0000 100.9840 46.5274 -3.7683 -34.4272 -38.3060 66.2443 59.3259 84.2989 137.8382 208.6181 248.0000 188.6181 97.8382 24.2989 -20.6741 -33.7557
contour(X,Y,Z,'ShowText','on')
xlabel('X')
ylabel('Y')