I am trying to plot something similar to below:
I am using Matlab. I achieved drawing contour plots. However I could not draw the discriminant. Can anyone show a sample Matlab code or give some idea to draw the discriminant?
If you know the probability density function of each of the gaussian for a given point (x,y), lets say its pdf1(x,y) and pdf2(x,y) then you can simply plot the contour line of f(x,y) := pdf1(x,y) > pdf2(x,y). So you define function f to be 1 iff pdf1(x,y)>pdf2(x,y). This way the only contour will be placed along the curve where pdf1(x,y)==pdf2(x,y) which is the decision boundary (discriminant). If you wish to define "nice" function you can do it simply by setting f(x,y) = sgn( pdf1(x,y) - pdf2(x,y) ), and plotting its contour plot will result in exact same discriminant.
Here is how I would solve this problem analytically: you equate these two discriminant functions
g1(x)=x' W1 x + w1' x + w10
g2(x)=x' W2 x + w2' x + w20
g1(x) = g2(x)
==> x' (W2 - W1) x + (w2-w1)'x + w20 - w10
then, I consider W2 - W1 to have be this matrix
W2-W1 = [a b; c d]
which then by expanding vector x=[x1 x2]', we get:
a x1^2 + (b+c) x1 x2 + d x2^2 + (w21-w11) x1 + (w22-w12) x2 + w20-w10 = 0
this is the equation of an ellipse, so you can simplify it into the form below:
(x1 - a0)^2/h + (x2-b0)^2/g = r^2
Or, you can assume that you know the range of x1 for example x1=[-2:0.1:2], and then solve the parabola
Related
I would like to know how to compute:
E[Y | X_i, X_j]
on MATLAB.
Let's say
x = unifrnd(0,1,1000,2) and the model is y = x1 + x2.
suppose that you have scatter plot x1 vs Y and x2 vs Y. With these scatter plots, you compute the regression coefficient (i.e. f1 = ax1 + b and f2 = cx2 + d).
now I want to compute expected value of Y given x1 and x2.
My question is how do I do that ? Thanks in advance
I'm a newbie to matlab. Now I'm trying to plot a 2d quadratic equation. Such as ax^2 + by^2 + cxy + dx + ey + f = 0.
The way I try is to use
x = linspace(-1,2,100);
and transform the equation to y = "use x to represent y"
then plot(x,"use x to represent y")
But since that y has a square(which means y = "square root of something").
So it wont show the negative part of y.
Is there any efficient way to plot such quadratic equation?
You can try doing something like this
a = 1;
b=1;
c=1;
d=1;
e=1;
f=1;
ezplot([num2str(a),'*x.^2 +',num2str(b),' *y.^2 + ',num2str(c),'*x.*y + ',num2str(d),'*x + ',num2str(e),'*y -',num2str(f)],[-10,10,-10,10])
I am trying to offset a plot after plotting using ezplot in x- direction by 10 units.
syms x y;
f1= x^2 + y^2 - (6*y/5)-1/16;
h = ezplot(f1,[-.25 .25 0 1.25]);
I am unable to resolve this issue on my own. Any help or links to relevant documentation would be greatly appreciated.
There's no way you can do that other than manually offsetting the plot yourself in f1 by:
syms x y;
f1 = (x - 10)^2 + y^2 - (6*y/5) - 1/16; % Shift x coordinate to the right by 10
h = ezplot(f1, [10-0.25 10+0.25 0 1.25]); % Note the shift in the x limits
Remember that ezplot's job is to plot an equation for you. You can't simply shift the points when you're done plotting.... actually you can't really do that with any of the MATLAB plot mechanisms because the job is simply to either plot points in arrays or matrices, or in your case for ezplot to plot the equations.
hi I have a question related to full polynomial function of two variables. I can write code to calculate the value of it but I dont know how to plot the contour at level 0. For example: given a full 5th-order polynomial
f(x,y) = theta1 + theta2*x + theta3*y + theta4*x*y + theta5*x^2 + theta6*y^2 + ... + theta20*x^5 + theta21*y^5
I already wrote the code to compute f(x,y) given theta, x and y but how can I plot this function
theta1 + theta2*x + theta3*y + theta4*x*y + theta5*x^2 + theta6*y^2 + ... + theta20*x^5 + theta21*y^5 = 0 ?
thank you!
I'm going to assume that your function takes values element wise. If not, declare a function handle that takes in the variables x and y that represent the values in those respective dimensions:
f = #(x,y) theta1 + theta2*x + theta3*y + theta5*x.^2 + theta6*y.^2 + ... + theta24*x.^5 + theta25*y.^5;
(.... do you need to have that many theta values?). Next, generate a domain of x and y coordinates that you wish to plot. You can do that with meshgrid:
[X,Y] = meshgrid(-10:0.01:10);
This generates a grid of (x,y) coordinates between -10 to 10 for both dimension in steps of 1.
Now, generate your Z coordinates by using the above function:
Z = f(X,Y);
Now that you're finally done, use the contour function with X,Y and Z. Now, if you want to show just the level where f(x,y) = 0, you specifically need to do this:
contour(X,Y,Z,[0 0]);
In general, the fourth element takes in a vector of contour levels but if you want to specifically draw one level, that's what you need to do.
I have been trying to fit a parabola to parts of data where y is positive. I am told, that P1(x1,y1) is the first data point, Pg(xg,yg) is the last, and that the top point is at x=(x1+xg)/2. I have written the following:
x=data(:,1);
y=data(:,2);
filter=y>0;
xp=x(filter);
yp=y(filter);
P1=[xp(1) yp(1)];
Pg=[xp(end) yp(end)];
xT=(xp(1)+xp(end))/2;
A=[1 xp(1) power(xp(1),2) %as the equation is y = a0 + x*a1 + x^2 * a2
1 xp(end) power(xp(end),2)
0 1 2*xT]; % as the top point satisfies dy/dx = a1 + 2*x*a2 = 0
b=[yg(1) yg(end) 0]'; %'
p=A\b;
x_fit=[xp(1):0.1:xp(end)];
y_fit=power(x_fit,2).*p(3)+x_fit.*p(2)+p(1);
figure
plot(xp,yp,'*')
hold on
plot(x_fit,y_fit,'r')
And then I get this parabola which is completely wrong. It doesn't fit the data at all! Can someone please tell me what's wrong with the code?
My parabola
Well, I think the primary problem is some mistake in your calculation. I think you should use three points on the parabola to obtain a system of linear equations. There is no need to calculate the derivative of your function as you do with
dy/dx = a1 + 2*x*a2 = 0
Instead of a point on the derivative you choose another point in your scatter plot, e.g. the maximum: PT = [xp_max yp_max]; and use it for your matrix A and b.
The equation dy/dx = a1 + 2*x*a2 = 0 does not fulfill the basic scheme of your system of linear equations: a0 + a1*x + a2*x^2 = y;
By the way: If you don't have to calculate your parabola necessarily in this way, you can maybe have a look at the Matlab/Octave-function polyfit() which calculates the least squares solution for your problem. This would result in a simple implementation:
p = polyfit(x, y, 2);
y2 = polyval(p, x);
figure(); plot(x, y, '*'); hold on;
plot(x, y2, 'or');