I want to study some "strange" functions by plotting them out in mathematica. One example is the following:
mod2[x_] := Which[Mod[x, 2] >= 1, -2 + Mod[x, 2], True, Mod[x, 2]];
f[x_] := Which[-1 <= x <= 1, Abs[x], True, Abs[mod2[x]]];
fn[x_, n_] := Sum[(3/4)^i*f[4^n*x], {i, 0, n}]
Plot[{fn[x, 0], fn[x, 1], fn[x, 2], fn[x, 5]}, {x, -2, 2}]
However, the plot I got from mma is misleading, in the sense that the maxima and minima of fn[x, 5] should be on the same two levels. But due to high oscillation of the function, and the fact that clearly mma only takes limited number of points to draw the function, you see the plot exhibit strange behavior. Is there any option in plot to remedy for this?
Many thanks.
You need to increase the setting for PlotPoints quite a bit to get a 'good' result.
Plot[Evaluate[
Reverse[{fn[x, 0], fn[x, 1], fn[x, 2], fn[x, 5]}]], {x, -2, 2},
PlotPoints -> 4000]
(I also reversed the order of the functions, so as to be able to see all the curves.)
Related
I am self-studying Introduction to algorithms, and am confused by the answer to 22.1-7.
Consider a directed graph with no self loop. I think it can be like picture3,
so the incidence matrix B should be
picture4
and the BT should be picture5
so The product of BBT should be picture6
i can understand the value in the diagonal means the the number of edge connect with the point
but in BBT[1][4] the value is 1 (0*1+(-1)*(-1)+(-1)*0)
i confused and don’t understand what wrong with it
To begin with, I think it's excellent that you actually tried to verify it with an example.
Unfortunately, you don't specify your edge numbers, so it's unclear what your incidence matrix refers to.
Suppose we use the following numbering:
Then the incidence matrix is
import numpy as np
b = np.array([[-1, -1, 0], [1, 0, 1], [0, 1, -1]])
>>> b
array([[-1, -1, 0],
[ 1, 0, 1],
[ 0, 1, -1]]
And the product is
>>> np.dot(b, b.T)
array([[ 2, -1, -1],
[-1, 2, -1],
[-1, -1, 2]])
which does not seem to be what you got, but actually makes a lot of sense.
I have a very large array of numbers in the form of a third order tensor.I want to find the highest of all the values in that tensor. How can I do it in mathematica? The context is that a reaction is carried out by varying temperature pressure and vessel volume. I want to find the optimum combination of the three to maximize the product. Each element of the tensor represents a value of the product produced corresponding to a specific combination of temperature pressure and volume.
Given some matrix, tensor, or basically any list-of-lists of real numbers, you can simply use the Max function to determine the maximum value and then Position to say where it is. Assuming your data isn't enormous (requiring some conservative/careful approach to save time/memory), this should be fine.
For example, here is a random list of lists of of lists of reals:
data = Table[RandomReal[],
{i, 1, RandomInteger[{4, 8}]},
{j, 1, RandomInteger[{4, 8}]},
{k, 1, RandomInteger[{4, 8}]}
];
You can just do:
m = Max[data]
Position[data, m]
This will tell you the position of the maximum value. If you did random integers instead, you could have ties, in which case you might have repeats:
data = Table[RandomInteger[{1, 10}],
{i, 1, RandomInteger[{4, 8}]},
{j, 1, RandomInteger[{4, 8}]},
{k, 1, RandomInteger[{4, 8}]}
];
m = Max[data]
Position[data, m]
Table[RandomInteger[100, 3], 3]
Prepend[Ordering[%[[First[Ordering[Reverse#*Sort /# %, -1]]]], -1],
First[Ordering[Reverse#*Sort /# %, -1]]]
% stands for the tensor to sort, in this case it's a random tensor generated from Table[RandomInteger[100, 3], 3]
This gives the position and value in one shot.
(m = RandomReal[{-1, 1}, {4, 3, 2}]) // MatrixForm
First#MaximalBy[
Flatten[MapIndexed[ {##} &, #, {-1}], ArrayDepth[#] - 1],
First] &#m
{0.903213, {3, 2, 2}}
Here is an alternate that will work with ragged lists:
Module[{h},
First#MaximalBy[List ### Flatten[MapIndexed[h### &, #, {-1}]],
First]] &#{{1, 2, 3}, {4, 5, {2, 3}}}
{5, {2, 2}}
I want to use the Manipulate function in Mathematica to fit an analytical function to a set of (x,y) data. I want to plot the dataset on the same axes that I use to manipulate the function (so I can get a visual check of how manipulating the parameters improves the fit, but I cannot find the correct syntax to draw the points behind the manipulated curve. Any solutions to this? Many thanks!
Show[plot1,plot2,...] will overlay the plots, see the docs on Show.
In[1]:= data = Table[{x, x^2+2*x+RandomReal[{-.1,.1}]}, {x,-3,3}];
Manipulate[
Show[ListPlot[data], Plot[a*x^2 + b*x + c, {x, -3, 3}]],
{{a, 0}, -4, 4}, {{b, 0}, -4, 4}, {{c, 2}, -4, 4}]
Out[1]= ...PlotSnipped...
I'm new to Mathematica and I'm trying to obtain a minimax rational function approximation to a certain expression. In particular, I'm using
mma = MiniMaxApproximation[x^2, {x, {8, 10}, 2, 2}]
Unfortunately, Mathematica 7 replies with the same expression I'm trying to calculate, namely
MiniMaxApproximation[x^2, {x, {8, 10}, 2, 2}]
Of course, I'm aware this is a very simple test, since x^2 is the rational function approximation of itself.
I'm also trying other possibilities like RationalInterpolation, EconomizedRationalApproximation etc., but none is working. Only PadeApproximant returns a result.
Anyone has an idea on why this happens?
Thank you very much in advance.
likely your first problem is that you havent loaded the function approximation package,
start a new kernel and try this:
Needs["FunctionApproximations`"]
mma = MiniMaxApproximation[Exp[x], {x, {0, 1}, 1, 2}]
Your specific example (x^2) throws a slew of (to me) nonsensical errors. I guess MiniMaxApproximation is not robust in handling trivial degenerate cases.
evidently requesting a numerator equal to the order for your expression is causing the error:
a[x_] = MiniMaxApproximation[x^2, {x, {8, 10}, 1, 2}][[2, 1]]
Plot[ {a[x] , x^2}, {x, -10, 30}]
Is it possible to overlay two or more graphics in Mathematica, if the graphics are generated by functions such as ReliefPlot or DensityPlot, using Opacity to control the appearance?
For example:
a = ReliefPlot[
Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}], ImageSize -> 100]
b = ReliefPlot[
Table[i + Sin[i^3 + j^3], {i, -4, 4, .03}, {j, -4, 4, .03}], ImageSize -> 100]
Show[a,b]
combines the two, but I can't work out how to insert an Opacity command anywhere here such that both are visible. The documentation states that these functions accept the same options as Graphics ("ReliefPlot has the same options as Graphics, with the following additions and changes:"), but I don't understand how to control the graphics... (And I may be confused about the difference between graphics options and directives, as well.)
Enlightenment - and less opacity - very welcome!
Edit: Wow, you guys are quicker than my version of Mathematica - thanks!
You'll have to issue the opacity directive to ColorFunction like so:
a = ReliefPlot[
Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}],
ImageSize -> 100]
b = ReliefPlot[
Table[i + Sin[i^3 + j^3], {i, -4, 4, .03}, {j, -4, 4, .03}],
ImageSize -> 100,
ColorFunction -> (Directive[Opacity[0.5],
ColorData["Rainbow"][#]] &)]
Show[a, b]
In general, in all *Plot* functions, you control opacity with either PlotStyle or ColorFunction, as the case may be. If this were just a Graphics primitive, you'd probably do something like Graphics[{Opacity[0.5], object}].
Since ReliefPlot doesn't have a PlotStyle option, you can use BaseStyle -> Opacity[0.5] to introduce transparency into the graphics.
An alternative is to work with Images and the ReliefImage function, and then compose the resulting images together using ImageCompose:
ImageCompose[
ReliefImage[Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}]],
{ReliefImage[Table[i + Sin[i^3 + j^3], {i, -4, 4, .03}, {j, -4, 4, .03}]],
0.5}
]
Since ReliefPlot also essentially returns pixel data in a Graphics-compatible format, perhaps Images will suit you better.
The default colour function of ReliefImage is different: you can use ColorFunction -> "LakeColors" to switch to ReliefPlot's one.
Originally I had a function here to extract the raster data from ReliefPlot, but then Brett Champion pointed to RasterImage in the comment below
The answers using transparency will work in a very general way, but in this particular example of combining two ReliefPlot[]s, you might want to consider plotting the sum of the two:
f[i_] := i + Sin[i^2 + j^2];
g[i_] := i + Sin[i^3 + j^3];
ReliefPlot[Table[f[i] + g[i], {i, -4, 4, .03}, {j, -4, 4, .03}], ImageSize -> 100]
Show[a, {Opacity[0.5],#}& /# b]