Interpolate curve - matlab

Interpolate the following data into a smooth curve in the intervalš‘„ āˆˆ [āˆ’2 4.9]. Plot the actual data and the interpolated curve on the same figure.
x| -2 -1.7 -1.4 -1.1 -0.8 -0.5 -0.2 0.1 0.4 0.7 1 1.3
y| 0.1029 0.1174 0.1316 0.1448 0.1566 0.1662 0.1733 0.1775 0.1785 0.1764 0.1711 0.1630
x| 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4 4.3 4.6 4.9
y| 0.1526 0.1402 0.1266 0.1122 0.0977 0.0835 0.0702 0.0579 0.0469 0.0373 0.0291 0.0224
My current code is...but I'm having trouble doing the actual interpolated curve,would it help putting this data into an excel sheet? First time dealing with this.
close all
clear all
clc
x = [-2, -1.7, -1.4, -1.1, -0.8, -0.5, -0.2, 0.1, 0.4, 0.7, 1, 1.3, 1.6, 1.9, 2.2, 2.5, 2.8, 3.1, 3.4, 3.7, 4, 4.3, 4.6, 4.9];
y = [0.1029, 0.1174, 0.1316, 0.1448, 0.1566, 0.1662, 0.1733, 0.1775, 0.1785, 0.1764, 0.1711, 0.1630, 0.1526, 0.1402, 0.1266, 0.1122, 0.0977, 0.0835, 0.0702, 0.0579, 0.0469, 0.0373, 0.0291, 0.0224];
plot(x,y)

This post that I just recently answered pretty much solves your problem. However, I'll throw you a bone and help you out. Use interp1 to do this interpolation for you. What you do is you specify the (x,y) control points, which are those points that are part of your dataset, then you specify a slew of other x points, which have more granularity and have values in between those values of the original x values that you originally specified, and you can see what the output is.
Here are the steps you'd need to perform to do this interpolation:
Define your (x,y) points (which is what you have already done).
Define a grid of x points that are further granular in comparison to what you have. You would specify x values that are in between the original x values that you have. Use linspace to generate a set number of points in between a minimum and maximum... which in your case is -2 and 4.9 respectively. We can hide this away by using min and max on your x points so that you can adapt this to any dataset you want to use.
Use interp1 with the grid of points in step (2) and find the interpolated y points.
Plot the points.
What I'm going to do is do the above procedure, then produce a plot where I'll draw what the original points that were given, as well as a dashed line that shows the interpolated curve. I'm also going to use spline interpolation to smooth out any irregularities.
As such:
%// Define your points - Step #1
x = [-2 -1.7 -1.4 -1.1 -0.8 -0.5 -0.2 0.1 0.4 0.7 1 1.3 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4 4.3 4.6 4.9];
y = [0.1029 0.1174 0.1316 0.1448 0.1566 0.1662 0.1733 0.1775 0.1785 0.1764 0.1711 0.1630 0.1526 0.1402 0.1266 0.1122 0.0977 0.0835 0.0702 0.0579 0.0469 0.0373 0.0291 0.0224];
%// Step #2 - Define grid of x points - Define 100 points in between min and max
xval = linspace(min(x), max(x), 100);
%// Step #3 - Use interp1
yval = interp1(x, y, xval, 'spline');
%// Step #4 - Plot the stuff
plot(x, y, 'bo', xval, yval, 'r--');
%// Throw in a grid too
grid;
This is the plot I get:
Cool?

Related

How can I plot cumulative plots with specific x values?

I was trying to find out, how to plot a cumulative distribution function (cdf) with specific x values but was not successful.
For example, if the dataset is:
x = [2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45];
y = [0.20 0.09 0.15 0.13 0.17 0.04 0.7 0.15]; % (total 1)
the graph shape definitely looks wrong, when I use y = cdfplot(x).
I also plotted the graph with cumsum(y) and x to check the shape and it looks fine, but I would like to know, if there is any code which plots cumulative distribution plots.
There's the stairs function for creating "stairstep graphs", which should be exactly what you want, incorporating your cumsum(y) idea.
Please see the following code snippet. I added two additional points for the start and end of some interval, here [0 ... 25]. Also, your values in y sum up to something larger than 1, so I modified these values, too.
x = [0 2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45 25];
y = [0 0.10 0.09 0.05 0.10 0.14 0.04 0.4 0.08 0];
stairs(x, cumsum(y));
xlim([-1 26]);
ylim([-0.2 1.2]);
That'd be the output (Octave 5.1.0, but also tested with MATLAB Online):
Hope that helps!

