Plotting a best fit curves - matlab

Suppose I have a best fit equation for a set of data.How can I plot the data with marker and the best fit curve together. I know the command to include a data marker for a curve but this will make the curve pass through all the points if I type something like this
plot(x,polyval(p,x),'kx',x,polyval(p,x))
But this is not what I want since I am plotting a best fit curve. So I would like to know how exactly I can do that. Thanks in advance for all the helps. And between how can I add a polynomial that pass through all the point and the best fit curve as well as the data point with data marker on a same figure.

Where is the data stored? lets say your datapoints are in x and y, then make a much finer xi so something like linspace(min(x), max(x), 1000) or xi = -100:0.01:100 or whatevers appropriate and then to plot
plot(x, y, 'k*', xi, polyval(p, xi))

Related

Matlab Plot Legend Locating on Each Curve

I have a Matlab plot that contains relatively high amount of different data and hence if i use standard legend representation, i could not figure out which legend color belongs to which curve.
So, is there any way to put legends on each curve? Lets say "BUS7" text should be located on the purple curve (undermost one).
Thanks.
I am trying to do something like this:
The text(x, y, txt) function allows you to add text at a given location on the plot. link.
Looking at your example, it seems that you want to have all the labels on the left-hand side. So, one way to do it would be like this, where I have generated my own dummy data:
leg = {'First Curve', 'Second Curve', 'Third Curve'};
figure;
x = linspace(1,10,100);
for ii = 1:3
y = x + ii*10;
plot(x, y);
hold on
text(x(1), y(1)+2.5, leg{ii});
end
Regretfully, this does not do anything clever with where the label is placed - it is merely placed slightly above the first point of the curve. You can clearly easily move it along the curve, but if you have many curves, like in your case, the labels will likely overlap and you will need to tinker a bit to avoid this.

smooth out/filter matrix data

I've got a 25x10 matrix that shows a fairly rough surface when i plot it.
I'd like to filter it out to create a smooth surface with more datapoints for a finer grid(but not lineary interpolated), and i need the filtered data that produces the smoothed surface as base for an open-loop. Can anyone recommend me a good approach? The result right now looks like this:
Cheers
EDIT: griddata with method 'cubic' looks more like the desired result, but it's still holding onto the data too much. The image below visualizes the result with cubic, and the desired result. The goal is to see no more clear peaks in the surface.
Use griddata to interpolate your data over a finer mesh. If x, y and z is your data, you can define a fine mesh (xq, yq) and interpolate / extrapolate your data on that extend. Since the default method is 'linear' and because you want to make your surface profile smoother, you should specify another method, like 'natural'.
[xq, yq] = meshgrid(0:20:6000, 0.2:0.01:1);
zq = griddata(x(:), y(:), z(:), xq, yq, 'natural');
surf(xq, yq, zq, 'EdgeColor', 'none')

How to set an arbitrary direction on a contour plot to perform operation in Matlab

I am looking for help for my particular problem.
I have a contour plot created from XYZ data. This plot contains 2 broad peaks with one more intense than the other.
When the most intense peak is aligned with the Y axis, I can perform a fitting of every YZ curve at each X values. I usually do a gaussian fit to plot the peak center on the same graph.
In some cases I need to perform the same fitting but no along the Y axis direction (in this case I just plot YZ scan at every different X values) but along another arbitrary direction.
For the moment the only way I found is the following:
-plot the contour plot and find for the position of the most intense peak
-if the position is not aligned with the Y axis, then rotate all the datas and plot again the contour
-perform the YZ gaussian fit for every X value
- Rotate the resulting XY position to go back to the original plot
-plot the XY position as a line on the original contour plot
this is quite long and requires a lot of memory. i would like ot know if there is a more elegant/faster way.
Thanks for your help
David
I take it you want to extract data from the (x,y,z) data along some arbitrary line in order to make a fit. A contour plot will show only part of the data, the full z(x,y) data can be shown with imagesc etc. Say you want the data along line defined by two points (x1,y1) -> (x2,y2). According to the eq of the line, the line y=a*x+b the slope a is (y2-y1)/(x2-x1) and b=y1-a*x1. For example, I'll select (x,y) coordinates in the following contour:
Create data and end points:
m=peaks(100);
x1=11 ; x2=97;
y1=66; y2=40;
Thus the line parameters are:
a=(y2-y1)/(x2-x1);
b=y1-a*x1;
and the line is:
x=x1:x2;
y=round(a*x+b);
select the proper (x,y) elements using linear indexing:
ind=sub2ind(size(m),y,x)
plot:
subplot(2,1,1)
contour(m,10); hold on
line([x1 x2],[y1 y2],'Color',[1 0 0]);
subplot(2,1,2)
plot(m(ind))
You can now use vec=m(ind) to fit your function.

Scatter non-square matrix

I would like to scatter matrix of any dimensions (height, width, depth). The problem is that scatter takes matrices of equal dimensions.
Is there any alternative way to achieve this?
I was thinking about complementing my matrix to square one and then minimizing the size and color of those points that are "virtual", but I'm trying to find a better solution.
Can anyone help?
Here is the way I intend this to work:
[x,y,z] = meshgrid(1:height, 1:widht, 1:depth);
scatter3(x(:), y(:), z(:), size, data_matrix(:));
To show purpose of this code: Say, I have temperature in a room stored in a matrix. I want to plot the temperature in a 3D plot. If there is a better way to achieve this then scatter (which seems to work for equal dimensions case), will be grateful to know it :D

Finding weight of each grid in Matlab?

So I have two vectors*(x & y)* with x and y co-ordinate data points. I would like to put these points on grid kinda like a scatter plot and count the number of points within each grid like the weight of each grid using Matlab.
What would you guys suggest is the best way to do this? Any suggestion is appreciated
I assume you mean that you want to perform a 2D histogram of your x-y data.
You can use several tools from the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/9896-2d-histogram-calculation
http://www.mathworks.com/matlabcentral/fileexchange/1487
http://www.mathworks.com/matlabcentral/fileexchange/14205-2d-histogram