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.
Related
I wish to plot implicit functions in MATLAB. Like x^3 + xy + y^2 = 36 , equations which cannot be made into simple parametric form. Is there any simple method ?
Here are a couple of options...
Using ezplot (or fplot recommended in newer versions):
The easiest solution is to use the function ezplot:
ezplot('x.^3 + x.*y + y.^2 - 36', [-10 10 -10 10]);
Which gives you the following plot:
Using contour:
Another option is to generate a set of points where you will evaluate the function f(x,y) = x^3 + x*y + y^2 and then use the function contour to plot contour lines where f(x,y) is equal to 36:
[x, y] = meshgrid(-10:0.1:10); % Create a mesh of x and y points
f = x.^3+x.*y+y.^2; % Evaluate f at those points
contour(x, y, f, [36 36], 'b'); % Generate the contour plot
xlabel('x'); % Add an x label
ylabel('y'); % Add a y label
title('x^3 + x y + y^2 = 36'); % Add a title
The above will give you a plot nearly identical to the one generated by ezplot:
In case you want to plot an implicit surface, for example a Horned cube, you can do something like the following.
The idea is to calculate all values of the function (even if they don't equal to zero) and then create an isosurface that will define your equality. In this example the implicit function equals to zero.
fun=#(x,y,z)(1-x.^8-3.*y.^8-2.*z.^8+5.*x.^4.*z.^2.*y.^2+3.*y.^4.*x.^2.*z.^2) ;
[X,Y,Z]=meshgrid(-2:0.1:2,-2:0.1:2,-2:0.1:2);
val=fun(X,Y,Z);
fv=isosurface(X,Y,Z,val,0);
p = patch(fv);
isonormals(X,Y,Z,val,p)
set(p,'FaceColor' , 'red');
set(p,'EdgeColor' , 'none');
daspect([1,1,1])
view(3); axis tight
camlight
lighting phong
axis off
Additionally there is a Matlab File Exchange submission called ezimplot3D that seems to do the job also, as #knedlsepp suggests.
Implot2 and implot from Matlab Central appear to do the job.
There are two new functions to plot implicit function in R2016b:
fimplicit for f(x,y) = 0
fimplicit3 for f(x,y,z) = 0
This is my primer time using matlab especially plotting.
I try to plot the solutions for y = x^2 − x − 2 like in here
But I don't know how to model this plot in matlab (it makes sense because the solutions must be computed first) but shouldn't the shape of curve will remain the same.
I tried:
clc;
close all;
%y = x2 − x − 2
x = [-3:1:3];
y= x.^2 - x -2;
plot(y), grid on;
But the curve I got is totally different.
You're choosing wrong points for drawing the curve.
The image that you showed above takes the values of x in the interval [-2,3]. Fixing this gives exactly the same curve as that of the one in the question.
Also note that the minima of your function is at x=0.5. So, if x has equal number of values less than 0.5 and greater than 0.5, you will get the curve of the desired shape.
Code:
x = [-2:0.01:3]; %Choosing 0.01, because more the points, more the curve will be accurate
y= x.^2 - x -2;
plot(x,y), grid on;
Output:
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');
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
I wish to plot implicit functions in MATLAB. Like x^3 + xy + y^2 = 36 , equations which cannot be made into simple parametric form. Is there any simple method ?
Here are a couple of options...
Using ezplot (or fplot recommended in newer versions):
The easiest solution is to use the function ezplot:
ezplot('x.^3 + x.*y + y.^2 - 36', [-10 10 -10 10]);
Which gives you the following plot:
Using contour:
Another option is to generate a set of points where you will evaluate the function f(x,y) = x^3 + x*y + y^2 and then use the function contour to plot contour lines where f(x,y) is equal to 36:
[x, y] = meshgrid(-10:0.1:10); % Create a mesh of x and y points
f = x.^3+x.*y+y.^2; % Evaluate f at those points
contour(x, y, f, [36 36], 'b'); % Generate the contour plot
xlabel('x'); % Add an x label
ylabel('y'); % Add a y label
title('x^3 + x y + y^2 = 36'); % Add a title
The above will give you a plot nearly identical to the one generated by ezplot:
In case you want to plot an implicit surface, for example a Horned cube, you can do something like the following.
The idea is to calculate all values of the function (even if they don't equal to zero) and then create an isosurface that will define your equality. In this example the implicit function equals to zero.
fun=#(x,y,z)(1-x.^8-3.*y.^8-2.*z.^8+5.*x.^4.*z.^2.*y.^2+3.*y.^4.*x.^2.*z.^2) ;
[X,Y,Z]=meshgrid(-2:0.1:2,-2:0.1:2,-2:0.1:2);
val=fun(X,Y,Z);
fv=isosurface(X,Y,Z,val,0);
p = patch(fv);
isonormals(X,Y,Z,val,p)
set(p,'FaceColor' , 'red');
set(p,'EdgeColor' , 'none');
daspect([1,1,1])
view(3); axis tight
camlight
lighting phong
axis off
Additionally there is a Matlab File Exchange submission called ezimplot3D that seems to do the job also, as #knedlsepp suggests.
Implot2 and implot from Matlab Central appear to do the job.
There are two new functions to plot implicit function in R2016b:
fimplicit for f(x,y) = 0
fimplicit3 for f(x,y,z) = 0