Create coordinate point list by using list of coordinate in x and y axis

I have this number:
x = [-4 -3.1 -2.2 -1.3 -0.4 0.5 1.4 2.3 3.2 4.1]
and
y = [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5]
I want to create a list of coordinate in 2 columns [x y] by using those numbers in the simplest cleverest way and not using a loop.
Here is the example of creating it by using loop:
for h=1:11
for j=1:11
gridCoord((h-1)*11+j,1)=x(1,j);
gridCoord((h-1)*11+j,2)=y(1,h);
end
end
** The coordinate moves in x-direction first.
Thanks
You can use meshgrid to achieve what you want
[yy, xx] = meshgrid(y, x);
gridCoord = [xx(:), yy(:)]
NOTE: Ordinarily, you will see meshgrid examples using [xx,yy] = meshgrid(x,y); however, since you specify that you want it to be row major (changing x first), I have reversed the order since by default meshgrid is column major (changes the first argument first). You could achieve the same effect by simply taking the transpose of xx and yy prior to concatenating them into gridCoord.

select the x0 (initial point for x ) in the curve fitting with lsqcurvefit

I wanted to fit an arbitrary function ( (k_plus-k_t*(1-exp(-k_plus/(a*k_t+b*k_d)))-k_d*(exp(-k_plus/(a*k_t+b*k_d)) to my data set. Therefore, I used lsqcurvefit in MATLAB.
The code was as follow:
clc;
clear all;
close all;
%% assign the anon function to a handle
k_plus =[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
K_minus_d = [0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
K_minus_t =[ 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
f1= sprintf('table%02d.txt',1);
data=tblread(f1);
x1=data(:,1);
x1=x1';
F=#(c,xdata)(xdata-K_minus_t*(1-exp(-xdata/(c(1)*K_minus_t+c(2)* K_minus_d)))- K_minus_d*(exp(-xdata/(c(1)*K_minus_t+c(2)* K_minus_d)))
x0 = [0.1 0.1];
[c,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,k_plus,x1)
figure;
hold on
plot(k_plus,x1,'r-', 'LineWidth', 1)
plot(k_plus,F(c,k_plus),'-b*','LineWidth', 1,'MarkerSize', 1)
hold off
grid on;
I wonder how can I select the x0 (initial point for x) because I got different value for C when I change it
If I understand you correctly, you're getting different values for c depending on your choice of the initial x0 and you want advice on how to choose a good initial value.
This is something that is hard to answer without sufficient knowledge about the function you are trying to fit. Is it describing experimental data? If so, you should have some expectations about what x should approximately be.
But it can very well be the case that there is no unique solution for your equation or that the solver gets stuck in a local minimum.

Plot an eye diagram of a signal in MatLab

The matrix X contained in a .mat file represents the acquired signal. The element of place (i, j) of the matrix is the i-th sample of the j-th screen. The sampling frequency is equal to 4 GS/s.
How do I plot the eye diagram relative to the signal contained in X using MatLab?
I tried but I could not draw the eye diagram from the matrix X (see http://ge.tt/8Xq5SYh/v/1?c). Here is the link to the matrix X that I used:
http://ge.tt/8Xq5SYh/v/0
and my MatLab code:
%sampling frequency fs=4 GS/s
rows=4000; %4000 rows (samples) |__ in matrix X
columns=10; %1000 columns (screens) |
%for plot all the graphics in the same window (overlapping)
hold on;
%index of the single row (column for the single column)
row=1:1:100;
t=1:1:100;
for column=1:columns,
%plot
plot(t,X(row, column),'-bo','LineWidth',1, 'MarkerEdgeColor','b', 'MarkerFaceColor','b', 'MarkerSize',2);
end
%axis properties
set(gca,'YTick', [-0.5 -0.45 -0.4 -0.35 -0.3 -0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5]); %soli valori di ascisse da visualizzare
grid on;
set(gca,'GridLineStyle','-');
axis([0 10 -0.5 0.5]);
Someone could try to show me how to do?
maybe the matrix is not correct?
Thanks in advance to anyone who answers
You can simply plot(x,'b'). The plot command will draw a line for every column of x, which corresponds to all the samples of each "screen". The 'b' in the command is just to make every line the same color like a typical eye diagram.

How to plot a grouped bar chart with errors bar as shown in the figure?

I am trying to plot a grouped bar chart like the one in the figure below. I found the errorbar() function, but so far I cannot figure it out how to make it.
Here is my code.
Y = [0.9322225 0.86225 0.8973;
0.8953635 0.862868 0.8099605;
0.7473585 0.675698 0.80484];
bar(Y, 'grouped')
bar(Y, 'BarWidth', 0.5);
set(gca,'XTickLabel',{'', 'ML', '', 'HSV', '', 'NCC'});
Credit: This figure is from the work of Sanin et al. "Shadow Detection: A Survey and Comparative Evaluation of Recent Methods."
![
clc;
close all;
%Suppose you have the following data for five different strains across 4
%different experimental conditions (Conditions A,B,C,D, from left to right)
Strain1_Mean=[1.0 1.5 2.0 1.5]; % data
Strain2_Mean=[2.0 2.5 2.0 2.5]; % data
Strain3_Mean=[1.0 0.5 1.0 3.5]; % data
Strain4_Mean=[1.2 2.5 1.0 1.5]; % data
Strain5_Mean=[2.1 1.3 2.0 2.5]; % data
Strain1_std=[0.15 1.3 0.5 0.4]; %errors in data
Strain2_std=[1.0 1.5 0.2 0.5]; %errors in data
Strain3_std=[1.3 0.4 0.3 0.5]; %errors in data
Strain4_std=[0.13 1.36 0.17 0.45]; %errors in data
Strain5_std=[1.8 1.1 0.25 0.38]; %errors in data
% for asymmetric errors:
barwitherr(cat(3,zeros(4,5),[Strain1_std' Strain2_std' Strain3_std'...
Strain4_std' Strain5_std']),[1 2 3 4],[Strain1_Mean' Strain2_Mean'...
Strain3_Mean' Strain4_Mean' Strain5_Mean'],'LineWidth',2,...
'BarWidth',0.8)
%for symmetric errors:
% barwitherr([Strain1_std' Strain2_std' Strain3_std' Strain4_std'...
% Strain5_std'], [1 2 3 4],[Strain1_Mean' Strain2_Mean'...
% Strain3_Mean' Strain4_Mean' Strain5_Mean'])
set(gca,'XTickLabel',{'Group A','Group B','Group C','Group D'})
legend('Parameter 1','Parameter 2','Parameter 3','Parameter 4',...
'Parameter 5')
ylabel('Value')
grid on
colormap summer
if you want to add more data sets, change zeros(4,5) to zeros(4,...) 6,7,8...
I don't know how implement circles and squares like in example, if somebody can do it, you are welcome to improve :)][3]
I'm not 100% how to replicate that graph but maybe this will help you get started.
I found this on the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/30639-bar-chart-with-error-bars/content/barwitherr.m, give that a try in for plotting a bar chart with error bars. It allows for asymmetrical error bars and I reckon that if you want to make the markers different (like the squares and circles in your example) then you need to alter this line in the code in the link: errorbar(mean(x,1),values(xOrder,col),lowerErrors(xOrder,col), upperErrors(xOrder, col), '.k')
to something like:
errorbar(mean(x,1),values(xOrder,col),lowerErrors(xOrder,col), 'ok');
errorbar(mean(x,1),values(xOrder,col), upperErrors(xOrder, col), '*k')
Also if you want all three groups to have labels try set(gca,'XTickLabel',{'ML', 'HSV', 'NCC'});
Also check out colormap(summer) and help legend