matlab - Full Multivariate polynomial function plot - matlab

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.

Related

How to create a third degree polynomial in x and y in matlab?

I was going through matlab docs and it's mentioned that a polynomial can be created and evaluated as mentioned in this doc
However, I would like to create a third degree polynomial in x and y. Example: f(x, y) = x^3 + y^3 + 3x^2y + 3xy^2.
How do we create a third degree polynomial in x and y, like the example above?
The link you post is for creating a vector of coefficients for a polynomial of one variable. For two variables like you have, could you just use a function handle? E.g.,
f = #(x, y) x.^3 + y.^3 + 3*x.^2.*y + 3*x.*y.^2

Matlab - How to plot 2d Quadratic equation

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

Finding minima and maxima of a surface in MATLAB

I have just started using MATLAB. I want to find the minima and maxima of this 2nd degree polynomial surface.
f(x,y) = 4490 + 8902*x + 1.385e+05*y + 9.497*x^2 + -9928*x*y + 1.481e+05*y^2;
I know how to calculate minima and maxima for a degree 1 function but I wasn't able to get any lead regarding higher degree functions. Any suggestions would be appreciated.
Evaluate the function for x and all y and find the min/max of that matrix:
x1D = 1:100;
y1D = 1:100;
[x,y] = meshgrid(x1D,y1D);
f = 4490 + 8902*x + 1.385e+05*y + 9.497*x.^2 + -9928.*x.*y + 1.481e+05*y.^2;
[minV, minL] = min(f(:));
[maxV, maxL] = max(f(:));
[minX, minY] = ind2sub(size(f),minL);
[maxX, maxY] = ind2sub(size(f),maxL);
Lets first define a range for x,y which you want to find minima.Then make a mesh grid which is basically a matrix containing those x and y as coordinates. Then evaluate the function at all those points as z which is also a matrix. Each member of z is at the position similar to x and y as input. Then find the location of minimum value for z matrix. The last 2 command should give you the position coordinates where the minima happened.
x=1:100;
y=1:100;
[X,Y]=meshgrid(x,y); %
z = 4490 + X.*8902 + Y.*1.385e+05 +X.^2*9.497 X.*Y.*9928+Y.^2*1.48e+05;
[X,Y]=meshgrid(x,y);
surf(x,y,z) % plotting the curve
[Row,Col]=find(z==min(min(z)))
x(Row) % x coordinate
y(Col) % y coordinate

Drawing decision boundary of two multivariate gaussian

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

MATLAB - Using for loops find all the combinations of x^2 + y

I have two for loops like this:
for x = 1:1:15
for y = 1:1:15
values(x,y) = x^2 + y
end
end
This allows me to calculate x^2 + y for every combination of x and y if they are integers.
However, what if I want to calculate x^2 + y for decimals as well?
So something like this:
for x = 0:0.1:15
for y = 0:0.1:15
????? = x^2 + y
end
end
Could anyone help me find a method that can calculate all the possibilities of x^2 + y if x and y are decimals so cannot be used as index references anymore?
use meshgrid, matlab's built in rectangular grid in 2-D and there's no need to loop!
[y x]=meshgrid(0:0.1:15)
values=x.^2+y
to visualize this:
imagesc(values);
title('values=x^2+y'); axis square
xlabel('x'); ylabel('y'); colorbar;
axis xy;
set(gca,'XTick',1:10:151,'YTick',1:10:151);
set(gca,'XTickLabel',0:1:15,'YTickLabel',0:1:15);
EDIT:
mdgrid is also fine the only thing to note is that [y x]=meshgrid... is the same [x y]=ndgird...
Use:
[x y] = ndgrid(0:0.1:15);
values = x.^2 + y;
Issues with the other answers:
#inigo's answer will change the order of x and y compared to your initial example (by using meshgrid rather than ndgrid.
#NominSim's answer has to go to extra effort get d_x from x
#mecid's answer has to count columns and rows separately (also there is no ++ operator in MATLAB). If I was to go down #mecid's route I would use the following.
x = 0:.1:15;
y = 0:.1:15;
values = zeros(numel(x),numel(y));
for xnum = 1:numel(x)
for ynum = 1:numel(y)
values(xnum,ynum) = x(xnum)^2 + y(ynum);
end
end
Since it generated some discussion, from the documentation (within MATLAB, not in the online documentation) on the difference between meshgrid and ndgrid:
meshgrid is like ndgrid except that the order of the first two input and output arguments are switched (i.e., [X,Y,Z] = meshgrid(x,y,z) produces the same result as [Y,X,Z] = ndgrid(y,x,z)) ... meshgrid is also limited to 2D or 3D.
for x =1:0.1:15
for y=1:0.1:15
values(x*10-10, y*10-10) =x^2+y;
end
end
Why not loop on integers from 1 to 151 then calculate the decimal to be used? Then you can still use index references.
i.e.
for x = 1:1:151
for y = 1:1:151
d_x = x / 10.0 - 0.1
d_y = y / 10.0 - 0.1
values(x,y) = d_x^2 + d_y
end
end
(Forgive me if my syntax is slightly off, haven't used MATLAB in a while).