Skip to main content

plotting - How to plot 2D plot against two functions



I need to Plot two functions one is on x-axis and second on y-axis, both function have common variable $x$. The two functions are


myfunction1[{al_, be_, ga_}] := (al*be)/2 Cos[x]
(Sec[x/2])^2 (Tan[x/2])^-(be + 1) (1 + ga (Tan[x/2])^-be)^(-(al/ga) - 1)
myfunction2[{al_, be_, ga_}] := (al*be)/2 Sin[x] (Sec[x/2])^2 (Tan[x/2])^-(be + 1) (1 +
ga (Tan[x/2])^-be)^(-(al/ga) - 1)


where $0Plot function or there is any other function in Mathematica to plot this?



Answer



The main difficulty of most beginners with Mathematica is that they start with a somewhat complicated problem (displaying two functions that have three parameters in one graphic) and then trying to do it all at once. First you have to understand how various Plot types work, such as Plot and ParametricPlot, and probably how to use graphics Options to control the look of the graphic. Then there is the question of whether the graphs of each of the functions can be reasonably combined in a single plot. And why do you say you want to plot one function on the x axis and the other on the y axis? That seems quixotic because maybe the range of the first function is discordant with the domain of the second function, and how would that make a good way to compare the two functions? Perhaps that was an error and you meant plotting both against the x axis with different scales on the left and right y axes? Plotting data, or functions, depends an awful lot on what the data or functions actually look like. One can't just go ahead with a generic approach without looking at the functions because all kinds of problems can arise depending on the parameters.


So let's just explore some first. Here is a somewhat different method for defining your first function.


myfunction1[{al_, be_, ga_}][
x_] := (al*be)/2 Cos[
x] (Sec[x/2])^2 (Tan[x/2])^-(be + 1) (1 +
ga (Tan[x/2])^-be)^(-(al/ga) - 1)


I've added an extra set of square brackets to contain the parameters and the final set of [] brackets contains the variable. The first set is known as SubValues in Mathematica. We wouldn't have to put the parameters in a List, but I went along with that. This type of definition has certain advantages: besides separating parameters from variables, it makes it easy to calculate derivatives with respect to the variable. For example:


myfunction1[{a, 1, 1}]'[x]

giving


-(1/2) a Cos[x] Cot[x/2] (1 + Cot[x/2])^(-1 - a) Csc[x/2]^2 - 
1/4 (-1 - a) a Cos[x] (1 + Cot[x/2])^(-2 - a) Csc[x/2]^4 -
1/2 a (1 + Cot[x/2])^(-1 - a) Csc[x/2]^2 Sin[x]

Now let's plot with the parameters {1,1,1}.


Plot[myfunction1[{1, 1, 1}][x], {x, 0, \[Pi]},

Frame -> True,
ImageSize -> 250]

enter image description here


Notice that I used a Frame instead of the default Axes plot. This was done with the Frame option. This is almost always better. If you do a lot of graphics you will want to get to know the options. Next you might want to explore the function in the parameter space. Here is a little custom dynamic module for doing it. (Most users use Manipulate but it can sometimes tie you in knots and it's often easier to construct a custom dynamic.)


DynamicModule[{a = 1, b = 1, c = 1},
Column[{
Row[{"a", Spacer[10],
Slider[Dynamic[a], {0, 5}, Appearance -> "Labeled"]}],
Row[{"b", Spacer[10],

Slider[Dynamic[b], {0, 5}, Appearance -> "Labeled"]}],
Row[{"c", Spacer[10],
Slider[Dynamic[c], {0, 5}, Appearance -> "Labeled"]}],
Dynamic@
Plot[myfunction1[{a, b, c}][x], {x, 0, \[Pi]},
Frame -> True,
ImageSize -> 250]
}]
]


I won't display it here, but it you start exploring with it you will find a serious problem. The scale on the vertical axis keeps changing! This is because without further guidance Mathematica determines what it thinks is a reasonable plot range, and it does this each time you move one of the sliders. Any dynamic plot requires a fixed background (you would be astonished at how often beginners make that mistake.) So take the control away from Mathematica and specify a specific fixed plot range. The following does that.


DynamicModule[{a = 1, b = 1, c = 1},
Column[{
Row[{"a", Spacer[10],
Slider[Dynamic[a], {0, 5}, Appearance -> "Labeled"]}],
Row[{"b", Spacer[10],
Slider[Dynamic[b], {0, 5}, Appearance -> "Labeled"]}],
Row[{"c", Spacer[10],
Slider[Dynamic[c], {0, 5}, Appearance -> "Labeled"]}],
Dynamic@

Plot[myfunction1[{a, b, c}][x], {x, 0, \[Pi]},
Frame -> True,
PlotRange -> {-1, 2},
ImageSize -> 250]
}]
]

Now define your second function and explore it.


myfunction2[{al_, be_, ga_}][
x_] := (al*be)/2 Sin[

x] (Sec[x/2])^2 (Tan[x/2])^-(be + 1) (1 +
ga (Tan[x/2])^-be)^(-(al/ga) - 1)

I'll leave the exploration to you. Just follow the example above.


Unless you have some really good reason, I would plot both functions on the x axis. As an example:


Plot[{myfunction1[{1, 1, 1}][x], myfunction2[{1, 1, 1}][x]}, {x, 
0, \[Pi]},
Frame -> True,
ImageSize -> 250]


enter image description here


Now you could use that plot in a DynamicModule. Again you will have to specify a PlotRange as before. You might consider scaling one or the other of the two functions and you might include this scaling information in the FrameLabel. There are other techniques, but I'm getting tired and I hope that helps enough. Remember that the method of presentation depends critically on what the data, functions or phenomenon actually is!


If you really want the functions on the x and y axes then you have to say what the purpose is so that it might be done in an intelligent manner.


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.