I'm pretty new to Matlab and I would like to make a plot with errorbars. I have the errors in a vector expressed in % of the measured values. I have tried to use Matlab's errorbar but it is only shifting the plot in a strange way.
Standard usage is errorbar(x,y,yerr, ...options...).
In your case sounds like yerr = y.*percenterr/100
Related
I've been using interp1 to plot curves to follow sets of datapoints, and for most of the datapoints it's been working:
But when I try it with another set of datapoints it doesn't follow them at all:
For both interpolations the code I'm using is just:
curve = interp1(x, y, 'pchip');
Where x is just a set of numbers that correspond to the x axis of each datapoint, and y is the values themselves.
I can't tell what is different about the second dataset that is causing the interp1 function to not follow the data.
So with thanks to #m.s. for providing his code, it turns out the issue is that with the second graph I was interpolating with x= -90:10:90, whereas if I interpolate with 1:19, in a similar manner to the first graph, then the problem is fixed.
Hello i am having some problems understading basic plotting in matlab.
I can understand why you would use a for loop when plotting data?
Can anybody explain this to me?
I am making a simple linear plot. Is there any reason this should be inside a loop
If you are making a simple plot there is virtually no reason to use a loop.
If you check doc plot you will find that plot can take some vectors as input, or even matrices for more interesting situations.
Example:
x=0:0.01:1;
y=sin(x);
plot(x,y)
No there is no need in Matlab to use a for loop for plotting. If you are looking for a simple linear plot your code could look like this:
x=1:100;
y=3*x+4;
plot(x,y)
As you see there is no for loop needed. Same goes for nearly all plots and visualization.
A possible reason to use a for loop to plot thing may be having several data to plot in a single matrix. Say you have two matrix Ax (MxN) and Ay (MxN) where N the length of each data and M is the amount of different data wanted to plot. For example like in this case N is 201 and M is 3:
% Create Ax and Ay
Ax=meshgrid(0:0.1:20,1:3);
Ay=zeros(size(Ax));
% Sinusoidals with different frequencies
for k=1:3
Ay(k,:)=sin(k.*Ax(k,:));
end
% create colours
colorVec = hsv(3);
% Plot
hold on
for k=1:3
plot(Ax(k,:),Ay(k,:),'Color',colorVec(k,:))
end
hold off
You get:
I am using matlab and I have a certain set of x and y data,
x=[0,1.25,1.88,2.5,5,6.25,6.88,7.19,7.5,10,12.5,15,20];
y=[-85.93,-78.82,-56.95,-34.56,-33.57,-39.64,-41.96,-49.28,-66.6,-66.61,-59.16,-48.78,-41.53];
I want to use the curve fitting toolbox which has the spline function to generate a graph, so i did this,
cftool
It would bring me to the toolbox which i can then choose the spline fit. I was thinking if its possible that i extract the data points from the spline graph generated. Which means i probably would have more x and y data points than those that i input in, since the spline graph is sort of a continuous graph. Anyone could give me some advice on this? Thanks!
You can perform the equivalent of the spline fit performed with cftool with fit, see for instance here, here, or here:
% perform spline fit without cftool
ft = fittype('cubicspline');
coeff=fit(x,y,ft);
% use plot to display the interpolating polynomial
% (relying on internal plot settings)
figure
h=plot(coeff,x,y,'o');
% extract the *interpolated* curve from the figure
xi=get(h,'XData');
yi=get(h,'YData');
Replot it just to show that we can:
But if you just want to interpolate do as Fraukje explained here. Define a finer grid on x and use the interp1 function, as in the following example (same x,y input data as before):
% interpolate
Ni = 100;
xi = linspace(x(1),x(end),Ni);
yi = interp1(x,y,xi,'spline');
Now xi,yi is the interpolated data:
How to plot a 3D figure in MATLAB based on a function like f(x,y,z)=0?
And this complicated function can not be written as z = f(x,y).
f(x,y,z)=sum(a.*exp(sv(:,1)-x).^2+sv(:,2)-y).^2+sv(:,3)-z).^2)-b=0
where a is a known vector, sv is a known matrix, b is a known value. x,y,z are three variables. How to draw this surface in 3D way in matlab?
I just solve this question by this tool from the Matlab File Exchange:
Ezimplot3: implicit 3D functions plotter
your function only contains 1D vectors( I am assuming they are of equal lengths), if summed it will give you a constant; therefore, there is really nothing to plot.
I've used the gmdistribution to fit data to a Gaussian mixture model. I wanted to plot a contour plot http://imgur.com/yVE1M where the contours are obviously missing. For a 1D problem I found fplot, but now I'm stumped.
I ran into a similar problem when I wrote an EM algorithm for gaussian mixtures. Here is the snippet of code that fixed it in my case:
for l=1:k
zz=gmdistribution(MU(l,:),SIG(:,:,l),PI(l));
ezcontour(#(x,y)pdf(zz,[x y]),[minx1 maxx1],[miny1 maxy1],250);
end
The key is to increase N:
ezcontour(...,N) plots FUN over the default domain using an N-by-N
grid. The default value for N is 60.