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
Related
I want to solve my differential equation and plot velocity vectors but I am having some trouble with that. I tried this:
syms y(x);
ode = (1+exp(x))*y*diff(y,x)-2*exp(x) == 0;
ySol = dsolve(ode)
[X,Y] = meshgrid(-2:.2:2);
Z = 2*exp(X)/((1+exp(X)).*Y);
[DX,DY] = gradient(Z,.2,.2);
figure
contour(X,Y,Z)
hold on
quiver(X,Y,DX,DY)
hold off
and I get this error:
Warning: Matrix is singular to working precision.
Warning: Contour not rendered for non-finite ZData
It is probably something simple that I do not see but I am just starting using Matlab and I cold not find a right way to do my task. Please help me...
EDIT
As bconrad suggested, I changed my Z function like this:
Z = 2*exp(X)/((1+exp(X)).*Y);
and the previous errors are fixed. However, my prime goal, to plot velocity vectors is not accomplished yet because I get a graph like this:
Don’t have the ability to check at the moment, but I reckon you want an element by element division in that line. You’re missing a dot on the division, try
Z = 2*exp(X)./((1+exp(X)).*Y);
I took a closer look once at my station. The zero-division mentioned by Pablo forces inf's in Z, so quiver get's confused when scaling the vectors (understandably) and just doesn't show them. Try this (with the ode part removed):
[X,Y] = meshgrid(-2 : .2 : 2);
Z = 2 * exp(X) ./ ((1 + exp(X)) .* Y);
Z(isinf(Z)) = nan; % To avoid 0-division problems
[DX, DY] = gradient(Z, .2, .2);
figure
contour(X, Y, Z, 30, 'k')
hold on
quiver(X, Y, DX, DY, 6)
hold off
I've done 3 things here:
Added the line Z(isinf(Z)) = nan; forcing infinite values to be essentially ignored by quiver
Added the arguments 30, 'k' to the contour function to show 30 lines, and make them black (a bit more visible)
Added the argument 6 to the quiver function. This overrides the automatic length-scaling of the vectors.
You'll want to play with the arguments in the contour and quiver functions to make your figure appear as you'd like.
PS: There is a handy arrow function on the file exchange that I find gives better control when creating vector field plots. See https://www.mathworks.com/matlabcentral/fileexchange/278-arrow - the ratings do it justice.
I have three different surfaces and I want to display all of them in one figure.
The problem is, that I have one surface defined in terms of z (means that I got x and y values and a grid which specifies the z-values for each combination) and two other ones which are defined in terms of x. This means that there exist various z-values for one x,y-pair.
My idea was:
figure
surf(x,y,zgrid)
hold on
surf(x,ygrid,z)
surf(x,ygrid2,z)
hold off
I hoped MATLAB would manage it by itself but it doesn't.
Do you have any ideas how to get the wanted results? I want to display all of them in one plot to show the cross-sections.
Here is an image of how it should more or less look like:
If there is a more beautiful method to display this, please let me know.
You didn't specify what exactly was going wrong, but I'll hazard an educated guess that you got an error like the following when you tried to plot your second surface:
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.
I'm guessing your x, y, and z variables are vectors, instead of matrices. The surf function allows for the X and Y inputs to be vectors, which it then expands into matrices using meshgrid. It doesn't do this for the Z input, though.
It's best practice, in my opinion, to just use matrices for all your inputs anyway. Here's an example where I do this (using meshgrid) to plot three surfaces of a cube:
% Top surface (in z plane):
[x, y] = meshgrid(1:10, 1:10);
surf(x, y, 10.*ones(10), 'FaceColor', 'r');
hold on;
% Front surface (in y plane):
[x, z] = meshgrid(1:10, 1:10);
surf(x, ones(10), z, 'FaceColor', 'b');
% Side surface (in x plane):
[y, z] = meshgrid(1:10, 1:10);
surf(ones(10), y, z, 'FaceColor', 'g');
axis equal
And here's the plot:
I am attempting to create a plot in MatLab of a specified function which has specific data markers that have drop down lines (preferably dashed) to the X/Y axis the correspond to that specific marker.
Below is my MatLab code for just the plot and the markers:
x = 0:4000;
y = 1 - exp(-(power(((x-900)/1015.54),2)));
xmarkers = [1800,2000];
ymarkers = 1 - exp(-(power(((xmarkers-900)/1015.54),2)));
plot(x,y,'b',xmarkers,ymarkers,'b*');
This results in the following plot:
I envision a graph like this, preferably with automatic labels for the specified X/Y value pair at the end of the lines:
My core question would be: Is this possible? And if it is, how would one go about coding this? I understand I could probably manage this manually by adding lines to the plots myself, but this is infeasible for large amounts of markers.
You can define a function which draws the marker:
function draw_marker(x, y)
hold on
plot(x, y, 'b*')
% plot coordinates as text
str = sprintf('(%d, %d)', x, y);
text(x, y, str)
plot([0, x], [y, y], 'k-.')
plot([x, x], [0, y], 'k-.')
end
And then you can create your plot like this:
plot(x,y,'b');
draw_marker(xmarkers(1), ymarkers(1))
draw_marker(xmarkers(2), ymarkers(2))
It would be nicer, of course, if you hand the figure handles as an argument, but the above works.
MATLAB's surf command allows you to pass it optional X and Y data that specify non-cartesian x-y components. (they essentially change the basis vectors). I desire to pass similar arguments to a function that will draw a line.
How do I plot a line using a non-cartesian coordinate system?
My apologies if my terminology is a little off. This still might technically be a cartesian space but it wouldn't be square in the sense that one unit in the x-direction is orthogonal to one unit in the y-direction. If you can correct my terminology, I would really appreciate it!
EDIT:
Below better demonstrates what I mean:
The commands:
datA=1:10;
datB=1:10;
X=cosd(8*datA)'*datB;
Y=datA'*log10(datB*3);
Z=ones(size(datA'))*cosd(datB);
XX=X./(1+Z);
YY=Y./(1+Z);
surf(XX,YY,eye(10)); view([0 0 1])
produces the following graph:
Here, the X and Y dimensions are not orthogonal nor equi-spaced. One unit in x could correspond to 5 cm in the x direction but the next one unit in x could correspond to 2 cm in the x direction + 1 cm in the y direction. I desire to replicate this functionality but drawing a line instead of a surf For instance, I'm looking for a function where:
straightLine=[(1:10)' (1:10)'];
my_line(XX,YY,straightLine(:,1),straightLine(:,2))
would produce a line that traced the red squares on the surf graph.
I'm still not certain of what your input data are about, and what you want to plot. However, from how you want to plot it, I can help.
When you call
surf(XX,YY,eye(10)); view([0 0 1]);
and want to get only the "red parts", i.e. the maxima of the function, you are essentially selecting a subset of the XX, YY matrices using the diagonal matrix as indicator. So you could select those points manually, and use plot to plot them as a line:
Xplot = diag(XX);
Yplot = diag(YY);
plot(Xplot,Yplot,'r.-');
The call to diag(XX) will take the diagonal elements of the matrix XX, which is exactly where you'll get the red patches when you use surf with the z data according to eye().
Result:
Also, if you're just trying to do what your example states, then there's no need to use matrices just to take out the diagonal eventually. Here's the same result, using elementwise operations on your input vectors:
datA = 1:10;
datB = 1:10;
X2 = cosd(8*datA).*datB;
Y2 = datA.*log10(datB*3);
Z2 = cosd(datB);
XX2 = X2./(1+Z2);
YY2 = Y2./(1+Z2);
plot(Xplot,Yplot,'rs-',XX2,YY2,'bo--','linewidth',2,'markersize',10);
legend('original','vector')
Result:
Matlab has many built-in function to assist you.
In 2D the easiest way to do this is polar that allows you to make a graph using theta and rho vectors:
theta = linspace(0,2*pi,100);
r = sin(2*theta);
figure(1)
polar(theta, r), grid on
So, you would get this.
There also is pol2cart function that would convert your data into x and y format:
[x,y] = pol2cart(theta,r);
figure(2)
plot(x, y), grid on
This would look slightly different
Then, if we extend this to 3D, you are only left with plot3. So, If you have data like:
theta = linspace(0,10*pi,500);
r = ones(size(theta));
z = linspace(-10,10,500);
you need to use pol2cart with 3 arguments to produce this:
[x,y,z] = pol2cart(theta,r,z);
figure(3)
plot3(x,y,z),grid on
Finally, if you have spherical data, you have sph2cart:
theta = linspace(0,2*pi,100);
phi = linspace(-pi/2,pi/2,100);
rho = sin(2*theta - phi);
[x,y,z] = sph2cart(theta, phi, rho);
figure(4)
plot3(x,y,z),grid on
view([-150 70])
That would look this way
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?)