I want to set the Y axis beginning value equal to 1e-20, but MATLAB always gives zero. My variable, which is meanerror has a value of 1e-15. So MATLAB's distances between the Y axis values are very big and whatever I do, I don't see 1e-15 in this graph.
Actually,I want to see 1e-15 after beginning any value which is not zero on Y axis. May be between 1e-20 (beginning value) and 1e-10.
How can I do that ?
Here is the code :
plot(x,meanerror,'Color','k');
set(gca, 'Ticklength', [0 0]);
set(gca,'xlim',[0 3e5]);
set(gca,'XTickLabel',{'0','','1E+5','','2E+5','','3E+5'});
set(gca,'ylim',[1e-20 1e3]);
Here is the graph:
Related
How do I find the actual y-range here:
This is just the example from the docs http://www.mathworks.com/help/matlab/ref/axis.html
x = linspace(-10,10,200);
y = sin(4*x)./exp(.1*x);
plot(x,y)
axis([-10 10 0 inf])
the ymin value is specified as zero and the max left automatic. If I now query the range with
get(gca,'YLim')
I just get [ 0 inf ]. How do i determine the actual plot y range used ( it is about [0 2.5] for this example..)
edit - aside
In case anyone else encounters this - it may be preferable to avoid the issue: make the plot with fully automatic ranging then fix the range as you like so you know exactly what it is, eg.
plot(x,y)
origYrange=ylim
origXrange=xlim
axis([origXrange 0 origYrange(2)])
Although the documentation doesn't tell, it appears that when the first (second) value of ylim is set to -inf (inf) Matlab sets the lower (upper) y-axis limit as the minimum (maximum) of all y values in the plot. The latter can be known by reading the 'YData' property of all 'children' of the axis.
yd = get(get(gca,'children'),'YData'); %// get y data of all plots
if iscell(yd) %// if there's more than one plot yd is a cell array of numeric vectors;
%// otherwise it's a numeric vector
yd = [yd{:}]; %// combine all values into a single numeric vector
end
ydminmax = [min(yd) max(yd)]; %// computed limits
result = ylim;
ind = isinf(result);
result(ind) = ydminmax(ind); %// replace infinite values by computed values
In your example, the result is
result =
0 2.4313
Can someone share a technique using MATLAB to plot the surface f(x,y)=(21/4)x^2y over the region x^2 <= y <= 1?
Also, if anyone is aware of some tutorials or links that would help with this type of problem, could you please share them?
Thanks.
Here is another approach:
%%
close all
x=linspace(-1,1,40);
g1=x.^2;
g2=ones(1,40);
y=[];
n=20;
for k=0:n
y=[y;g1+(g2-g1)*k/n];
end
x=x(ones(1,n+1),:);
z=21/4*x.^2.*y;
meshz(x,y,z)
axis tight
xlabel('x-axis')
ylabel('y-axis')
view(136,42)
And the result:
And finally, you can map the region (-1,1)x(0,1) in the uv-plane into the region bounded by $y=x^2 and y=1 in the xy-plane with the parametrization:
f(u,v) = (u\sqrt{v},v)
Capture from: https://math.stackexchange.com/questions/823168/transform-rectangular-region-to-region-bounded-by-y-1-and-y-x2
This code produces the same image shown above:
close all
[u,v]=meshgrid(linspace(-1,1,40),linspace(0,1,20));
x=u.*sqrt(v);
y=v;
z=21/4*x.^2.*y;
meshz(x,y,z)
axis tight
xlabel('x-axis')
ylabel('y-axis')
view(136,42)
First off, let's look at your valid region of values. This is telling us that y >= x^2 and also y <= 1. This means that your y values need to be on the positive plane bounded by the parabola x^2 and they also must be less than or equal to 1. In other words, your y values must be bound within the area dictated from y = x^2 to y = 1. Pictorially, your y values are bounded within this shape:
As such, your x values must also be bound between -1 and 1. Therefore, your actual boundaries are: -1 <= x <= 1 and 0 <= y <= 1. However, this only locates our boundaries for x and y but it doesn't handle where the plot has valid values. We'll tackle that later.
Now that we have that established, you can use ezsurf to plot surface plots in MATLAB that are dictated by a 2D equation.
You call ezsurf like so:
ezsurf(FUN, [XMIN,XMAX,YMIN,YMAX]);
FUN is a function or a string that contains the equation you want, and XMIN,XMAX,YMIN,YMAX contain the lowest and highest x and y values you want to plot. Plotting without these values assumes a span from -2*pi to 2*pi in both dimensions. As such, let's create a new function that will handle when we have valid values, and when we don't. Use this code, and save it to a new file called myfun.m. Make sure you save this to your current Working Directory.
function z = myfun(x,y)
z = (21/4)*x.^2.*y;
z(~(x.^2 <= y & y <= 1)) = nan;
end
This will allow you to take a series of x and y values and output values that are dictated by the 2D equation that you have given us. Any values that don't satisfy the condition of x^2 <= y <= 1, you set them to NaN. ezsurf will not plot NaN values.
Now, call ezsurf like so:
ezsurf(#myfun, [-1,1,0,1]);
You thus get:
This will spawn a new figure for you, and there are some tools at the top that will allow you interact with your 3D plot. For instance, you can use the rotation tool that's at the top bar beside the hand to rotate your figure around and see what this looks like. Click on this tool, then left click your mouse and hold the left mouse button anywhere within the surface plot. You can drag around, changing the azimuth and the latitude to get the perspective that you want.
Edit: June 4th, 2014
Noting your comments, we can decrease the jagged edges by increasing the number of points in the plot. As such, you can append a final parameter to ezsurf which is N, the number of points to add in each dimension. Increasing the number of points will decrease the width in between each point and so the plot will look smoother. The default value of N is 60 in both dimensions. Let's try increasing the amount of points in each dimension to 100.
ezsurf(#myfun, [-1,1,0,1], 100);
Your plot will look like:
Hope this helps!
Try the following to make the required function, compute the values, and plot only the region that is desired:
% Make the function. You could put this in a file by itself, if you wanted.
f = #(x,y) (21/4)*x.^2.*y;
[X Y] = meshgrid(linspace(0,1));
Z = f(X,Y);
% compute the values we want to plot:
valsToPlot = (X.^2 <= Y) & (Y <= 1);
% remove the values that we don't want to plot:
X(~valsToPlot) = nan;
Y(~valsToPlot) = nan;
Z(~valsToPlot) = nan;
% And... plot.
figure(59382);
clf;
surf(X,Y,Z);
I apologize for asking this, I believe this is a simple task, but I don't know how to do it.
Suppose I have a formula y = (exp(-x) + x^2)/sqrt(pi(x) and I want to plot it as y versus x^2.
How does one do this?
Like this:
X = 0:0.1:5; %// Get the x values
x = X.^2; %// Square them
%// Your formula had errors, I fixed them but I could have misinterpreted here, please check
y = (exp(-x) + x.^2)./sqrt(pi*x); %// Calculate y at intervals based on the squared x. This is still y = f(x), I'm just calculating it at the points at which I want to plot it.
plot(x,y) %//Plot against the square X.
At this point this is no different to having just plotted it normally. What you want is to make the tickmarks go up in values of X.^2. This does not change the y-values nor distort the function, it just changes what it looks like visually. Similar to plotting against a log scale:
set(gca, 'XTick', X.^2) %//Set the tickmarks to be squared
The second method gives you a plot like
edit:
Actually I think you were asking for this:
x = 0:0.1:5;
y = x.^2; %// Put your function in here, I'm using a simple quadratic for illustrative purposes.
plot(x.^2,y) %//Plot against the square X. Now your y values a f(x^2) which is wrong, but we'll fix that later
set(gca, 'XTick', (0:0.5:5).^2) %//Set the tickmarks to be a nonlinear intervals
set(gca, 'XTickLabel', 0:0.5:5) %//Cahnge the labels to be the original x values, now accroding to the plot y = f(x) again but has the shape of f(x^2)
So here I'm plotting a simple quadratic, but if I plot it against a squared x it should become linear. However I still want to read off the graph that y=x^2, not y=x, I just want it to look like y=x. So if I read the y value for the x value of 4 on that graph i will get 16 which is still the same correct original y value.
Here's my answer: it is similar to Dan's one, but fundamentally different. You can calculate the values of y as a function of x, but plot them as a function of x^2, which is what the OP was asking, if my understanding is correct:
x = 0:0.1:5; %// Get the x values
x_squared = x.^2; %// Square them
%// Your formula had errors, I fixed them but I could have misinterpreted here, please check
y = (exp(-x) + x.^2)./sqrt(pi*x); %// Calculate y based on x, not the square of x
plot(x_squared,y) %//Plot against the square of x
As Dan mentioned, you can always change the tickmarks:
x_ticks = (0:0.5:5).^2; % coarser vector to avoid excessive number of ticks
set(gca, 'XTick', x_ticks) %//Set the tickmarks to be squared
I am plotting some discrete values with a stem plot in MATLAB. I found that if the value is zero, the stem plot will put a circle on the x axis to show the zeros. Is there a way to have a stem NOT showing the circles if the value is zero?
Treat them as NaN's, ie:
Y = [1;2;3;0;3;2;4;0;1];
Y(Y == 0) = NaN;
stem(Y);
The 4th and 8th index will still exist on the x-axis, but if the observation is set to NaN, no line or circle will be plotted.
Hi can somebody help me with the Matlab command here. I've got to determine the highest point in a filled contour I've plotted using matrix data in a file. And then I have to mark the highest point with a red x.
load('0101862_mod.dtm') % loading the dtm file
X = X0101862_mod(1:81,:) % we name X0101862, it is the location where the data X, Y and Z is stored
Y = X0101862_mod(82:162,:)
Z = X0101862_mod (163:243,:)
figure (1)
subplot(2,2,3)
[C,h] = contourf(X,Y,Z,10);
xlabel('x'); ylabel('y'); zlabel('z'); title('X0101862_mod');
view(-73,34); axis equal; colormap summer; colorbar;
i know it involves max command. Kept getting error when i use max.
To plot the red 'X', you have to call first hold on to make sure that the second plotting command won't erase the contour. Then, you use plot(xMax,yMax,'xr') to plot a red 'x' at the x/y coordinates where z is at its maximum.
To find xMax and yMax, you have to use the second output argument of max. MAX returns, as first output, the maximum (e.g. of Z), and as a second output, it returns the number of the element that is maximal. Use that number (the index) to find the elements in X and Y that correspond to the maximum Z-value, i.e. xMax and yMax.