I have a function f(x1,x2) = x1*x2. That is easy enough to plot using ezsurf.
Can I plot f(x1,x2) = x1*x2 = 1? I want to see what specific sets of solutions look like.
Try using contour:
z = f(x1, x2);
contour(x1, x2, z, [1 1]);
Related
This is my code:
syms x y;
f= x^2/(y-y^2);
ezcontour(f,[-1,1],[0.1,0.9]);
How can i show the labels? I wan to show something like this:
Thank You very much!
Using contour :
x = [-1:0.01:1];
y = [0.1:0.01:0.9];
[X, Y] = meshgrid(x,y);
f= X.^2./(Y-Y.^2);
[C, h] = contour(f);
clabel(C, h);
clabel wants as an input the contour matrix being displayed by the Contour object. While ezcontour doesn't return the matrix like contour does, the Contour object has a 'ContourMatrix' property. If you specify an output for ezcontour it will return the handle to the plotted contour that can be queried directly.
For example:
f = #(x, y) x.^2/(y-y.^2);
h = ezcontour(f, [-1, 1], [0.1, 0.9]);
C = h.ContourMatrix; % R2014b or newer
% C = get(h, 'ContourMatrix'); % R2014a and older
clabel(C, h);
Returns the desired output:
Alternatively, you can can just pass the handle to the contour to obtain the same result:
clabel([], h);
Per the documentation:
If you do not have the contour matrix C, then replace C with [].
I have this function of 2 variables:
match = (2.*x-1).*y.^(.1) - 2.*x.*y;
Plot using ezplot(match, [0 1], [0 1]) yields:
I would like to shade (fill) the area under this green curve all the way to the 2 axis. I have tried to get the x and y data via get(h,'XData') and get(h,'YData'), and then use
area(x,y)
but the plot is not correct. Any suggestions?
Another option:
Assuming that you have a match() function:
function z = match(x,y)
z = (2.*x-1).*y.^(.1) - 2.*x.*y;
end
And you use ezplot():
fig = ezplot('match', [0 1], [0 1]);
The following code extract the data from the figure and plot the area:
h1 = findall(fig,'Type','hggroup');
matrix = get(h1,'ContourMatrix');
xData = matrix(1,3:end);
yData = matrix(2,3:end);
area(xData,yData)
xlim([0 1])
ylim([0 1])
The key point is that ezplot() uses a contour object, therefore it is a little more trickier to get the data.
OK, a rather inefficient solution is by using the symbolic toolbox:
syms y
match = #(x) (2.*x-1).*y.^(.1) - 2.*x.*y;
n = 100;
xs = linspace(0,1,n);
yval = zeros(n,2);
for ii=1:n
x=xs(ii);
temp = solve(match(x),'y', 'Real', true);
if length(temp)==1
yval(ii,1) = temp;
elseif length(temp)==2
yval(ii,:) = temp;
end
end
area(xs,yval(2))
Mind that the first solution is always '0'. I don't know if performance is an issue but this might be working for you!
I've managed to plot on normal graph using the plot function in matlab. I then generated a symbolic function and I managed to plot that using ezplot. I would like to plot the two graphs into one set of x,y axis. I'm not sure how to do this... Here is the code:
a = [50; 100; 150;200;250;300;350];
b = [56;23;22;18;14;15;21];
plot(a,b);
y = polyfit(a,b,2);
syms x;
f = y(1)*x^2 + y(2)*x + y(3);
g = diff(f);
u = solve(g);
subplot(2, 2, 2);
ezplot(f);
Are you familiar with the hold command?
hold command
How can I draw i.e. x = 5 line in plot at Matlab?
I plot like that:
x = (-10:.1:10);
f= 10;
plot(x, f, 'r');
of course it doesn't work. For every variable of x, y is equal to 0 except for x=10. When x = 10 y equals to everything. How to plot this?
In MATLAB, plot(X, Y) simply draws points on the graph (and connects them with lines). Note that in this form of syntax, X and Y must have the same dimensions. Therefore, to plot the line x = 5 create a vector of your desired y-coordinates, and then create matching x-coordinates, which are all equal to 5:
Y = -10:0.1:10;
X = 5 * ones(size(Y));
plot(X, Y);
A useful function from the FileExchange in hline and vline.
You could also achieve this by plotting only 2 points:
f = 5;
plot([-10 10] , [1 1]*f);
I think using line is more straightforward here than plot.
x = [-10, 10];
f = ones(size(x));
f = 5 .* f;
line(x, f);
How do you plot a user-defined function of two variables in Matlab?
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
Z = my_func(X,Y);
surf(X,Y,Z);
Alternatively, if your function is not vectorized,
X, Y = meshgrid(xs, ys); % values of x and y at which we want to evaluate
for x = 1:length(xs)
for y = 1:length(ys)
Z(x,y) = my_func(X(x,y), Y(x,y));
end
end
Z = my_func(X,Y);
surf(X,Y,Z);
ezsurf is a simple solution, or ezmesh, or ezcontour, or ezsurfc, or ezmeshc.
It has many types.
You can go plot gallery and select your variable and then types like mesh, 3D, surface, ...