Plot rain attenuation model in MATLAB - matlab

I would like to plot the rain attenuation model based on ITU-R P.838-3 Recommendation as shown in the graph Rain Attenuation Graph
I tried to used "rainlp" command in MATLAB but I could not get the same graph.
clear all;clc
freq=[1:1000]*1e9;
Rr=[0.25, 1.25, 5, 25, 50, 100, 150];
for i=1:7
L=rainpl(10000,freq,Rr(i));
plot(freq/1e9,L)
hold on
grid
end
Any ideas how to get this graph by MATLAB?
Thanks in Advance

Related

How to plot unevenly spaced data on Matlab / Origin?

I want to plot my following data:
x-axis: [0,10,50,100,500,1000,1500]
y-axis: [75.6,78,78.2,81.8,84.7,85.2,86.3]
As seen above, the data on the x-axis are unevenly spaced. When I plot the above data linearly using origin, I get:
I got the similar graph on Matlab too. Notice that most of the Amp data lie for x<500. I want to plot the graph such that the whole output (y-axis) become clearly visible. For this, I tried using the Logarithmic plot. I changed the x-axis into logarithmic in Matlab as follows:
set(gca, 'XScale','log');
In Origin, we can use GUI to change the x-axis to logarithmic. The obtained graphs are as follows:
The obtained graphs are still not good. Any ideas, please!
Thank you very much.
https://www.mathworks.com/help/matlab/ref/semilogx.html
x = [0,10,50,100,500,1000,1500];
y = [75.6,78,78.2,81.8,84.7,85.2,86.3];
semilogx(x,y,'.-', 'markersize', 15);
set(gca,'XTick',x);
set(gca,'XTickLabelRotation',45);
x = [0,10,50,100,500,1000,1500];
y = [75.6,78,78.2,81.8,84.7,85.2,86.3];
y2 = [80,84,85,86,89,90,92];
semilogx(x+1,y,'.-', 'markersize', 15);
set(gca,'XTick',x);
set(gca,'XTickLabelRotation',45);
hold on;
semilogx(x+1,y2,'.-', 'markersize', 15);
hold off;
grid on;
legend('y1','y');

MATLAB Histogram Problems

