This question already has answers here:
How to make a log plot in matlab
(3 answers)
Closed 1 year ago.
I have to draw a quadratic plot (something like y=a*x^2). I wonder is there anyway to find a logarithmical scale for x or y axis to convert this plot to linear form?
for example lets imagine we have a plot like this:
I want the final plot be something like this by changing the x or y axis to logarithmical scale. I wonder if this is possible theoretically and the answer is yes how can I find the base value of logarithmical scale.
Not sure what the other answers are going on about here - you're right that if you plot a graph like y = ax^2 with logarithmic scaling on both axes, you'll see a straight line. For example, I just ran
x = 0:0.1:5;
y = x.^2;
plot(x, y)
set(gca, 'xscale', 'log')
set(gca, 'yscale', 'log')
in Matlab and the result is a straight line.
Alternatively, you could take the logarithm of the values and plot them,
plot(log(x), log(y))
with a linear axis scale. In this case the gradient of the line will be the power of your equation (for a quadratic like this, 2) and the y-intercept will be the constant log(a). This is because
y = ax^b
log(y) = log(ax^b)
log(y) = log(a) + log(x^b)
log(y) = log(a) + b*log(x).
The base of your logarithm doesn't matter, as long as you know what it is. For these sorts of problems it's common to use the natural logarithm, log in Matlab. log10 or log2 also work.
Note that these methods only work when x and y are both positive.
Related
>> x=0:0.001:720;
>> y=sind(x);
>> z=cosd(x);
>> surf(x,y,z);
I want to plot a surface using the above code i.e. x in X-axis, y is Y-axis, and z in Z-axis.
I also programmed a FORTRAN code for the following purpose, created a csv file, and plotted it in origin. I am getting a result as this:
However, in MATLAB, I am getting a similar idea when using:-
>> plot3(x,y,z)
as in this image:
but it's not a surface (for obvious reasons).
When using the surf command, I am also getting an error saying:
Z must be a matrix, not a scalar or vector.
What could be the possible problem with my code?
Using surf requires Z to be a matrix. This is fixed easily with functions like meshgrid (also useful is griddata).
Using meshgrid makes using surf very accessible.
But both Z and Y are just functions of X so I'm unable to explain why your plot Z-value would change with both X and Y. From the analytical (mathematical) equations you've listed, the Z-value should be constant in the Y-dimension.
stepsize = 1; % use 10 for cleaner look
x = 0:stepsize:720;
y = sind(x);
[X,Y] = meshgrid(x,y);
Z = cosd(X);
surf(X,Y,Z)
Notice that the contour lines are straight & parallel in the Y dimension (using surfc(X,Y,Z)).
Another method is to loop through elements of x (indexed by i) and y (indexed by j) where both x and y (vectors) to calculate Z(i,j) where Z is a matrix. Due to default assignment for rows & columns, this method requires transposing the Z matrix such as surf(X,Y,Z.').
Related Posts:
How can I plot a function with two variables in octave or matlab?
MATLAB plot part of surface
I have a problem in matlab.
I used a ksdensity function on a vector of deltaX, which was my computed X minus actual X.
And I did the same on deltaY.
Then I used plot on that data. This gave me two 2d plots.
As I have two plots showing how (in)accurate was my system in computing X and Y (something like gaussian bell it was). Now I would like to have one plot but in 3d.
The code was just like that:
[f,xi] = ksdensity(deltaX);
figure;
plot(xi,f)
Ok what I'm about to show is probably not the correct way to visualize your problem, mostly because I'm not quite sure I understand what you're up to. But this will show you an example of how to make the Z matrix as discussed in the comments to your question.
Here's the code:
x = wgn(1000,1,5);%create x and y variables, just noise
y = wgn(1000,1,10);
[f,xi] = ksdensity(x);%compute the ksdensity (no idea if this makes real-world sense)
[f2,xi2] = ksdensity(y);
%create the Z matrix by adding together the densities at each x,y pair
%I doubt this makes real-world sense
for z=1:length(xi)
for zz = 1:length(xi2)
Z(z,zz) = f(z)+f2(zz);
end
end
figure(1)
mesh(xi,xi2,Z)
Here's the result:
I leave it up to you to determine the correct way to visualize your density functions in 3D, this is just how you could make the Z matrix. In short, the Z matrix contains the plot elevation at each x,y coordinate. Hope this helps a little.
I have a vector with 1000 random numbers called v. I also have a vector, called x that represents the domain of which the numbers in v are generated, and another vector y that has the numbers of the cdf of the values in v. I know that I can do plot(x,y); and get a smooth function of the (non-empirical) cdf, and I also know that I can do cdfplot(v) to get a function of the empirical cdf.
My question is: How can I get these plots on the same set of axis?
Thank you for your help.
You could either generate data for an empirical cdf plot using ecdf or plot it directly with cdfplot like you mentioned. I would recommend using cdfplot since it sets up a few more things, such as a grid:
hFig = figure;
cdfplot(v);
hold all;
plot(x, y);
And as a bonus! Consider showing the X axis in logarithmic units, whichever reveals the data the best for you:
hAxes = get(hFig, 'CurrentAxes');
set(hAxes, 'XScale', 'log')
I have various plots (with hold on) as show in the following figure:
I would like to know how to find equations of these six curves in Matlab. Thanks.
I found interactive fitting tool in Matlab simple and helpful, though somewhat limited in scope:
The graph above seems to be linear interpolation. Given vectors X and Y of data, where X contains the arguments and Y the function points, you could do
f = interp1(X, Y, x)
to get the linearly interpolated value f(x). For example if the data is
X = [0 1 2 3 4 5];
Y = [0 1 4 9 16 25];
then
y = interp1(X, Y, 1.5)
should give you a very rough approximation to 1.5^2. interp1 will match the graph exactly, but you might be interested in fancier curve-fitting operations, like spline approximations etc.
Does rxns stand for reactions? In that case, your curves are most likely exponential. An exponential function has the form: y = a*exp(b * x) . In your case, y is the width of mixing zone, and x is the time in years. Now, all you need to do is run exponential regression in Matlab to find the optimal values of parameters a and b, and you'll have your equations.
The advice, though there might be better answer, from me is: try to see the rate of increase in the curve. For example, cubic is more representative than quadratic if the rate of increase seems fast and find the polynomial and compute the deviation error. For irregular curves, you might try spline fitting. I guess there is also a toolbox in matlab for spline fitting.
There is a way to extract information with the current figure handle (gcf) from you graph.
For example, you can get the series that were plotted in a graph:
% Some figure is created and data are plotted on it
figure;
hold on;
A = [ 1 2 3 4 5 7] % Dummy data
B = A.*A % Some other dummy data
plot(A,B);
plot(A.*3,B-1);
% Those three lines of code will get you series that were plotted on your graph
lh=findall(gcf,'type','line'); % Extract the plotted line from the figure handle
xp=get(lh,'xdata'); % Extract the Xs
yp=get(lh,'ydata'); % Extract the Ys
There must be other informations that you can get from the "findall(gcf,...)" methods.
I 'm having a problem with creating a joint density function from data. What I have is queue sizes from a stock as two vectors saved as:
X = [askQueueSize bidQueueSize];
I then use the hist3-function to create a 3D histogram. This is what I get:
http://dl.dropbox.com/u/709705/hist-plot.png
What I want is to have the Z-axis normalized so that it goes from [0 1].
How do I do that? Or do someone have a great joint density matlab function on stock?
This is similar (How to draw probability density function in MatLab?) but in 2D.
What I want is 3D with x:ask queue, y:bid queue, z:probability.
Would greatly appreciate if someone could help me with this, because I've hit a wall over here.
I couldn't see a simple way of doing this. You can get the histogram counts back from hist3 using
[N C] = hist3(X);
and the idea would be to normalise them with:
N = N / sum(N(:));
but I can't find a nice way to plot them back to a histogram afterwards (You can use bar3(N), but I think the axes labels will need to be set manually).
The solution I ended up with involves modifying the code of hist3. If you have access to this (edit hist3) then this may work for you, but I'm not really sure what the legal situation is (you need a licence for the statistics toolbox, if you copy hist3 and modify it yourself, this is probably not legal).
Anyway, I found the place where the data is being prepared for a surf plot. There are 3 matrices corresponding to x, y, and z. Just before the contents of the z matrix were calculated (line 256), I inserted:
n = n / sum(n(:));
which normalises the count matrix.
Finally once the histogram is plotted, you can set the axis limits with:
xlim([0, 1]);
if necessary.
With help from a guy at mathworks forum, this is the great solution I ended up with:
(data_x and data_y are values, which you want to calculate at hist3)
x = min_x:step:max_x; % axis x, which you want to see
y = min_y:step:max_y; % axis y, which you want to see
[X,Y] = meshgrid(x,y); *%important for "surf" - makes defined grid*
pdf = hist3([data_x , data_y],{x y}); %standard hist3 (calculated for yours axis)
pdf_normalize = (pdf'./length(data_x)); %normalization means devide it by length of
%data_x (or data_y)
figure()
surf(X,Y,pdf_normalize) % plot distribution
This gave me the joint density plot in 3D. Which can be checked by calculating the integral over the surface with:
integralOverDensityPlot = sum(trapz(pdf_normalize));
When the variable step goes to zero the variable integralOverDensityPlot goes to 1.0
Hope this help someone!
There is a fast way how to do this with hist3 function:
[bins centers] = hist3(X); % X should be matrix with two columns
c_1 = centers{1};
c_2 = centers{2};
pdf = bins / (sum(sum(bins))*(c_1(2)-c_1(1)) * (c_2(2)-c_2(1)));
If you "integrate" this you will get 1.
sum(sum(pdf * (c_1(2)-c_1(1)) * (c_2(2)-c_2(1))))