Gaussian Contour plot of 3 variables - MATLAB - matlab

I have generated a 3D plot that resembles Gaussian distribution, with random variables Y, X1, and X2 (1000x1) vectors. Y is on the vertical axis, X1, and X2 are horizontal.
Specifically, this is the code I used for the plot:
plot3(x(:,1),x(:,2),y,'.')
The graph that has been created has this form:
What I also want to produce is something like that:
But, when I use this code:
contour(x(:,1),x(:,2),y);
I receive a message that:
Error using contour (line 48)
Z must be at least a 2x2 matrix.
I really don't get how to fix that problem, I assume Z is the Y but I don't understand why it has to be 2x2 at least. Anyhow, any help would be much appreciated.

You cannot create a contour over scattered data, you need a grid. It is possible to interpolate the data on a grid of NxN samples in the XY domain, using griddata (here the domain is [-2,2]x[-2,2] as an example):
N = 200;
xi = linspace(-2, 2, N);
yi = linspace(-2, 2, N);
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(x(:,1), x(:,2), y, XI, YI, 'v4');
contour(XI,YI,ZI);
More info on how to interpolate scattered data here.

Related

Using Surf and Peaks for non square Matrices

Im trying to use meshgrid with peak. When I use square matrices like:
[x,y] = meshgrid(1:10,1:10)
z = peaks(10)
surf(x,y,z)
Everything works fine.
However, when I use Non square matrices:
[x,y] = meshgrid(-30000:500:0,10:500);
z = peaks(?)
surf(x,y,z)
Im getting the following error:
Data dimensions must agree.
Any idea how to make it work?
Thank you.
When you plot a function using surf the variables you pass it must all be the same size. Although you are changing the x and y matrices to non-square matrices, you are not creating a correspondingly sized z matrix. If you look at the documentation for peaks, this tells you that this function can only produce square outputs, which complicates things somewhat. I would suggest that you try to plot some other example function instead.
If you absolutely must plot the peaks function, you could use some kind of interpolation:
x = 1:10;
y = 1:10;
[x_mesh, y_mesh] = meshgrid(x,y);
z = peaks(10);
x2 = 1:0.1:10;
y2 = 1:0.5:10;
[x2_mesh, y2_mesh] = meshgrid(x2, y2);
z2 = interp2(x_mesh, y_mesh, z, x2_mesh, y2_mesh);
surf(x2_mesh, y2_mesh, z2);
This gives me the following plot:

How to get the integral (function) from a vector?

I have a vector than can be plotted and I would like to compute its integral. I don't mean the total area below, but how it evolves over the domain of integration. Basically, its "indefinite" integral. Is this possible? maybe via interpolation? Since I am working with Chebyshev differentiation matrices, do you know if there is an equivalent for integration?
Thank you
You probably want cumtrapz:
x = linspace(0,2*pi,1000); % example x axis values
y = sin(x); % example function
I = cumtrapz(x, y); % compute its integral
plot(x, y, x, I) % plot them
grid on % grid

How to plot using surf gird in 2D using function value

if the function F is available it is easy to draw surf plot i.e.
x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;
in my case I calculated F function using least square:
for example I have x and y vectors
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2754.5 4043.3 5376.3 5050.4];
the linear function of these two vector is define by
F= [1149.73 , 37.63];
therefore the estimation is equal to
z= [ones(5,1) x']*F';
which is
z = [1149.73 1519.67 4051.96 5589.35 4912.65];
and if it is plotted
plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');
The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs
[X,Y] = meshgrid(x,y);
but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this
Thanks
P.S: the F is calculated using pinv (least square).
You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors
xi=linspace(min(x),max(x),100)
yi=linspace(min(y),max(y),100)
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
contourf(XI,YI,ZI)

Cross section 2D data of symmetric len into Z matrix MATLAB - 3D plot from 2D cross section

I have a problem and maybe you will be able to help me. Like in the title i have cross section data of a symmetric lens - coordinates s=-100:1:100 and height y - and I would like to create 3D plot the whole lens (x,y,z). Is there any build in function that helps with that? Thanks for help in advance!
If I'm understanding correctly, you have a 1-D array that you'd effectively like to 'sweep' around a circle to produce a 3-D plot. Here is an example of how to do that
% Some dummy data
Npts = 100;
z = sin(linspace(0, pi, Npts));
Nreps = 100; % How many times to repeat around circle
% Create polar meshgrid and convert to Cartesian
[r, theta] = meshgrid( ...
linspace(-length(z)/2, length(z)/2, Npts), ...
linspace(0, pi, Nreps));
[X, Y] = pol2cart(theta, r);
% Copy data Nreps times
Z = repmat(z, Nreps, 1);
% Plot!
surf(X, Y, Z)
Without more specs (such as if your y is a 2D matrix or a 1D array), it's not possible to give you the exact answer. However here is how you draw a surface in Matlab:
% create a meshgrid used as the independent variables of your surface
[sx, sy] = meshgrid(-100:100);
% if you have your own 2D matrix, ignore this line.
% if you have a formula, replace this line with your own formula
y = cos(sqrt(((sx/100).^2+(sy/100).^2)/2)*(pi/2));
% draw the surface
surf(sx, sy, y);
To have the opposite side as well, draw another surf on the same figure:
hold on;
surf(sx, sy, -y);

Plot a curve rather than a line

In matlab, I have a vector, say x and a function of x say y. I want to plot x and y in matlab.
The problem is I want smooth curve (not in a sense of smooth texture but differentiable, and without sharp bends). Matlab, with plot, simply joins the points and the plotted curve has sharp bends.
Is there a way I can resolve this?
Following Dan and wakjah, what you need is to interpolate x and y to more sample points
plot( x, y, '+r' ); % plot the original points
n = numel(x); % number of original points
xi = interp1( 1:n, x, linspace(1, n, 10*n) ); % new sample points
yi = interp1( x, y, xi );
hold all;
plot( xi, yi ); % should be smooth between the original points