I need to calculate the probability density of the function 1/2-x where x is a randomly generated number between 0 and 1.
My code looks like this:
n=10^6;
b=10^2;
x=(1./(2-(rand(1,n))));
a=histfit(x,b,'kernel'); % Requires Statistics and Machine Learning Toolbox
xlim([0.5 1.0])
And I get a decent graph that looks like this:
As it may be apparent, there are a couple of problems with this:
MATLAB draws a fit that differs from my histogram, because it counts in the empty space outside the [0.5 1] range of the function as well. This results in a distorted fit towards the edges. (The reason you don't see said empty space is because I entered an xlim there)
I don't know how I could divide every value in the Y-axis by 10^6, which would give me my probability density.
Thanks in advance.
To solve both of your problems, I suggest using hist (Note if you have a version above 2010b, you should use histogram instead) instead of histfit to first get the values of your histogram and then doing a regression and plotting them:
n=10^6;
b=10^2;
x=(1./(2-(rand(1,n))));
[counts,centers]=hist(x,b);
density = counts./trapz(centers, counts);
%// Thanks to #Arpi for this correction
polyFitting = polyfit(centers,density,3)
polyPlot = polyval(polyFitting,centers)
figure
bar(centers,density)
hold on
plot(centers,polyPlot,'r','LineWidth',3)
You can also up the resolution by adjusting b, which is set to 100 currently. Also try different regressions to see which one you prefer.
1. Better result can be obtained by using ksdensity and specifying the support of the distribution.
2. By using hist you have access to the counts and centers, thus the normalization to get density is straightforward.
Code to demonstrate the suggestions:
rng(125)
n=10^6;
b=10^2;
x=(1./(2-(rand(1,n))));
subplot(1,2,1)
a = histfit(x,b,'kernel');
title('Original')
xlim([0.5 1.0])
[f,c] = hist(x,b);
% normalization to get density
f = f/trapz(c,f);
% kernel density
pts = linspace(0.5, 1, 100);
[fk,xk] = ksdensity(x, pts, 'Support', [0.5, 1]);
subplot(1,2,2)
bar(c,f)
hold on
plot(xk,fk, 'red', 'LineWidth', 2)
title('Improved')
xlim([0.5 1.0])
Comparing the results:
EDIT: If you do not like the endings:
pts = linspace(0.5, 1, 500);
[fk,xk] = ksdensity(x, pts, 'Support', [0.5, 1]);
bar(c,f)
hold on
plot(xk(2:end-1),fk(2:end-1), 'red', 'LineWidth', 2)
title('Improved_2')
xlim([0.5 1.0])

How to make a confusion matrix plot with percentage values in Matlab

I would like to plot a 2x2 confusion matrix in Matlab in such a way that the plot would have percentage values in each of the grid boxes in this way:
In summary I'm asking for how to make a plot like the one above when you're given the percentage values in a matrix:
V = [0.15, 0.30; 0.05, 0.50]
Is it possible to make a plot like that in Matlab? I did think of drawing the vertical lines and then plotting some text into spesific coordinates, but is this the only way to do this?
Let me show you how you could do it. I did it just for fun!
You can change the size of your input matrix as you wish.
A=[0.3 0.2 ; 0.1 0.7];
sA=size(A);
Aplot=rot90(A,3);
figure;hold on
rectangle('Position',[0,0,sA(2),sA(1)],'Facecolor',[1 1 1],'edgecolor','none')
for ii=0:sA(1)
plot([0 sA(2)], [ii ii],'k','Linewidth',3)
end
for ii=0:sA(2)
plot([ii ii],[0 sA(1)],'k','Linewidth',3)
end
for ii=1:sA(2)
for jj=1:sA(1)
text((ii-1)+0.35,(jj-1)+0.5,strcat(num2str(Aplot(ii,jj)*100),' %'),'fontsize',30)
end
end
margin=0.05;
axis([0-margin sA(2)+margin 0-margin sA(1)+margin])
axis off
With Matlab 2014b and the new graphic engine, smoother option:

Octave smooth curve graph

I want to draw a smooth curve fit graph in Octave on my data instead of sharp line just like scatter with smooth line in Excel.
My data as as follows:
x = [1, 2 , 3];
y = [53, 48, 31];
y1 = [89, 51, 49];
When i do a normal plot it gives linear graph with sharp edges on a point. I want smooth graph. How can i plot that in Octave. Thank you
Well, you only have 3 data points, so of course your graph is not going to be "smooth". Get (much) more data points, and you will have a "smooth" graph, provided your data is "smooth".
You can also fit a "smooth" curve to your data, but that's a different question entirely, plus you will also need more data points for a meaningful fit.
Very old, but might still be useful. Do e.g.
x = [1, 2 , 3];
y = [53, 48, 31];
y1 = [89, 51, 49];
xspline = linspace(min(x),max(x),100);
yspline = interp1(x,y,xspline,"pchip");
yspline1 = interp1(x,y1,xspline,"pchip");
plot(xspline,yspline,xspline,yspline1);
You can find different interpolation "smoothing" methods by entering
help interp1
in the example above
"pchip"
Piecewise cubic Hermite interpolating
polynomial--shape-preserving interpolation with smooth first
derivative.
is used.

Axis adjusting of an autocorrelation plot

I'm trying to create my autocorrelation function, because I don't have the Econometrics toolbox and I have succesfully created my function, but I can't get the x-axis in the stem-plot correct. The following picture will higlight my point:
As you can see in the picture there should be 4 where there is now 5. How can adjust the x-axis so that 5 will "jump" one step forward and so on...
Thank you for any help =)
EDIT:
Here is the code which generates this plot:
h = stem(corrVals, 'fill');
set(h,'MarkerFaceColor','red')
axis([1 25 -0.5 1.5])
Variable corrVals is a vector consisting of the autocorrelation values.
corrVals(1) is the autocorrelation at lag 0, corrVals(2) is the autocorrelation at lag 1 and so on...
Specify the x-values for stem:
h = stem(0:length(corrVals) - 1, corrVals, 'fill');