making a trendline in log-log scale scatter graph - matlab

I am coding to make a trendline on my scatter plot.
data=[];
for k=1:100
int=0;
for t=1:100
if k_star_90(k,t)~=0
int=int+k_star_90(k,t);
end
if k_star_90(k,t)==0 && int~=0
data=[data int];
int=0;
end
end
end
intervals = linspace(0, 1, 100);
h1 = histc(data, intervals);
scatter(intervals, h1, 'r');
set(gca,'xscale','log')
set(gca,'yscale','log')
picture of plot result
This is in log-log scale. On this plot, I want to draw y=ax+b(1st order) trendline. I am not sure how to do it.
I will really appreciate your help

I'm not sure I understood your intention correctly, but if you need a trend line you may do something like this
intervals = [0.01 0.02 0.2 0.1 0.3 0.5 0.03 0.4 0.15 0.2 0.2 0.25 1 0.9 0.8 0.8 0.7];
h1 = [70 40 4 20 2 3 60 10 50 40 10 20 1 2 3 1 2] ;
coeffs = polyfit(intervals, h1, 1);
xFitting = 0:0.01:1;
yFitted = polyval(coeffs, xFitting);
scatter(intervals, h1, 'r');
set(gca,'xscale','log');
set(gca,'yscale','log');
grid on;
hold on;
plot(xFitting, yFitted, 'b', 'LineWidth', 2);
hold off;
ylim([1 80]);
xlabel('intervals');
ylabel('h1');
Here is your trend in Log scale:
Og course it does not look like a first order trend. To depict it as a line you need to go back to a normal plot:

Related

How to calculate the area between two curves

Based on the following code:
clear vars;
close all;
x1 = [0 0 0.01 0.09 0.1 0.11 0.2 0.3 0.35 0.50 0.64 0.8 1]
y1 = [0.05 0.10 0.15 0.20 0.25 0.30 0.38 0.42 0.45 0.48 0.52 0.86 1]
x2 = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.9 0.9 1]
y2 = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.9 0.9 1]
plot(x1, y1); hold on;
plot(x2, y2);
I need to calculate the area (green area) between the two curves, for example:
How can I calculate it?
This area is the difference of the two curves integral in the specified domain between each intersection (as mentioned by MBo). Hence, you can find the intersections using InterX and then use trapz to do this:
P = InterX([x1;y1],[x2;y2]);
area = 0;
% for each segment
% each segment is between P(1,i) and P(1, i+1)
% So we can find xsegments with idx = find(x < P(1,i+1) && x > P(1,i)) and [P(1,i) x(idx) P(1,i+1)]
% ...
area = area + abs(trapz(xsegment1i,ysegment1i) - trapz(xsegment2i,ysegment2i));
Since one of the curves is a straight line you can rotate then add up the areas from the new x axis.
The line is at 45 degrees. So the rotation matrix is
cos 45 sin 45
-sin 45 cos 45
Multiply each point in the second curve by that matrix. That gives points with the line as the new x axis. Now use area of the triangle (0.5 * width * height) to add up the areas of the fragments.

2D interpolation with interp2

I want to describe surface flatness of a plane with interp2 function.
Spatial sampling points are as below.
width=[0 500];
length=[0 100 200 300 400 500 600 700 800 900 1000];
and flatness are as below, at width 0 and 500, respectively.
a = [1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1]; % flatness at width 0
b = [-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]; % flatness at width 500
With these values, the surface shape will be like following figure.
I wanted to change this figure into following figure with interp2 function.
Below is my code.
widthq=[0 100 200 300 400 500];
flatness=[a' b'];
flatnessq=interp2(width,length,flatness,widthq,length);
But, not working with one error, 'The input data has inconsistent size.'
Can anyone explain this error and give a way how to interpolate my data with interp2 function?
The first three inputs need all to be of the same size
[W, L] = meshgrid(width, length);
The last two arguments need also to be of same size
[Wq, Lq] = meshgrid(widthq, length);
Then it should work
flatnessq=interp2(W,L,flatness,Wq,Lq);

Plot bar chart with specific range of x axis in MATLAB

I would like to plot a bar chart like below in MATLAB. Any one know which function should I use? Many thanks in advance!
The bar is specified with range in x (some may be wider than others).
There is no line between two bars (red cross in the figure).
X axis is in log scale.
why don't you try to do it with the plot or semilogx function?
x = [0.1 0.18 0.18 0.32 0.32 0.56 0.56 1.0];
y = [30 30 25 25 110 110 80 80];
semilogx(x,y);
and if you want the x ticks like in your figure you can set them on the axis object:
ax = gca;
ax.XTick = unique(x);
ax.XTickLabel = unique(x);

scale part of an axis in matlab

I have the following image which I want to have the depth axis range like below :
(10 9.5 9 8.5 8 7.5 7 6 5 3 2 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0) to show the data between depth 1 and 0 in larger scale, and I have the following code
depths = [10 5 1 0.5 0; 10 5 1 0.5 0] % these are the real depths in meter
contourf(points,depths,RFU15102013_BloomAsMainPoint);
set(gca, 'XTick', points(1) : points(2), 'XTickLabel',{ 'LSB1', 'LSB2'});
ylabel('Depth(m)');
xlabel('Points');
title('Date: 15.10.2013');
this is the image :
how can I do that?
EDIT1
Real Data:
RFU15102013_BloomAsMainPoint = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
points = [1 1 1 1 1; 2 2 2 2 2 ];
depths = [10 5 1 0.5 0; 10 5 1 0.5 0];
As most of a data changes around zero it could be enough to change scaling of Y axis. Here is an example
close all; clear all;
z = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
x = repmat([1; 2], 1, 5);
y = repmat([10 5 1 0.5 0], 2, 1);
% plotting with equally spaced y-s
h = subplot(1,2,1);
contourf(x,y,z);
y2 = log(y + 0.25);
yTicks = linspace(min(y2(1,:)), max(y2(1,:)), 10);
% plotting with logarithmically spaced y-s
h = subplot(1,2,2)
contourf(x,y2,z);
set(h,'YTick', yTicks)
set(h,'YTickLabel', exp(yTicks) - 0.25);
print('-dpng','scaling.png')
The result
This way any monotonic continuous function for axis scaling can be applied.
You could use UIMAGE - UIMAGESC from the mathworks file exchange and set the y values to emphaisize points in 1 to 0 range.

X axis scaling with matlab plotting

My data is sparse therefore when I plot my graph I get the following result
As you can see the first x axis tick starts at 500(s), but most of my data is around 30(s). Can I change the scaling of the x axis?
How about this?
X = [1 3 6 10 25 30 235 678 1248];
Y = [0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.8 0.9];
plot(X,Y,'-b.')
figure
semilogx(X,Y,'-b.')
I see the following output:
If you want to display data from 0 to 30s only you can either plot only those like this:
idcs=Xdata <30; %# find indices where X is less than 30s
plot(Xdata(idcs),Ydata(idcs),'b'); %#plot only these data.
or you can just express XLimits on the figure.
plot(Xdata,Ydata,'b'); %# plot everything
set(gca,XLim,[0 30]); %# limit display on X axis