How to use reverse Scaling function with error bars in mathematica? - plot

I have data which I would like to plot along with the corresponding error bars:
{{{54927.7, -1.91044}, ErrorBar[38.2664, 0.0538982]},
{{55320.9, -1.97673}, ErrorBar[45.3592, 0.101486]},
{{55671.4, -2.15716}, ErrorBar[41.2234, 0.0258249]},
{{56032.9, -2.15957}, ErrorBar[38.8805, 0.0191277]},
{{56410.6, -2.14289}, ErrorBar[41.5501, 0.0189911]},
{{56787.2, -2.19703}, ErrorBar[38.1972, 0.00632055]},
{{57137.5, -2.1839}, ErrorBar[35.6098, 0.0084108]},
{{57493.3, -2.19994}, ErrorBar[38.0298, 0.00651633]},
{{57859.5, -2.19687}, ErrorBar[40.9682, 0.00658857]}}
I can use the "ErrorListPlot" function in *Mathematica just fine, however if I would like to reverse the y axis scale with the function "ScalingFunctions->"Reverse" " the error bars do not get plotted along with the data.....any suggestions on how to fix this?

Similar to this question. Some messing around to get the ticks looking right.
Needs["ErrorBarPlots`"]
data = {
{{54927.7, -1.91044}, ErrorBar[38.2664, 0.0538982]},
{{55320.9, -1.97673}, ErrorBar[45.3592, 0.101486]},
{{55671.4, -2.15716}, ErrorBar[41.2234, 0.0258249]},
{{56032.9, -2.15957}, ErrorBar[38.8805, 0.0191277]},
{{56410.6, -2.14289}, ErrorBar[41.5501, 0.0189911]},
{{56787.2, -2.19703}, ErrorBar[38.1972, 0.00632055]},
{{57137.5, -2.18390}, ErrorBar[35.6098, 0.0084108]},
{{57493.3, -2.19994}, ErrorBar[38.0298, 0.00651633]},
{{57859.5, -2.19687}, ErrorBar[40.9682, 0.00658857]}};
data[[All, 1, 2]] = -data[[All, 1, 2]];
ep = ErrorListPlot[data];
newTicks = AbsoluteOptions[ep, Ticks][[1, 2, 2]] /.
{x1_, x2_, x3_, x4_} :> If[x1 == x2,
{x1, NumberForm[-x2, {3, 2}], {0.014, 0}, x4},
{x1, x2, {0.007, 0}, x4}];
newTicks = newTicks /. {
GrayLevel[0.] -> GrayLevel[0.5],
AbsoluteThickness[0.25] -> AbsoluteThickness[0.18],
AbsoluteThickness[0.125] -> AbsoluteThickness[0.08]};
ErrorListPlot[data, Ticks -> {Automatic, newTicks}]

