Shade Bounded Region along y-axis - maple

I plotted a line but want to shade the area between 2 and 4 along the y axis to illustrate area under the curve but cannot figure out how to do that, can anyone help? This is the code, which is rather simple
>y:=(2*x);
>plot(y,x=0..3);

I find it difficult to understand just what region you have in mind.
Is it this?
restart;
y := 2*x:
plots:-display(
plot(2*x, x=0..3),
plots:-inequal([Y>=y, Y>=2, Y<=4], x=0..3, Y=0..6,
'nolines', 'color'="Burgundy")
);
Of course you could omit the curve (line) y=2*x by removing the call to plot above.
If you have some other region in mind then you should be able to adjust the call to plots:-inequal accordingly.
There are other ways to accomplish such things, such as calling plot with its filled option. You can also use plottools:-reflect, or use the parametric calling sequence of plot, to flip x and y axes.
I figure that you might be wanting to avoid having to "solve for x", to get x values corresponding to y=2 and y=4 (even though in this example of y=2*x you can do that in your head).
These are reasons why I think you'd likley find it easiest to just use plots:-inequal.
[edit: for followup comment about 8 rectangles]
First, a slightly different example, hopefully for more clarity.
restart;
x:=arcsin(y/6):
P := plots:-display(
plot(x, y=2..5),
plots:-inequal([X<=x], y=2..5, X=0..1.2,
'nolines', 'color'=pink)
):
plots:-display(
plot(2, color=black, linestyle=dot),
plot(5, color=black, linestyle=dot),
plot([x, y, y=0..6]),
plottools:-transform((x,y)->[y,x])(P),
view=[0..1.2,0..6],
labels=["x","y"],
size=[500,300]
);
The upper of lower sums (using rectangles) can be visualized using the RiemannSum command from the Student:-Calculus1 package. (Or you could use the seq command and construct them all by formulas for their corners -- but that just seems like a lot of awkward bookkeeping.)
You can of course remove any of the parts passed below to the plots:-display command.
restart;
with(Student:-Calculus1):
x:=arcsin(y/6):
P:=RiemannSum(x, y=2..5, method = upper, output = plot,
partition=8,
boxoptions=[filled=[color=pink,transparency=.5]],
caption=""):
rP:=plottools:-transform((x,y)->[y,x])(P):
plots:-display(
plot(2, color=black, linestyle=dot),
plot(5, color=black, linestyle=dot),
plot([x, y, y=0..6]),
rP,
view=[0..1.2,0..6],
labels=["x","y"],
size=[500,300]
);
Or,
restart;
with(Student:-Calculus1):
x:=arcsin(y/6):
P:=RiemannSum(x, y=2..5, method = lower, output = plot,
partition=8,
boxoptions=[filled=[color=pink,transparency=.5]],
caption=""):
rP:=plottools:-transform((x,y)->[y,x])(P):
plots:-display(
plot(2, color=black, linestyle=dot),
plot(5, color=black, linestyle=dot),
plot([x, y, y=0..6]),
rP,
view=[0..1.2,0..6],
labels=["x","y"],
size=[500,300]
);
All these examples would be a little less complicated if you wanted the area between the curve and the x-axis instead.

Related

Colormap and colorbar for 3d trajectory. (Matlab) [duplicate]

