Cloud of points curve fitting - matlab

I have a 2D cloud of points, i imported them into matlab and i want to have a smoothing spline function that passes through them. I tried with the "curve fitting" app and the "smoothing spline" option but in the extremes the function seems to not follow much the cloud and anyway in the middle of the cloud does not pass trough the points but goes up or down the points. I tried "basic fitting" option after i plotted the cloud but no improvements as you can see:
The idea of what i wanted is this:
Of course i modified the image with Gimp (i am using linux) and the red line is a hand made one...so not so smooth as it would be. Note i have magnified the image.
I was asking to myself if it was possible to have a variable grade fitting along all the cloud.
Anyway i don't know how to do it and any suggestion is appreciated.
The "smoothing spline" (in the "curve fitting tool") seems to be more equal to an interpolation than to a spline

You could use the function spline instead of the graphical tool. For example:
x = 1:100; % x-axis values for data
y = conv(randn(1,100),ones(1,10),'same'); % example smooth random data
xx = 1:.1:100; % x-axis values for plotting the spline
yy = spline(x,y,xx);
plot(x,y,'.',xx,yy)
See the doc of spline for more information.

Related

Linear transformations performed on a coordinate grid in MATLAB

Is it possible to graphically represent linear transformations in MATLAB like the one shown below?
In particular, I'm looking for a way to represent the way a coordinate grid might become squished, stretched, or rotated after a matrix multiplication has been performed. I've tried using meshgrid(x,y) to plot the grid but I've been having trouble getting it to display properly. Ideally, it would be nice to see the way that the transformation acts on a unit square (and, if possible, to see the transformation being performed in a continuous fashion, where you can see the graphs morphing into each other). However, I'm willing to sacrifice these last two requirements.
Here is the code I have used so far:
x = -10:2:10;
y = -10:2:10;
[X,Y] = meshgrid(x,y);
plot(X,Y)
For some reason, only vertical lines have been plotted. (The documentation says it will display a square grid.)
please read the plot doc.
the problem is that what you are doing is trying to plot mash matrixes in a plot not by using mesh or something like that.
you may find this very helpful.

Draw surface of 3D cloud in matlab

I have 3D cloud of dots. I need to plot them as a surface. I tried variant with meshdrid, griddata, scatteredInterpolant,trisurf-delaunay. Nothing works. I know that this question was discussed a lot, but it seems I don't understand some important details. The code which i have now:
load('coords.mat')
figure()
subplot(1,2,1)
plot3(x,y,z,'.')
axis off
view(3)
subplot(1,2,2)
C=gray(numel(x)); % unsuccessful attempt
[~,idx]=sort(z); % to have
C=C(idx,:); % illumination
scatter3(x,y,z,50,C,'filled')
axis off
view(3)
produces the following image:
Could you help me:
1) to find a way to draw it with surface function.
and as some dots may be inside the surface (may be it is my problem)
2) How to remove 'invisible' dots?
I need solution for different cases, picture and data presents just an example.
Mat file may be downloaded here.
P.S.
In case it is important – I obtain coordinates of this dots as a rotation of random bezier curve.
UPDATE
In case data above is too big I generate another set with smaller amount of dots:
Coordinates are here.
where do you get this data from? It is represented as vectors but if you reshape it to matrices you can use the surf function. Try this code:
z=reshape(z,100,100);
y=reshape(y,100,100);
x=reshape(x,100,100);
surf(x,y,z)

MATLAB Plotting Contour Map from Different Plots

