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

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...