This question already has answers here:
Change color of 2D plot line depending on 3rd value
(5 answers)
Closed 6 months ago.
I'm looking to do something similar to the below image (source) in Matlab. Ignore the vectors and the shaded cone; I just want to
plot a trajectory on the unit sphere, given by a function m(x) (whose values are points on the sphere),
color it according to parameter x (running from -inf to +inf),
display a color bar which explains the mapping from x-value to color.
I can plot the sphere easily with the help of built-in functions sphere and mesh, and I can draw the trajectory with plot3. To generate the x-values I can use something like
x = tan((-1:0.01:1)*pi/2);
which gives a distribution of points fitting for the types of functions m(x) will be (most of the action will be around x = 0).
The tricky part is the color. I have looked at the colormap and colorbar built-ins, but both seem to pertain to meshes and surfaces rather than curves, so I don't see an obvious way to adapt it to my use case.
Does anyone have any ideas for how I might proceed?
When using plot it is not possible for one line to be multicoloured. In order to make one line multicoloured, you must plot each individual segment and then colour them individually. For example:
% make some dummy data
% note that this is in the form of 1 x N row vectors
t = -.99:0.01:0.99;
x = cos(t * pi/2);
y = sin(t * pi/2);
z = t;
% reorder data in order to plot individual line segments
indices = [1:length(x) - 1; 2:length(x)];
figure;
p = plot3(x(indices), y(indices), z(indices), 'LineWidth', 2);
set(p, {'Color'}, num2cell(parula(length(x) - 1), 2)); % see what happens when you comment out this line
You can change the colormap you want by changing the parula keyword above. You can use any of the supplied colormaps, or create your own (in the form of an M x 3 RGB triple matrix).
You can then add the colorbar (note that these are dummy values).
c = colorbar;
caxis([-6 6]);
set(c, 'Ticks', [-6, -4, -2, -1, 0, 1, 2, 4, 6]);
set(c, 'TickLabels', {'-\infty', '-4', '-2', '-1', '0', '1', '2', '4', '\infty'});
You can achieve a somewhat similar, but different, effect using scatter3(). It is similar to plot3() but will allow you to set color (and size, if you wish) for each data point that it plots. If the aesthetics suit you, it is a simple and versatile approach.

Slice contour graphic at given level

