I typically use something like this
[X, Y] = ndgrid(-10:10, -10:10);
pdfVals = zeros(size(X));
pdfFunc = @(x,y) ... % function to evaluate the pdf
for i=1:size(X,1)
for j=1:size(X,2)
pdfVals(i,j) = pdfFunc(i,j);
end
end
figure(1); clf
contour(X, Y, pdfVals);
[edit]
I may have misunderstood what you have available. Given just experimental data you can show an approximate pdf with the histogram tool.
% Generate random data
nData = 1e5;
data = zeros(2,nData);
m1 = 0; m2 = 1;
s1 = 1; s2 = 2;
for i=1:nData
d1 = m1+s1*randn;
d2 = m2+s2*randn;
data(:,i) = [d1; d2];
end
% hist3 will bin the data
xi = linspace(min(data(1,:)), max(data(1,:)), 50);
yi = linspace(min(data(2,:)), max(data(2,:)), 50);
hst = hist3(data',{xi yi});
% normalize the histogram data
dx = xi(2)-xi(1);
dy = yi(2)-yi(1);
area = dx*dy;
pdfData = hst/sum(sum(hst))/area;
% plot pdf
figure(2); clf
contour(xi,yi,pdfData);
Maybe this code can help you:
function mse
x = linspace(0,10,100);
y = sin(x)+x-(x/5).^2;
hold off
plot(x,y)
yMax = max(y);
xMax = x(find(y==yMax));
hold on
plot(xMax,yMax,'*g')
y90 = yMax*0.9;
plot(x,y90,'-k')
for i=2:length(x)
if y(i-1)<=y90 && y(i)>=y90 || y(i-1)>=y90 && y(i)<=y90
plot(x(i),y(i),'og')
plot(x(i-1),y(i-1),'og')
end
end
y50 = yMax*0.5;
plot(x,y50,'-k')
for i=2:length(x)
if y(i-1)<=y50 && y(i)>=y50 || y(i-1)>=y50 && y(i)<=y50
plot(x(i),y(i),'og')
plot(x(i-1),y(i-1),'og')
end
end
end
Here $x,y$ are my data. Finding the maximum is easy, but for the 90% or 50% values I iterated through the data. As we do not have continous data we check, whether the 90% or 50% values lies between to points. The result is shown below. If you only want to highlight a single point instead of the two you could perform some (linear) interpolation.
EDIT: This would highlight all points that match the X% criteria. If you only want to highlight the closest points to the maximum, you could change the loop to something like
for i=find(y==ymax):length(x)
Which finds the point on the right and for the left side
for i=find(y==ymax):-1:length(x)
To mark only one point you could use a break command.
EDIT2:
The following code performs the same thing as the for loops, but it is shorter.
y90 = yMax*0.9;
xId= find(diff(sign(y-y90))~=0);
plot(x(xId),y(xId),'*k')
plot(x(xId+1),y(xId+1),'*k')
EDIT3:
For your date you need to interpolate between the two values. Replace the loop with something like
for i=2:length(x)
if y(i-1)<=y90 && y(i)>=y90 || y(i-1)>=y90 && y(i)<=y90
plot(x(i),y(i),'og')
plot(x(i-1),y(i-1),'og')
xl = x(i-1);
yl = y(i-1);
xr = x(i);
yr = y(i);
xMid = (y90-yl)*(xr-xl)/(yr-yl)+xl;
plot(xMid,y90,'or');
end
end
Best Answer
I'm going to give two answers.
Answer 1: If you already know $a$, then $c = \bar{y} - a\bar{x}$, where $\bar{x}$ and $\bar{y}$ are the means of the $x$ and $y$ values, respectively. (This is actually in that pile of formulas in the link provided by Yuval Filmus.)
Answer 2: At least in my version of Matlab, polyfit gives both $a$ and $c$. For example, if you generate a set of data via (I'm modifying the example given in the Matlab help)
and then call polyfit with $n=1$ (for a linear curve fit, or linear regression)
The output is
which means your linear equation is $y = 0.3554x + 0.3191$.