I have two realizations for (X,Y)
(0, 200)
(23, 700)
I want to find the value of Y at X1=12.5 with matlab with linear interpolation
Would interp1 work? but haven't figured out how to use the arguments
Thank you
This does the thing:
x = [0,23];
y = [200,700];
xq = 12.5;
yq = interp1(x,y,xq)
%visualization
plot(x,y);hold on;
stem(xq,yq,'r')
Related
The stairstep-look of the graph is unintentional. When I plot the same-sized vectors Arb and V (plot (Arb, V, 'r')) I get the following graph:
enter image description here
To make it smoother, I tried using 1-D data interpolation, interp1, as follows:
xq = 0:0.001:max(Arb);
Vq = interp1 (Arb, V, xq);
plot (xq, Vq);
However, I get this error message:
Error using interp1>reshapeAndSortXandV (line 416)
X must be a vector.
Error in interp1 (line 92)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2})
interp1 will use linear interpolation on your data so it won't help you much. One thing you can try is to use a cubic spline with interp1 but again given the nature of the data I don't think it will help much. e.g.
Alternative 1. Cubic Spline
xq = 0:0.001:max(Arb);
Vq = interp1 (Arb, V, xq, 'spline');
plot (xq, Vq);
Alternative 2. Polynomial fit
Another alternative you can try is, polynomial interpolation using polyfit. e.g.
p = polifit(Arb, V, 2); % I think a 2nd order polynomial should do
xq = 0:0.001:max(Arb);
Vq = polyval(p, xq);
plot (xq, Vq);
Alternative 3. Alpha-beta filter
Last but not least, another thing to try is to use an smoothing alpha-beta filter on your V data. One possible implementation can be:
% x is the input data
% a is the "smoothing" factor from [0, 1]
% a = 0 full smoothing
% a = 1 no smoothing
function xflt = alphaBeta(x, a)
xflt = zeros(1,length(x));
xflt(1) = x(1);
a = max(min(a, 1), 0); % Bound coefficient between 0 and 1;
for n = 2:1:length(x);
xflt(n) = a * x(n) + (1 - a) * xflt(n-1);
end
end
Usage:
plot (Arb, alphaBeta(V, 0.65), 'r')
I'm trying to plot the caculated Eigenvectors of 2D dataset, here the script I've wrote for that:
clear ;
s = [2 2]
set = randn(200,1);
x = normrnd(s(1).*set,1)+3
x = zscore(x) % Standardize
y = normrnd(s(1).*set,1)+2
y= zscore(y)%Standardize
x_0 = mean(x)
y_0 = mean (y)
c = linspace(1,100,length(x)); % color
scatter(x,y,100,c,'filled')
xlabel('1st Feature : x')
ylabel('2nd Feature : y')
title('2D_dataset')
grid on
% gettign the covariance matrix
covariance = cov([x,y])
% getting the eigenvalues and the eigenwert
[eigen_vector, eigen_values] = eig(covariance)
eigen_value_1 = eigen_values(1,1)
eigen_vector_1 =eigen_vector(:,1)
eigen_value_2 = eigen_values(2,2)
eigen_vector_2 =eigen_vector(:,2)
% ploting the eigenvectors !
hold on
x_0 = repmat(x_0,size(eigen_vector_2,1),1);
y_0 = repmat(y_0,size(eigen_vector_1,1),1);
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1),'-r','LineWidth',5)
and here is the result I'm getting:
I've double checked the math, the values are correct, but the plot is a mess !
Any idea what I'm missing in the plot of the 2 vectors ?
thanks in advance !
In your code, replace this part:
covariance = cov([x,y])
% getting the eigenvalues and the eigenwert
[eigen_vector, eigen_values] = eig(covariance)
eigen_value_1 = eigen_values(1,1)
eigen_vector_1 =eigen_vector(:,1)
eigen_value_2 = eigen_values(2,2)
eigen_vector_2 =eigen_vector(:,2)
% ploting the eigenvectors !
hold on
x_0 = repmat(x_0,size(eigen_vector_2,1),1);
y_0 = repmat(y_0,size(eigen_vector_1,1),1);
quiver(x_0, y_0,eigen_vector_2*(eigen_value_2),eigen_vector_1*(eigen_value_1),'-r','LineWidth',5)
with the following code:
covariance = cov([x,y]);
[eigen_vector, eigen_values] = eig(covariance);
eigen_vector_1 = eigen_vector(:,1);
eigen_vector_2 = eigen_vector(:,2);
d = sqrt(diag(eigen_values));
hold on;
quiver(x_0,y_0,eigen_vector(1,2),eigen_vector(2,2),d(2),'k','LineWidth',5);
quiver(x_0,y_0,eigen_vector(1,1),eigen_vector(2,1),d(1),'r','LineWidth',5);
hold off;
Does this produces what you are looking for? It looks much more coherent to me...
You are plotting the two components of one eigenvector as the x component of two vectors, and the other eigenvector as the y components.
[eigen_vector, eigen_values] = eig(covariance)
eigen_x = eigen_vector(1,:);
eigen_y = eigen_vector(2,:);
scale = diag(eigen_vector)'; % not sure what the output orientation is
% ploting the eigenvectors !
hold on
x_0 = repmat(x_0,size(eigen_vector_2,1),1);
y_0 = repmat(y_0,size(eigen_vector_1,1),1);
quiver(x_0, y_0,eigen_x.*scale,eigen_y.*scale,'-r')
Actually, because they are orthonormal, slicing the matrix the other way does not change much. But your scaling is changing the angles of the vectors, not just thier length, because of what I mention above.
I am trying to extend my polyfit line of best fit in Matlab. I want it to extend to y = 200 and x = 64. What would be the best way of doing this?
Cheers Tom
Link to image of polyfit through data
Plot your polyval over a different range. Something like that should work:
% Make up data
x = 1:100;
y = -0.03.*x.^2 + 2*x + 10*randn(size(x));
x2 = -50:150;
P = polyfit(x,y,2);
plot(x,y,'o',x2,polyval(P,x2),'--');
grid on;
I was trying to plot a regression curve:
coef_fit = polyfit(norm_dist,norm_time,7);
y_fit = polyval(coef_fit,xlim);
plot(xlim,y_fit,'r');
But it is always plotting a line respective of the order I pass.
The problem is that the x values you are using are the output ot xlim, which is a length-2 vector. You need to define an x vector with more values:
norm_dist = sort(5*randn(1,50) + (1:50)); %// example x values
norm_time = 5*randn(1,50) + (1:50).^2; %// example y values
x = linspace(min(norm_dist), max(norm_dist), 200); %// define x values for plot
coef_fit = polyfit(norm_dist,norm_time,7);
y_fit = polyval(coef_fit,x);
plot(x,y_fit,'r');
hold on
plot(norm_dist, norm_time, 'b.') %// plot original points for comparison
I am beginner in matlab programming, so i wrote this little programm to see it in action, and now I have a little problem because I am not sure why it is not working.
x = zeros(50);
squared = zeros(50);
cubed = zeros(50);
for num = 1:50
x(num) = num;
squared(num) = num^2;
cubed(num) = num^3;
end
% calculate the mean
mean_cubed = mean(cubed);
% clear screen and hold the plot
clf;
hold on
plot(x, squared);
plot(x, cubed);
plot([0, 50], [mean_cubed, mean_cubed]);
hold off
The main program is when i start the program i get a error:
Error using plot
Vectors must be the same lengths.
Error in basic_mathlab_plotting_2 (line 20)
plot([0, limit], [mean_cubed, mean_cubed]);
I think the size of vector are the same, so i dont know what is wrong.
Thanks!!!
In the first lines, you probably meant
x = zeros(1,50);
squared = zeros(1,50);
cubed = zeros(1,50);
Note that zeros(50) is equivalent to zeros(50,50) and so it returns a 50x50 matrix.
In addition, those lines and the for loop could be replaced by
x = 1:50;
squared = x.^2;
cubed = x.^3;
This applies the important concept of vectorization, by using the element-wise power operation.