0
$\begingroup$

I have 2 problems with which I would need help: -first one is that I would like to plot a discontinuous function of 2 variables but can't succeed (see attachment) -then I also would like to display a legend with a contour plot on which there are several ellipses but can't figure out (also see attachment). I don't want the usual contourplot legend with color graduation but one like : blue->ellipse one red->ellipse two

    s1t = 90
    s2t = 40
    s1c = -130
    s2c = -60
    f[x_, y_] := (x/s1t)^2 + (y/s2t)^2 - x*y/(s1t)^2 /; x > 0 && y > 0 
    f[x_, y_] := (x/s1t)^2 + (y/Abs[s2c])^2 - x*y/(Abs[s1c])^2 /; 
x > 0 && y < 0
    f[x_, y_] := (x/Abs[s1c])^2 + (y/s2t)^2 - x*y/(s1t)^2 /; 
x < 0 && y > 0
    f[x_, y_] := (x/Abs[s1c])^2 + (y/Abs[s2c])^2 - x*y/(Abs[s1c])^2 /; 
x < 0 && y < 0
    ContourPlot[f == 1, {x, -30 Pi, 30 Pi}, {y, -45 Pi, 20 Pi}, 
Axes -> {True, True}]
    ContourPlot[{(x/s1t)^2 - x*y/(s1t*s2t) + (y/s2t)^2 == 
1, (x/s1t)^2 + (y/s2t)^2 + x*y*(1/(s1t^2) - 1/(s2t^2)) == 1, 
x^2/(s1t^2) + y^2/(s2t^2) - x*y/(s1t^2) == 1, 
x^2/(s1t*Abs[s1c]) + 
y^2/(s2t*Abs[s2c]) + (1/s1t - 1/Abs[s1c])*
 x + (1/s2t - 1/Abs[s2c])*y + 
2*x*y*1/(2*s45^2)*(1 - (1/s1t - 1/Abs[s1c] + 1/s2t - 1/Abs[s2c])*
    s45 - (1/(s1t*Abs[s1c]) + 1/(s2t*Abs[s2c]))*s45^2) == 1, 
x^2/Abs[(s1t*s1c)] +  y^2/Abs[(s2t*s2c)] + (1/s1t - 1/Abs[s1c])*
 x + (1/s2t - 1/Abs[s2c])*y - 1/(2*Sqrt[s1t*s1c*s2t*s2c]) == 1, 
x^2/Abs[(s1t*s1c)] + 
y^2/Abs[(s2t*s2c)] + (1/s1t - 1/Abs[s1c])*
 x + (1/s2t - 1/Abs[s2c])*y + 1/Sqrt[s1t*s1c*s2t*s2c] - 
1/(2*S^2) == 1, (x/s1t)^2 + (y/s2t)^2 == 
1, (x/s1t)^2 + (y/s2t)^2 - x*y/(s1t*s2t) == 
1, (x*y - x^2)/(s1t*s1c) - (y^2/(s2t*s2c)) + 
x*(s1t + s1c)/(s1t*s1c) + y*(s2c + s2t)/(s2t*s2c) == 
1, (1.95*x*y - x^2)/(s1t*s1c) - (y^2/(s2t*s2c)) + 
x*(s1c + s1t)/(s1t*s1c) + y*(s2c + s2t)/(s2t*s2c) == 
1}, {x, -165 Pi, 70 Pi}, {y, -75 Pi, 30 Pi}, Axes -> {True, True}, 
FrameLabel -> {sigma1, sigma2}, PlotLabel -> failure surface, 
AspectRatio -> Automatic]
$\endgroup$
3
  • $\begingroup$ ContourPlot[f[x,y] == 1, {x, -30 Pi, 30 Pi}, {y, -45 Pi, 20 Pi}, Axes -> {True, True}] ? $\endgroup$
    – cormullion
    Commented Feb 26, 2013 at 10:10
  • $\begingroup$ Your second ContourPlot is incomplete: what is s45? $\endgroup$
    – Verbeia
    Commented Feb 26, 2013 at 10:19
  • $\begingroup$ Fracture mechanics? $\endgroup$
    – Yves Klett
    Commented Feb 26, 2013 at 10:27

1 Answer 1

