2D interpolation with interp2 - matlab

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);

Related

How to convert actual frequency to normalised frequency?

I have two data sets ( which is 80*80 matrix) with relative risk ranging from -1.5 to +1.5.
I want to plot these two data sets as ine normalised frequency distribution plot.
How can I convert the actual frequency to normalised one( range 0 to 1)
So what I actually want is: if my frequency ranges from 0 to 200
( i want 0 to assign value of 0, 20 as 0.1, 40 as 0.2 , 60 as 0.3 .... 200 as 1)
So if the value of relative risk is -1 and actual frequency at this risk is 60 for dataset one and 80 for data set two, so in that case, I want -1 ( which is relative risk value) to show frequency as 0.3 and 0.4 for dataset one and two respectively after normalization. I need it in the same graph so that I could figure out the difference between two data sets.
This what I want my graphs axis to be:
Y-axis: normalized frequency for the following groups (ranging from 0 to 1)
X-axis; RR classes - <-1.5, -1.5 to -1.25, -1.25 to -1, -1 to -.75, -0.75 to -0.5, -0.5 to -0.25, -0.25 to 0, 0 to 0.25, 0.25 to 0.5, 0.5 to 0.75, 0.75 to 1, 1 to 1.25, 1.25 to 1.5 and >1.5
From the matlab documentation:
% assuming a,b are your frequencies
figure; hold on;
histogram(a,'Normalization','probability');
histogram(b,'Normalization','probability');

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.

How to plot two stem plots and two box plots in one figure in MATLAB?

I have two vectors
A=[0.1 0.1 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.5 0.5]
B=[0.7 0.7 0.8 0.8 0.9 0.9 0.9 0.9 1 1 1]
How to plot their stem plots and box plots at the same time?
The y-axis should be the probability of the stem plots.
What I am looking for is something like this.
Drawing the box-plots can be accomplished with
boxplot([A B], [ones(size(A)) 2*ones(size(B))], ...
'orientation', 'horizontal', 'positions', [1 1]);
After which you can add the stem plots with
hold on
stem(xa, ya);
stem(xb, yb);
where I'm not sure exactly what you are asking for for x and y.

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