Needs["ErrorBarPlots`"]
data = {
{{54927.7, -1.91044}, ErrorBar[38.2664, 0.0538982]},
{{55320.9, -1.97673}, ErrorBar[45.3592, 0.101486]},
{{55671.4, -2.15716}, ErrorBar[41.2234, 0.0258249]},
{{56032.9, -2.15957}, ErrorBar[38.8805, 0.0191277]},
{{56410.6, -2.14289}, ErrorBar[41.5501, 0.0189911]},
{{56787.2, -2.19703}, ErrorBar[38.1972, 0.00632055]},
{{57137.5, -2.18390}, ErrorBar[35.6098, 0.0084108]},
{{57493.3, -2.19994}, ErrorBar[38.0298, 0.00651633]},
{{57859.5, -2.19687}, ErrorBar[40.9682, 0.00658857]}};
(* Invert the error bars, visible in InputForm *)
ep = InputForm[ErrorListPlot[data]] /.
{Line[{{a_, b_}, {c_, d_}}] :> Line[{{a, -b}, {c, -d}}],
Line[{Offset[e_, {f_, g_}], Offset[h_, {i_, j_}]}] :>
Line[{Offset[e, {f, -g}], Offset[h, {i, -j}]}]};
(* Discard the InputForm wrapper *)
ep2 = First[ep];
Show[ErrorListPlot[data, ScalingFunctions -> "Reverse"],
Delete[ep2, Most#First#Position[ep2, Point]]]

Related

Module caution: In 'Show' occurs where it is probably not going to be evaluated before going out of scope

I have tried to make my own Manipulate module that draws numerical trajectories in 2D space depending on 1 parameter.
The problem is that I have this caution in title for every variable in Show.
I understand that it is related to some dynamic functionality, but I still don't know how to get rid of it.
Also, it will be great if I can get rid of the local variable name (k$8245) in output of this module.
MyManipulatePlot2D[list_, opts : OptionsPattern[]] :=
Module[{mv, pr, rt, constPlots, k},
{rt, mv, pr} = Dimensions[list];
constPlots = Table[ListPlot[list[[i,;;, 1 ;; 2]], opts], {i, rt}];
Manipulate[
Show[constPlots[[k]]], {k, 1, rt, 1}]
]
P.S. I don't want to take Manipulate out of the Module, because in my code this function is way more complex and it actually draws trajectories depending on 2 parameters.
Right here if someone is interested
MyManipulatePlot2D[list_, opts : OptionsPattern[]] :=
Module[{rt, mv1, pr, constPlots, k1, mv2, i, k2, pointsPlot},
{rt, mv1, mv2, pr} = Dimensions[list];
constPlots =
Table[ListPlot[
ArrayReshape[list[[;; , i, ;; , ;;]], {rt*mv2, pr}][[;; ,
1 ;; 2]], PlotStyle -> ColorData[97, "ColorList"][[2]],
opts], {i, mv1}];
pointsPlot = list[[;; , ;; , ;; , 1 ;; 2]];
pointsPlot = ArrayReshape[pointsPlot, {rt, mv1, mv2, 1, 2}];
Manipulate[
Show[{constPlots[[k1]],
ListPlot[pointsPlot[[;; , k1, k2]],
PlotLegends ->
Placed[ToString[{list[[1, k1, k2, 3]], list[[1, k1, k2, 4]]}],
Top]]}], {k1, 1, mv1, 1}, {k2, 1, mv2, 1}]
]
I have tried experementing with Evaluate funtion but it didn't work out.
You can fix the k$8245 problem by using Block instead of Module, or you can use a label "k" as below. k$8245 is the module's local variable name so it is showing as expected, even though not what you want. No other problems observed.
MyManipulatePlot2D[list_, opts : OptionsPattern[]] :=
Module[{mv, pr, rt, constPlots, k}, {rt, mv, pr} = Dimensions[list];
constPlots = Table[ListPlot[list[[i, ;; , 1 ;; 2]], opts], {i, rt}];
Manipulate[Show[constPlots[[k]]], {{k, 1, "k"}, 1, rt, 1}]]
The second manipulate just errors disasterously.

Unable to plot solution of ODE in Maxima

Good time of the day!
Here is the code:
eq:'diff(x,t)=(exp(cos(t))-1)*x;
ode2(eq,x,t);
sol:ic1(%,t=1,x=-1);
/*---------------------*/
plot2d(
rhs(sol),
[t,-4*%pi, 4*%pi],
[y,-5,5],
[xtics,-4*%pi, 1*%pi, 4*%pi],
[ytics, false],
/*[yx_ratio , 0.6], */
[legend,"Solution."],
[xlabel, "t"], [ylabel, "x(t)"],
[style, [lines,1]],
[color, blue]
);
and here is the errors:
integrate: variable must not be a number; found: -12.56637061435917
What went wrong?
Thanks.
Here's a way to plot the solution sol which was found by ode2 and ic2 as you showed. First replace the integrate nouns with calls to quad_qags, a numerical quadrature function. I'll introduce a made-up variable name (a so-called gensym) to avoid confusion with the variable t.
(%i59) subst (nounify (integrate) =
lambda ([e, xx],
block ([u: gensym(string(xx))],
quad_qags (subst (xx = u, e), u, -4*%pi, xx)[1])),
rhs(sol));
(%o59) -%e^((-t)-quad_qags(%e^cos(t88373),t88373,-4*%pi,t,
epsrel = 1.0E-8,epsabs = 0.0,
limit = 200)[
1]
+quad_qags(%e^cos(t88336),t88336,-4*%pi,t,
epsrel = 1.0E-8,epsabs = 0.0,
limit = 200)[
1]+1)
Now I'll define a function foo1 with that result. I'll make a list of numerical values to see if it works right.
(%i60) foo1(t) := ''%;
(%o60) foo1(t):=-%e
^((-t)-quad_qags(%e^cos(t88373),t88373,-4*%pi,t,
epsrel = 1.0E-8,epsabs = 0.0,
limit = 200)[
1]
+quad_qags(%e^cos(t88336),t88336,-4*%pi,t,
epsrel = 1.0E-8,epsabs = 0.0,
limit = 200)[
1]+1)
(%i61) foo1(0.5);
(%o61) -1.648721270700128
(%i62) makelist (foo1(t), t, makelist (k, k, -10, 10));
(%o62) [-59874.14171519782,-22026.46579480672,
-8103.083927575384,-2980.957987041728,
-1096.633158428459,-403.4287934927351,
-148.4131591025766,-54.59815003314424,
-20.08553692318767,-7.38905609893065,-2.71828182845904,
-1.0,-0.3678794411714423,-0.1353352832366127,
-0.04978706836786394,-0.01831563888873418,
-0.006737946999085467,-0.002478752176666358,
-9.118819655545163E-4,-3.354626279025119E-4,
-1.234098040866796E-4]
Does %o62 look right to you? I'll assume it is okay. Next I'll define a function foo which calls foo1 defined before when the argument is a number, otherwise it just returns 0. This is a workaround for a bug in plot2d, which incorrectly determines that foo1 is not a function of t alone. Usually that workaround isn't needed, but it is needed in this case.
(%i63) foo(t) := if numberp(t) then foo1(t) else 0;
(%o63) foo(t):=if numberp(t) then foo1(t) else 0
Okay, now the function foo can be plotted!
(%i64) plot2d (foo, [t, -4*%pi, 4*%pi], [y, -5, 5]);
plot2d: some values were clipped.
(%o64) false
That takes about 30 seconds to plot -- calling quad_qags is relatively expensive.
it looks like ode2 does not know how to completely solve the problem, so the result contains an integral:
(%i6) display2d: false $
(%i7) eq:'diff(x,t)=(exp(cos(t))-1)*x;
(%o7) 'diff(x,t,1) = (%e^cos(t)-1)*x
(%i8) ode2(eq,x,t);
(%o8) x = %c*%e^('integrate(%e^cos(t),t)-t)
(%i9) sol:ic1(%,t=1,x=-1);
(%o9) x = -%e^((-%at('integrate(%e^cos(t),t),t = 1))
+'integrate(%e^cos(t),t)-t+1)
I tried it with contrib_ode also:
(%i12) load (contrib_ode);
(%o12) "/Users/dodier/tmp/maxima-code/share/contrib/diffequations/contrib_ode.mac"
(%i13) contrib_ode (eq, x, t);
(%o13) [x = %c*%e^('integrate(%e^cos(t),t)-t)]
So contrib_ode did not solve it completely either.
However the solution returned by ode2 (same for contrib_ode) appears to be a valid solution. I'll post a separate answer describing how to evaluate it numerically for plotting.

Return the index for a list from a list in Erlang

I've been practicing using recursion to define the index in Erlang. Here I need to implement a function to return the index for a list from a list.
eg.
([2, 4, 4], [1, 1, 2, 4, 4, 3, 4 ]) ----> 2
([1, 3], [5, 2, 2, 3, 1, 3, 5]) ----> 4
([1], [3, 2, a, {1, 1}, 1] ----> 4
Here is my code:
-module(project).
-export([index/2]).
index([X|XS],[_]) -> index([X|XS],[_],1).
index(_,[],_) -> [];
index([X|XS],[X|_], ACC) -> ACC;
index([X|XS],[_|rest],ACC) ->index([X|XS],rest,ACC+1).
I modified and coded logically but it still can not being compiled. I hope someone who can help me with it. Thanks!
Just for fun, here is an implementation that is not written a very clean way, but illustrates the techniques I think you are looking for. Note there are two basic states: "checking" and "matching".
-module(sublistmatch).
-export([check/2]).
check(Segment, List) ->
SegLen = length(Segment),
ListLen = length(List),
Index = 1,
check(Segment, List, SegLen, ListLen, Index).
check(S, S, _, _, I) ->
{ok, I};
check(_, _, SL, LL, _) when SL >= LL ->
nomatch;
check(S = [H|Ss], [H|Ls], SL, LL, I) ->
case matches(Ss, Ls) of
true -> {ok, I};
false -> check(S, Ls, SL, LL - 1, I + 1)
end;
check(S, [_|L], SL, LL, I) ->
check(S, L, SL, LL - 1, I + 1).
matches([H|S], [H|L]) -> matches(S, L);
matches([], _) -> true;
matches(_, _) -> false.
Note that this depends on knowing the lengths of both the segment you are checking for, and the current length of the remaining list to check. Consider why this is necessary. Also consider how using the utility function matches/2 gives us a natural place to explore whether an option matches, and backtracks if it does not.
In real programs you would use the standard library functions such as lists:prefix/2, lists:suffix/2, or sets:is_subset/2, or maybe some key or member operation over a gb_tree, dict, map or array depending on the situation.
To Compile the code you have to change it to:
-module(project).
-export([index/2]).
%%index([X|XS],[_]) -> index([X|XS],[_],1).
index([X|XS],List) -> index([X|XS],List,1).
%% you shuld not pass '_' as parameter it's will be marked as unbound
index(_,[],_) -> [];
index([X|XS],[X|_], ACC) -> ACC;
%%index([X|XS],[_|rest],ACC) ->index([X|XS],rest,ACC+1).
index([X|XS],[_|Rest],ACC) ->index([X|XS],Rest,ACC+1).
%% rest is an atom, it's not the case you need to use here.
%%Variables should start with upper case letter.
This code will compiled but wrong results as some cases.

Using Mathematica Manipulate function to plot a transfer function

It's my first time asking for help here, I hope someone respond. I was hoping to post images to show the problem I had but I need at least 10 reps to do it. But I hope everyone understand what I'm asking for.
I'm trying to create a manipulate box to plot a transfer function with type in boxes so that I can type in the the transfer function and specify the x and y axis. But the plot itself is not appearing only the axis are
but if i type the code outside of "manipulate" it works.
If you try running this on Mathematica you could probably see the problem I'm having.
My mathematica code is below.
Manipulate[tfplot,
{{tfplot1, 0, "Transfer Function="}},
Delimiter,
{{fmin1, 10, "fmin = "}},
{{fmax1, 10^7, "fmax = "}},
{{ymin1, 1, "ymin = "}},
{{ymax1, 2*10^2, "ymax = "}},
Delimiter,
Row[{
Button["Plot", tfplot = LogLogPlot[Abs[tfplot2[2*Pi*I*f] /. {tfplot2[s_] -> tfplot1}], {f, fmin1, fmax1}, PlotPoints -> 1000, PlotRange -> {{fmin1, fmax1}, {ymin1, ymax1}}, PlotLabel -> "tf Plot"], ImageSize -> 80]
}]
, ControlPlacement -> {Left, Left, Left, Left, Left, Left, Left, Top}]
tfplot3 = (3.333321894500285`*^6 (4.611679331492357`*^6 - 72057.48955456808` s - 4.138291871540356`*^9 s^3 - 3.889993968666704`*^9 s^4 + s^5))/(s^2 (2.606152799059127`*^18 + 4.6278171788297256`*^16 s + 1.0779994813998577`*^14 s^2 + 1.5235290577558628`*^8 s^3 + s^4))
LogLogPlot[Abs[tfplot4[2*Pi*I*f] /. {tfplot4[s_] -> tfplot3}], {f, 10, 10^7}, PlotPoints -> 1000, PlotRange -> {{10, 10^7}, {1, 2*10^2}}, PlotLabel -> "tf Plot"]
Thank you.
Spiderfiq
Edit .. take 2..
Manipulate[
fplot = LogLogPlot[Abs[tfplotf /. s -> 2*Pi*I*f], {f, fmin1, fmax1},
PlotPoints -> 1000, PlotRange -> {{fmin1, fmax1}, {ymin1, ymax1}},
PlotLabel -> "tf Plot"],
{{tfplotf, (3.333321894500285`*^6 (4.611679331492357`*^6 -
72057.48955456808` s - 4.138291871540356`*^9 s^3 -
3.889993968666704`*^9 s^4 +
s^5))/(s^2 (2.606152799059127`*^18 +
4.6278171788297256`*^16 s + 1.0779994813998577`*^14 s^2 +
1.5235290577558628`*^8 s^3 + s^4))
, "Transfer Function="}},
Delimiter,
{{fmin1, 10, "fmin = "}},
{{fmax1, 10^7, "fmax = "}},
{{ymin1, 1, "ymin = "}},
{{ymax1, 2*10^2, "ymax = "}},
Delimiter,
ControlPlacement -> {Left, Left, Left, Left, Left, Left, Left, Top}]
This is some old code I had lying around from my System Dynamics and Controls class.
Manipulate[tf = TransferFunctionModel[eq, s];
BodePlot[tf, GridLines -> Automatic, ImageSize -> 500,
FrameLabel -> {{{"magnitude (db)", None}, {None,
"Bode plot"}}, {{"phase(deg)", None}, {"Frequency (rad/sec)",
None}}},
ScalingFunctions -> {{"Log10", "dB"}, {"Log10", "Degree"}},
PlotRange -> {{{0.1, 100}, Automatic}, {{0.1, 100},
Automatic}}], {eq, (5 s)/(s^2 + 4 s + 25)}]
-Brian

Mathematica - Plotting the output of a function created directly from solve

I've got some fairly long matrix algebra that I am trying to plot the outcome of. Nothing seems to show up on the axes, and I can't quite tell where the problem is. I successfully created a function from the output of solve using the tips here:
How to create a function directly from the output of Solve
But it just won't plot!
Here (briefly) is the code:
eqn = m.{1, r} == {t, 0}
sols1 = Solve[eqn, {t, r}]
m is a complex matrix
Here is the output:
{{t -> -((-1. cos[9.62458 s]^2 - (1. + 0. I) sin[9.62458 s]^2)/(
cos[9.62458 s] - (0. + 2.4087 I) sin[9.62458 s])),
r -> ((0. + 2.1913 I) sin[9.62458 s])/(
cos[9.62458 s] - (0. + 2.4087 I) sin[9.62458 s])}}
So far so good (except that Mathematica seems to have trouble with the whole cos^2 + sin^2 = 1 thing).
Then I try to plot the real part of t as a function of s:
Plot[Re[t /. sols1], {s, 0, 0.4}]
And I just get empty axes.
I try assigning the output to a function and plotting that way
f[s_] = t /. sols1[[1, 1]]
Plot[Re[f[s]], {s, 0, 0.4}]
And I still get an empty axis. I transcribed the function in Matlab where it plots just fine, so I know the solution is sound. I have to solve this for several matrices m which just get hairier and hairier, so transcribing to Matlab is not ideal. I want to plot right in Mathematica.
Any ideas?
Try using mathematica syntax. I.e. Sin in place of sin; same for cos:
In[1]:= sols1={
{t->-((-1. cos[9.62458 s]^2-(1.+0. I) sin[9.62458 s]^2)/(cos[9.62458 s]-(0.+2.4087 I) sin[9.62458 s]))
,r->((0.+2.1913 I) sin[9.62458 s])/(cos[9.62458 s]-(0.+2.4087 I) sin[9.62458 s])}
}/.{cos->Cos,sin->Sin}//FullSimplify//Chop
Out[1]= {{t->1./(Cos[9.62458 s]-(0. +2.4087 I) Sin[9.62458 s]),r->1/(-1.09921-(0. +0.45635 I) Cot[9.62458 s])}}
then e.g.
GraphicsColumn[Plot[t /. sols1 // #, {s, 0, .4}, PlotLabel -> #[t], Frame -> True] & /# {Re, Im, Abs[#]^2 &}]
should work well.

Resources