I have the following function (z) that should output a graphic which if sliced at f(x,y) = 0.001 the result image should be a message.
I wrote this code but I`m unable to slice it right
[x,y] = meshgrid(-1.5:0.3:1.5,-2.5:0.5:2.5) ;
z=exp(-4*x.^2-2*y.^2)*cos(8*x)+exp(-3*((2*x+1)/2).^2-6*y.^2);
% meshc (x,y,z, [0.001 0.001]);
meshc (x,y,z);
What did i miss?
You are likely looking for the countour function instead of meshc. meshc plots a contour plot under a mesh plot, but you don't need a mesh plot to view the message. In fact, the countour docs show an example of how to only plot a specific level:
contour(x, y, z, [0.001 0.001])
I also suspect that your function is not defined correctly. exp(...) * cos(...) should probably read exp(...) .* cos(...):
The poor granularity leads me to believe that the sample spacing should be decreased (i.e., the grid should be made finer):
[x,y] = meshgrid(-1.5:0.003:1.5,-2.5:0.005:2.5);
Contouring the original function with the finer spacing also shows why I think * should probably be .* in the expression:
[x,y] = meshgrid(-1.5:0.003:1.5,-2.5:0.005:2.5);
z=exp(-4*x.^2-2*y.^2)*cos(8*x)+exp(-3*((2*x+1)/2).^2-6*y.^2);
contour(x, y, z, [0.001 0.001])
Unless the message is a count of sausage shapes, I think that the .* version is more likely to contain useful information (looks like HI).
You'll want to use contour rather than meshc to plot the intersection of your mesh with 0.001 so that you can specify exactly at what value you'd like to compute the contour.
contour(x, y, z, [0.001 0.001]);
If you do want to use meshc, you can use the output which contains a handle to the contour plot to set the LevelList property to your desired value
h = meshc(x, y, z);
set(findobj(h, 'type', 'contour'), 'LevelList', [0.001 0.001])
As far as getting an intelligible message out, I guess intelligent is in the eye of the beholder

How to interpolate and plot a 4-Dimensional hamburger?

I have solved the heat equation in octave via finite difference and produced the following 3-D plot whose point colors correspond to the temperatures in each element of my three dimensional hamburger.
My computational resources limit the resolution at which I may solve my burger. Thus the only way to get the plot I want is to make my scatter3 points huge blobs of color and it looks kind of bad.
[x,y,z] = meshgrid(1:nx,1:ny,1:nz) % Defines a grid to plot on
scatter3(x(:), y(:), z(:), 40, burgermatrix(:), 's', 'filled')% Point color=value
What I want is a nice gorgeous smooth rectangular prism like this:
So I figure I need to somehow interpolate between the 3D points that I have. Can anyone help me figure out how to do this?
I might be missing something obvious, but here's the example from octave's help slice:
[x, y, z] = meshgrid (linspace (-8, 8, 32));
v = sin (sqrt (x.^2 + y.^2 + z.^2)) ./ (sqrt (x.^2 + y.^2 + z.^2));
slice (x, y, z, v, [], 0, []);
[xi, yi] = meshgrid (linspace (-7, 7));
zi = xi + yi;
slice (x, y, z, v, xi, yi, zi);
shading interp; %addition by me
Isn't this exactly what you need? You have your grid (x,y,z), your solutions (T), so you just need to plot it slicing along [0 0 1] etc. Something like
[xi yi]=meshgrid(unique(x),unique(y));
slice (x, y, z, T, xi, yi, max(z(:))*ones(size(xi)));
and the same for cuts along the two other axes. (Obviously the unique calls should be substituted with the vectors you already have from which you constructed the 3d mesh in the first place.)
NOTE: By the way, you should really consider changing the default (jet) colormap. I was yesterday enlightened by a colleague about the viridis colormap made by the SciPy people, see for instance this post and video link therein. Their reasoning is overwhelming, and their colormap is beautiful. This should define it: viridis, although I haven't tried it myself yet.
(If it wasn't for jet, I'd tell you that your temperature profile seems strongly 1d. Do you happen to have periodic boundary conditions along the vertical walls and homogeneous (i.e. constant) boundary conditions along the horizontal ones?)

How to make a graph tangent to tops of another graph?

I've got a graph: the velocity as a function of the rpm of a car. The actual graph consists of four subgraphs (one for each of the 4 gears of the car). I conjoined those sub-graphs with a for loop and some if statements. This resulting graph is shown below.
I need to add a graph which is tangent to all the tops of the graphs, like the red line. The final result would look like this:
(It's now still without the red line, obviously...)
These are the coordinates where the tangent line would touch the graph: (17, 5130.36), (28, 3117.98), (39, 2239.37), (51, 1714.72).
Since you already know the tangent points,
x = [17 28 39 51];
y = [5130.36 3117.98 2239.37 1714.72];
plot(x, y, 'b.')
the solution is a simple interpolation
xs = linspace(min(x), max(x), 100);
ys = spline(x, y, xs);
hold on
plot(xs, ys, 'r')
with the result:
I here used Matlab's standard cubic spline interpolation; depending on the properties of your data, other interpolation functions might give better results. In particular, if you knew not only the positions of the tangent points but also the tangent slope, you could use that information to impose constraints on a piecewise polynomial interpolation.

A Matlab surf like plot with a colormap in mathematica?

I would like to ask the community for help in creating a Matlab-like 3D surf plot with a colormap but in Mathematica. To make the question more specific lets say that we have a set of numbers in 3D space and to each point some function value c is asigned:
{{x1,y1,z1,c1},{x2,y2,z2,c2},...,{xN,yN,zN,cN}}
These points lay on a surface in 3D e.g. a sphere. Now in matlab if these points are properly represented as matrixes X, Y, Z, C we can simpliy plot this surface with
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = magic(2^k);
surf(x,y,z,c);
colormap gray
axis equal
What is important here are the function values which can be read of the plot thanks to the colorbar.
Now the question to the community is:
Can we produce such a plot in Mathematica (with a colorbar - this is the important part)???
I searched for a solution on the web without luck. I looked at things like ListPlot3D, ListSurfacePlot3D, ListPointPlot3D but to these functions you can only pass a surface without the corresponding point values (unless there is something I don't understand).
Does anyone know a solution, a way to create such plots in Wolfram Mathematica?
You can use ColorFunction to specify, well, the color function :
ParametricPlot3D[{Sin[t] Cos[f], Sin[t] Sin[f], Cos[t]}, {t, 0, Pi}, {f, 0, 2 Pi} ,
ColorFunction -> Function[{x, y, z, t, f}, RGBColor[z]]]
Another way is to use textures; I will use the data you generated with the other software.
{x, y, z, c} = Import["/tmp/sphere.mat"];
The texture we want to apply is c (actually rotated 90 degrees)
ArrayPlot[c]
ParametricPlot3D[{Sin[t] Cos[f], Sin[t] Sin[f], Cos[t]}, {t, 0, Pi}, {f, 0, 2 Pi},
PlotStyle -> Texture[ Rotate[ArrayPlot[c], Pi/2]], Lighting -> "Neutral"]