I am trying to write a MATLAB script to give me a contour map. The contour map must be created from inputs that I generated from 100 images.
The story is like this:
I have 100 images on which I ran an image processing algorithm for optimization. Now, I got their energy curves. So, I have 100 energy curves. I want to create a contour map that will show me where the points are denser on the plot. (the energy curves are plotted as energy vs. iteration with fixed number of iterations)
The following is my variable:
energy(iteration,numImages)
Hope I explained it well.
Thanks in advance.
I interpret your question to boil down to how can I create a surface plot with colors according to the energy found in energy. I would solve this by using the contour function with a grid generated using meshgrid. If each image is described in 1000 data points with 100 files the plot can be generated as follows:
% using stuff as random junk instead of energy
numPoints = 1000;
numFiles = 100;
stuff = rand(1000,100); % replace with actual information
[X, Y] = meshgrid(1:numFiles, 1:numPoints);
contour(X,Y,stuff);
You can also create a 3D surface plot using surf and the same logic.
From what i see of you graph (and using the comments also), one possible way is to use plot3 to plot a line in 3D for every plot.
For doing so, you can use something like this code:
x=(0:0.01:1)';
aexp=zeros(100,numel(x));
hold on
for ii=1:100;
% aexp(ii,:)=exp((-x+ii/10)); %exponential
aexp(ii,:)=exp(-(x-ii/100).^2); %~gaussian
% aexp(ii,:)= x*ii; %linear increase
plot3(x,aexp(ii,:),ii*ones(1,numel(x)));
end
% set(gca,'yscale','log'); % uncomment if you need logscale.
giving
I have a few options of plot. It always plot from the XY view. I changed by hand, but you can use the view command. Notice that i used a simple counter to make the spacing in the z direction.
In a similar manner, you can plot using the contour. For my code, after the data have been generated in the for loop, remove/comment the plot3 and add:
contour(aexp) %outside the for loop,
giving
Notice that i have not really take care what i'm plotting. You can find more info on contour in the Matlab page .
You commented that the x-axis should be number of iterations, y-axis should be energy and z-axis should be the information containing how many lines are passing through from some areas. For this, make a qq variable, being it qq=number_of_lines(number of iterations,energy) . Make a discrete grid for the energy if you don't have one. Number of iterations is probably discrete anyway. The function is you who need to devise, but i would go for something which checks the number of lines for every energy and every iteration. In this case you will have the z-function that depends on y and x, that is the case to use contour or surface.
My function above make a line for every ii point, to have a 3d function. An edition for another extra loop is not hard. Just remember to have the same regular grid for every point, otherwise you will have trouble.

How to fit a surface ((x,y,z) matrix) using Matlab?

I made AFM (Atomic Force Microscopy) measurements. I exported my data from Gwyddion to a text file (can be download here), such as I load it in Matlab like:
data = importdata('001_Zsensor.xyz');
x=data(:,1);y=data(:,2);z=data(:,3);
shading('interp');
tri = delaunay(x,y);
figure(1)
tsurf=trisurf(tri,x,y,z,'EdgeColor','none','Facecolor','interp');
So now I have my surface. It corresponds to the rugosity of a bead, so I need to extract the spherical feature of this surface in order to recover the rugosity landscape over a plane surface (from which I can then compute my physical parameters).
Basically, I want to fit a ellipsoid to the surface I previously defined (tsurf). I tried using cftool (even though I'd rather use command so I can put this in a Matlab script) but as the equation should be of form
z=f(x,y)
and the ellipsoid's equation is
((x-x0)/a)^2 + ((y-y0)/b)^2 + ((z-z0)/c)^2 = 1,
I did not manage to get the fit to work.
How can I do so?
Thank you very much.
Ok, found it! Here it is:
Following the previous code lines I wrote (see the question of this post):
ffit = fit([x y],z,'poly22')
figure(1)
hold on
plot(ffit)
Of course, the fit can be more constrained if needed.

Suggestions on plotting 3 1-Dimensional variables

I have taken readings on my software defined radio. I have three quantities, frequency, power and error rate. I would like to keep frequency on my x-axis as the latter quantities are measured with respect to frequency. Now I need to plot them on a single curve so that i can see frequency vs power and frequency vs error rate in one shot. I have been looking at matplotlib for quite some time but I see the 3d scatter plots and they seem to not convey the picture i want. So my question is
Is 3d plot useless in my case and I better plot 2 graphs and join the y axes of both graphs together so that I could see both the graphs on top of each other.
Is there a way in python or matlab such that you plot a 3d curve and then if you move the cursor you can see frequency vs power and if you move the cursor the other way you can see frequency vs error rate.
Any other ideas on how i can represent my readings better will be helpful.
When two sets of data share the same dependant variable (x-axis), it is common in scientific literature at least to plot them both in the following manner to save space:
This is preferred to twining the xaxis which can cause confusion for some people. It works especially well if features are to be compared.
This plot was generated using:
import pylab as py
x = py.linspace(0,10,1000)
y = py.sin(x)
z = py.sinc(x)
ax1 = py.subplot(211)
ax1.plot(x,y)
ax1.set_xticklabels([]) # Remove the xticks from the top figure
ax1.set_yticks(ax1.get_yticks()[1:]) # Stops the y axis overlapping
ax2 = py.subplot(212)
ax2.plot(x,z)
py.subplots_adjust(hspace=0.0)
py.show()
I believe there is a better way to remove the xticklabels but I can't remember it at the moment. Be careful not to just py.set_xticks([]) as if you then ask for a grid the top plot has no ticks.