I have a continuous step curve and now I need to integrate the higher step curve. Can I specify where X and Y start in Matlab. How to implement this method?
My original data is,
i = 0:0.000005:xmax
x = i
I've tried making X out of 18.52503 an integer, but the result isn't as good as it should be.
i = 18.52503:0.00005:xmax
Related
The figure shown above is the plot of cumulative distribution function (cdf) plot for relative error (attached together the code used to generate the plot). The relative error is defined as abs(measured-predicted)/(measured). May I know the possible error/interpretation as the plot is supposed to be a smooth curve.
X = load('measured.txt');
Xhat = load('predicted.txt');
idx = find(X>0);
x = X(idx);
xhat = Xhat(idx);
relativeError = abs(x-xhat)./(x);
cdfplot(relativeError);
The input data file is a 4x4 matrix with zeros on the diagonal and some unmeasured entries (represent with 0). Appreciate for your kind help. Thanks!
The plot should be a discontinuous one because you are using discrete data. You are not plotting an analytic function which has an explicit (or implicit) function that maps, say, x to y. Instead, all you have is at most 16 points that relates x and y.
The CDF only "grows" when new samples are counted; otherwise its value remains steady, just because there isn't any satisfying sample that could increase the "frequency".
You can check the example in Mathworks' `cdfplot1 documentation to understand the concept of "empirical cdf". Again, only when you observe a sample can you increase the cdf.
If you really want to "get" a smooth curve, either 1) add more points so that the discontinuous line looks smoother, or 2) find any statistical model of whatever you are working on, and plot the analytic function instead.
I'm trying to write a script so that one can put his hand on the screen, click a few points with ginput, and have matlab generate an outline of the persons hand using splines. However, I'm quite unsure how you can have splines connect points that result from your clicks, as they of course are described by some sort of parametrization. How can you use the spline command built into matlab when the points aren't supposed to be connected 'from left to right'?
The code I have so far is not much, it just makes a box and lets you click some points
FigHandle = figure('Position', [15,15, 1500, 1500]);
rectangle('Position',[0,0,40,40])
daspect([1,1,1])
[x,y] = ginput;
So I suppose my question is really what to do with x and y so that you can spline them in such a way that they are connected 'chronologically'. (And, in the end, connecting the last one to the first one)
look into function cscvn
curve = cscvn(points)
returns a parametric variational, or natural, cubic spline curve (in ppform) passing through the given sequence points(:j), j = 1:end.
An excellent example here:
http://www.mathworks.com/help/curvefit/examples/constructing-spline-curves-in-2d-and-3d.html
I've found an alternative for using the cscvn function.
Using a semi-arclength parametrisation, I can create the spline from the arrays x and y as follows:
diffx = diff(x);
diffy = diff(y);
t = zeros(1,length(x)-1);
for n = 1:length(x)-1
t(n+1) = t(n) + sqrt(diffx(n).^2+diffy(n).^2);
end
tj = linspace(t(1),t(end),300);
xj = interp1(t,x,tj,'spline');
yj = interp1(t,y,tj,'spline');
plot(x,y,'b.',xj,yj,'r-')
This creates pretty decent outlines.
What this does is use the fact that a curve in the plane can be approximated by connecting a finite number of points on the curve using line segments to create a polygonal path. Using this we can parametrize the points (x,y) in terms of t. As we only have a few points to create t from, we create more by adding linearly spaced points in between. Using the function interp1, we then find the intermediate values of x and y that correspond to these linearly spaced t, ti.
Here is an example of how to do it using linear interpolation: Interpolating trajectory from unsorted array of 2D points where order matters. This should get you to the same result as plot(x,y).
The idea in that post is to loop through each consecutive pair of points and interpolate between just those points. You might be able to adapt this to work with splines, you need to give it 4 points each time though which could cause problems since they could double back.
To connect the start and end though just do this before interpolating:
x(end+1) = x(1);
y(end+1) = y(1);
So this question is more specifically based off of Newtons form but I'm having trouble writing the script. How would you approach writing this, what I have so far is
length=abs(c-d);
m=length/11;
for k=c:m:d;
fk=func(k);
end
But I can't figure out how to store all the values into vector so I can run Newtons polynomial interpolation.
Do not use length as a variable name because there is a pre-defined function named length.
If you divide the length by 11 and do c:m:d, you create 12 equally-spaced points. Divide it by 10.
When you do c:m:d, you need to make sure c<d.
This is just a simple linear regression problem. You can either manually solve the problem by creating a 11x11 matrix, or you can just use polyfit.
X = c:m:d;
Y = atan(X);
C = polyfit(X,Y,10);
Is the easiest solution, but if you want to know what's happening, you can do this.
A = [X.^10,X.^9,X.^8,X.^7,X.^6,X.^5,X.^4,X.^3,X.^2,X.^1,X,ones(11,1)];
C = A\Y;
You will get the same coefficient vector C. Your desired polynomial is like this
y = C(1)*x^10 + C(2)*x^9 + ......+ C(10)*x + C(11)
The prompt is to develop a program to evaluate convolutions. This is to be done without using MATLAB's built in conv function. Therefore utilizing the Fourier transform, multiplying the two functions together and then inverse Fourier transforming the product. The transform is done by using direct integration. The trapz function is recommended form of integration to accomplish this goal.
I would appreciate ANY feedback on how to improve my code, please thoroughly explain what the improvements and link an reference as to how they work.
Given the code:
t = -5:.1:5;
w = pi;
X = zeros(101,1);
H = zeros(101,1);
Y = zeros(101,1);
y = zeros(101,1);
if t >= 0
x = 0;
h = 0;
else
x = exp((-3.*t)+(-1i*w.*t));
h = exp((-2*t)+(-1i*w.*t));
end
for k=2:101
X(k)=trapz(t(1:k),x(1:k));
H(k)=trapz(t(1:k),h(1:k));
Y = (X.*H)*exp(1i*w.*t);
y(k) = (1/(2*pi))*trapz(t(1:k),Y(1:k));
end
disp (length(x))
disp (length(X))
disp (length(Y))
disp (length(y))
disp (y)
figure(1);
subplot(1,2,1),plot(t,real(y));grid on;
As I do not have enough reputation to directly post images, the actual output and desired output are as follows:
The actual plot is this.
The desired plot is this.
My primary question is this: why is my plot not working?
Secondarily: What is unneeded in this code? What could make this code more efficient?
I won't do your entire homework, but I'll give you a few clues:
Skip the if t < 0 part, it doesn't work. For your exam, try to understand why. If you can't figure it out, come with your best guess and you might get an explanation =)
Try the following instead (no loops or ifs needed:
x = exp((-3.*t)+(-1i*pi.*t)).*(t>0);
And the same for h. Try to understand what .*(t<0) does in this context.
This one: Y = (X.*H)*exp(1i*w.*t); should be outside the loop. Why? Make a guess and you might get guidance if you're wrong.
Also Y is a 101x101 matrix. I guess you want it to be 101x1? You probably need to transform one of the vectors in the expression used to create Y. Before you do, you should figure out the difference between ' and .' (an important difference in this case).
You are using subplot, but only plotting one graph. If you want to graphs in the same plot, use hold on. If you want to plots beside each other, remember to plot the second one too.
And why use w = pi;, when you can just as well use pi in the equations?
First, I should specify that my knowledge of statistics is fairly limited, so please forgive me if my question seems trivial or perhaps doesn't even make sense.
I have data that doesn't appear to be normally distributed. Typically, when I plot confidence intervals, I would use the mean +- 2 standard deviations, but I don't think that is acceptible for a non-uniform distribution. My sample size is currently set to 1000 samples, which would seem like enough to determine if it was a normal distribution or not.
I use Matlab for all my processing, so are there any functions in Matlab that would make it easy to calculate the confidence intervals (say 95%)?
I know there are the 'quantile' and 'prctile' functions, but I'm not sure if that's what I need to use. The function 'mle' also returns confidence intervals for normally distributed data, although you can also supply your own pdf.
Could I use ksdensity to create a pdf for my data, then feed that pdf into the mle function to give me confidence intervals?
Also, how would I go about determining if my data is normally distributed. I mean I can currently tell just by looking at the histogram or pdf from ksdensity, but is there a way to quantitatively measure it?
Thanks!
So there are a couple of questions there. Here are some suggestions
You are right that a mean of 1000 samples should be normally distributed (unless your data is "heavy tailed", which I'm assuming is not the case). to get a 1-alpha-confidence interval for the mean (in your case alpha = 0.05) you can use the 'norminv' function. For example say we wanted a 95% CI for the mean a sample of data X, then we can type
N = 1000; % sample size
X = exprnd(3,N,1); % sample from a non-normal distribution
mu = mean(X); % sample mean (normally distributed)
sig = std(X)/sqrt(N); % sample standard deviation of the mean
alphao2 = .05/2; % alpha over 2
CI = [mu + norminv(alphao2)*sig ,...
mu - norminv(alphao2)*sig ]
CI =
2.9369 3.3126
Testing if a data sample is normally distribution can be done in a lot of ways. One simple method is with a QQ plot. To do this, use 'qqplot(X)' where X is your data sample. If the result is approximately a straight line, the sample is normal. If the result is not a straight line, the sample is not normal.
For example if X = exprnd(3,1000,1) as above, the sample is non-normal and the qqplot is very non-linear:
X = exprnd(3,1000,1);
qqplot(X);
On the other hand if the data is normal the qqplot will give a straight line:
qqplot(randn(1000,1))
You might consider, also, using bootstrapping, with the bootci function.
You may use the method proposed in [1]:
MEDIAN +/- 1.7(1.25R / 1.35SQN)
Where R = Interquartile Range,
SQN = Square Root of N
This is often used in notched box plots, a useful data visualization for non-normal data. If the notches of two medians do not overlap, the medians are, approximately, significantly different at about a 95% confidence level.
[1] McGill, R., J. W. Tukey, and W. A. Larsen. "Variations of Boxplots." The American Statistician. Vol. 32, No. 1, 1978, pp. 12–16.
Are you sure you need confidence intervals or just the 90% range of the random data?
If you need the latter, I suggest you use prctile(). For example, if you have a vector holding independent identically distributed samples of random variables, you can get some useful information by running
y = prcntile(x, [5 50 95])
This will return in [y(1), y(3)] the range where 90% of your samples occur. And in y(2) you get the median of the sample.
Try the following example (using a normally distributed variable):
t = 0:99;
tt = repmat(t, 1000, 1);
x = randn(1000, 100) .* tt + tt; % simple gaussian model with varying mean and variance
y = prctile(x, [5 50 95]);
plot(t, y);
legend('5%','50%','95%')
I have not used Matlab but from my understanding of statistics, if your distribution cannot be assumed to be normal distribution, then you have to take it as Student t distribution and calculate confidence Interval and accuracy.
http://www.stat.yale.edu/Courses/1997-98/101/confint.htm