2
$\begingroup$

Two issues here: first, as cormullion pointed out in comments, you need the argument of the first ContourPlot to be f[x,y]==1, not just f==1. The reason is that the x and y in your function defition for f are just placeholders, that can substitute for whatever arguments are passed. Mathematica doesn't assume that the x you used in the function definition is the same as the x used in a plot range. So:

ContourPlot[f[x, y] == 1, {x, -30 Pi, 30 Pi}, {y, -45 Pi, 20 Pi}, 
 Axes -> {True, True}]

enter image description here

I'm not sure what you mean by it being discontinuous. It doesn't look discontinuous either in a Plot3D or in a Plot holding one variable constant. In fact if you look at the function, at the point that it switches definition, several terms converge to zero no matter which function is being used. So it is actually effectively continuous even if it it isn't continuously differentiable. (It would have helped to remove the Abs[s1c] complication for s1c and s2c and simply used positive variables instead. They don't appear outside Abs expressions.)

For your second plot, you need the ContourStyle option to style the contours, unsurprisingly enough. You also have several undefined variables, including s45, which I've set to be 50 in the following. There is also a capital S. Please be aware that it is not good practice to name variables with single capital letters as it might clash with a built-in function.

Having defined s45, the following works:

ContourPlot[{(x/s1t)^2 - x*y/(s1t*s2t) + (y/s2t)^2 == 
   1, (x/s1t)^2 + (y/s2t)^2 + x*y*(1/(s1t^2) - 1/(s2t^2)) == 1, 
  x^2/(s1t^2) + y^2/(s2t^2) - x*y/(s1t^2) == 1, 
  x^2/(s1t*Abs[s1c]) + 
    y^2/(s2t*Abs[s2c]) + (1/s1t - 1/Abs[s1c])*
     x + (1/s2t - 1/Abs[s2c])*y + 
    2*x*y*1/(2*s45^2)*(1 - (1/s1t - 1/Abs[s1c] + 1/s2t - 1/Abs[s2c])*
        s45 - (1/(s1t*Abs[s1c]) + 1/(s2t*Abs[s2c]))*s45^2) == 1, 
  x^2/Abs[(s1t*s1c)] + 
    y^2/Abs[(s2t*s2c)] + (1/s1t - 1/Abs[s1c])*
     x + (1/s2t - 1/Abs[s2c])*y - 1/(2*Sqrt[s1t*s1c*s2t*s2c]) == 1, 
  x^2/Abs[(s1t*s1c)] + 
    y^2/Abs[(s2t*s2c)] + (1/s1t - 1/Abs[s1c])*
     x + (1/s2t - 1/Abs[s2c])*y + 1/Sqrt[s1t*s1c*s2t*s2c] - 
    1/(2*S^2) == 1, (x/s1t)^2 + (y/s2t)^2 == 
   1, (x/s1t)^2 + (y/s2t)^2 - x*y/(s1t*s2t) == 
   1, (x*y - x^2)/(s1t*s1c) - (y^2/(s2t*s2c)) + 
    x*(s1t + s1c)/(s1t*s1c) + y*(s2c + s2t)/(s2t*s2c) == 
   1, (1.95*x*y - x^2)/(s1t*s1c) - (y^2/(s2t*s2c)) + 
    x*(s1c + s1t)/(s1t*s1c) + y*(s2c + s2t)/(s2t*s2c) == 
   1}, {x, -165 Pi, 70 Pi}, {y, -75 Pi, 30 Pi}, Axes -> {True, True}, 
 FrameLabel -> {sigma1, sigma2}, PlotLabel -> "failure surface", 
 AspectRatio -> Automatic, ContourStyle -> {Red, Blue}]

enter image description here

If you additionally provide a value for S (I used $2$ for the purposes of the next plot), you'll get more contours.

enter image description here

Adding a legend, if that's really what you meant, is quite straightforward in version 9: add the following option to the end of the contour plot and you get the following:

PlotLegends -> Placed[LineLegend[Automatic, 
 {"something", "something else"}], Bottom]

enter image description here

As a side note, the PlotLabel should be a string in quote marks. You only got away with it in this case because multiplying failure and surface results in failure surface in canonical (alphabetical) order.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.