Matlab - How to plot 2d Quadratic equation - matlab

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

Related

helix-shaped movement of an object in Matlab

I have found this equation in a paper which represents a helix-shaped movement of an object:
When I plotted the S vector in Matlab I got a different result, not helix shape.
Where is the problem, is it in the equation or in the code?
l is a random number in [-1,1]
r is a random vector in [0,1]
b is a constant for defining the shape of the logarithmic spiral.
Matlab code:
dim =3;
Max_iter =10;
X_star=zeros(1,dim);
ub = 100;
lb = -100;
X=rand(1,dim).*(ub-lb)+lb;
S = [];
t=0;
while t<Max_iter
a=-1+t*((-1)/Max_iter);
r=rand();
b=1;
l=(a-1)*rand + 1;
for j=1:size(X,2)
D= abs(X_star(j) - X(1,j));
X(1,j)= D * exp(b.*l).* cos(l.*2*pi) + X_star(j);
end
X_star=X(1,:);
S = [S X];
plot(S);
t = t+1;
end
that equations does not look like helix at all. Lets do this for 3D. First some definitions:
p0,p1 - centers of start and end of the helix 3D points
r - scalar radius of the helix
m - scalar number of screws (if not integer than the screws will not start and end at the same angular position)
We first need TBN vectors:
n = p1-p0
t = (1,0,0)
if (|dot(n/|n|,t)|>0.9) t = (0,1,0)
b = cross(n,t)
b = r*b / |b|
t = cross(b,n)
t = r*t / |t|
And now we can do the helix using TBN as basis vectors:
// x' y' z'
p(a) = p0 + t*cos(2*Pi*m*a) + b*sin(2*Pi*m*a) + n*a
Where a = <0.0,1.0> is the parameter which point on helix you want. If you move out of this interval you will extrapolate the helix before p0 and after p1. You can consider a is the time...
As you can see the equation is just 2D parametric circle (x',y') + linear movement (z') and transformed by basis vectors to p0,p1 direction and position.
If you need equations/code for the dot,cross and abs value of 3D vectors see:
Understanding 4x4 homogenous transform matrices
They are at the bottom of that answer of mine.

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

matlab - Full Multivariate polynomial function plot

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.

MATLAB polynomial fit selective powers

I have 2 vectors x and y to which I want to fit a polynomial as y = f(x) in MATLAB.
I could have used polyfit. However, I want to fit only selective power terms of the polynomial. For example, y = f(x) = a*x^3 + b*x + c. Notice that I don't have the x^2 term in there. Is there any built-in function in MATLAB to achieve this?
I am not sure if simply ignoring the coefficient that MATLAB gives for x^2 is same as fitting the polynomial without x^2 term.
If you don't have the curve fitting tool box (see #thewaywewalk's comment), or anyway, it is easy to use mldivide:
x=rand(10,1); % your x data
y=5*x.^3+2*x+7+rand(10,1)*.01; % some y data with noise
[x.^3 x ones(size(x))]\y % least squares solve to y = a*x^3 + b*x + c
gives
ans =
4.9799
2.0211
6.9980
Note that "simply ignoring the coefficient that MATLAB gives for x^2" is definitely not the "same as fitting the polynomial without x^2 term".
It sounds like you have the fitting toolbox and want to just remove a possible coefficient. If that's the case, here's a way to do it.
%What is the degree of the polynomial (cubic)
polyDegree = 3;
%What powers do you want to skip (x^2 and x)
skipPowers = [2, 1];
%This sets up the options
opts = fitoptions( 'Method', 'LinearLeastSquares' );
%All coefficients of degrees not specified between x^n and x^0 can have any value
opts.Lower = -inf(1, polyDegree + 1);
opts.Upper = inf(1, polyDegree + 1);
%The coefficients you want to skip have a range from 0 to 0.
opts.Lower(polyDegree + 1 - skipPowers) = 0;
opts.Upper(polyDegree + 1 - skipPowers) = 0;
%Do the fit using the specified polynomial degree.
[fitresult, gof] = fit( x, y, ['poly', num2str(polyDegree)] , opts );
In recent Matlab versions you can do it easily with a GUI by selecting the APPS tab and then the Curve Fitting Tool.
For example, I define:
x = 1:10;
y = x + randn(1,10);
I then select those variables as X data, Y data in the tool and I choose a custom equation defined as a*x^3 + b*x + c. The results are:

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