Contour plot for 3D vector - matlab

I am trying to produce a contour plot for the 3D vectors returned by a custom function in the xy plane where z = 0.
I tried this but I just get an empty graph:
% Stand in for the real function I want to plot.
f = #(x, y, z) [x ^ 2, y ^ 2, x * y + z];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y), 3);
% I know this can be vectorized but the function I really want to graph can't
% be.
for i = 1:length(x)
for j = 1:length(y)
z(i, j, :) = f(x(i), y(j), 0);
end
end
figure;
axis equal;
contour(x, y, z);

You should mention what your axis will be. You have x,y and 3 outputs from f.
If you consider 3 outputs of your f as the ones to be plotted then you should use,
contour(z(:,:,1),z(:,:,2),z(:,:,3));
Which will give you this,
I think what you are looking for, is a function with one output, like,
f = #(x, y,z) [x ^ 2 + y ^ 2 + x * y + z ];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y));
for i = 1:length(x)
for j = 1:length(y)
z(i, j) = f(x(i), y(j),0);
end
end
contour(x,y,z,20);

Related

Plotting Problems on Matlab How could I draw?

I am trying to plot level curve of f(x,y) = y^4 - 2xy^2 + x^3 - x over [-5,4] x [-3,3], but I am stuck. This my code and how should I continue to it?
My Code:
x = linspace(-5, 4, 25);
y = linspace(-3, 3, 25);
[X,Y] = meshgrid(x,y);
z = #(x,y) y.^4-2*x*y.^2+x^3-x;
Z = z(X,Y);
surf(X,Y,Z)
You could do something like:
clear
x = linspace(-5, 4, 25);
y = linspace(-3, 3, 25);
[X,Y] = meshgrid(x,y);
z = #(x,y) y.^4-2.*x.*y.^2+x.^3-x;
Z = z(X,Y);
% Probing some line parallel to the x axis:
y_probe = 0;
% Probing a point to calculate the gradient in:
x_probe = -3;
% Getting all points for which x = x_probe:
z_probe = z(x, y_probe);
% Compute the gradient:
dx = gradient(x);
dz = gradient(z_probe);
dzdx = dz./dx;
clf
subplot(121)
surf(X,Y,Z)
hold on
plot3(x, ones(1,length(y)) .* y_probe, z_probe ,'r','linewidth',2)
subplot(122)
plot(x, z_probe,'r','linewidth',1)
hold on
xi = find(x > x_probe, 1);
plot(x, dzdx(xi) .* (x - x(xi)) + z_probe(xi),'k--','linewidth',1);
h = quiver(x(xi), z_probe(xi), -dx(xi), dz(xi),'k','LineWidth',2);
xlim([min(x) max(x)])
ylim([min(z_probe) max(z_probe)])
To get:

Plotting x,y,z with intervals

How do I plot xyz In rectangular, polar, and 3-D?
for x = 0 to 35pi:
Y = x*sin(x)
Z = x*cos(x)
Using the the intervals of X which provides very smooth plots . Create three plots including tittle and labels .
This is the input I have put in so far. I'm not sure if this is correct:
x = pi*linspace(0,35);
y = pi*x,sin(pi*x);
z = pi*x,cos(pi*x);
plot3(x,y,z)
title('data analysis')
xlabel('independent x')
ylabel('dependent y')
zlabel('z')
I believe this solves the problem as you describe it:
x = linspace(0, 35*pi, 10000);
y = x .* sin(x);
z = x .* cos(x);
plot3(x, y, z);
title('data analysis');
xlabel('independent x');
ylabel('dependent y');
zlabel('z');

Plotting four variables u = f(x, y, z) with ranges of x, y, z in Matlab

I'm trying to plot four variables (x, y, z, u) in a 3D space.
Below I defined u = f(x,y,z) = (x-y)/z and ranges for x, y and z.
Then I used the scatter3 function for plotting.
But the linspace command only returned
u1 = f(x1, y1, z1)
u2 = f(x2, y2, z2)
...
I wanted to plot my variables so that I would get values of u for all x, y and z, so as well as u_i = f(x_i, y_i, z_i), I want:
u112 = f(x1, y1, z2)
u121 = f(x1, y2, z1)
...
Which function do I need to use for this?
Full code:
z = linspace(1,2,50);
y = linspace(0,0.5,50);
x = linspace(-1,1,50);
f = (x-y)./z;
scatter3(x,y,z,5,f,'filled')
for i=1:50
if f(i)<0
scatter3(x,y,z,5,f,'r','filled');
else
scatter3(x,y,z,5,f,'b','filled');
end
end
You need to pass your x, y, and z vectors to meshgrid to have it generate points to fill the whole 3-D volume:
z = linspace(1, 2, 50);
y = linspace(0, 0.5, 50);
x = linspace(-1, 1, 50);
[X, Y, Z] = meshgrid(x, y, z);
f = (X-Y)./Z;
These will be 50-by-50-by-50 matrices. To plot them with scatter3 you will need to reshape them into column vectors using the colon operator:
scatter3(X(:), Y(:), Z(:), 5, f(:), 'filled');
If you'd like to plot negative values of f as red and positive values as blue, you can call scatter3 like so and add a jet colormap (no loops necessary):
scatter3(X(:), Y(:), Z(:), 5, (f(:) < 0), 'filled');
colormap(jet);

How can I plot a surface generated by a function f(x,y) where the values x and y are subject to constraints?

I'm new to Matlab and I have a function
and
How can I plot:
Define your X and Y in a linear array
X = linspace(-2, 2, 1000);
Y = linspace(-2, 2, 1000);
Mesh them so you have a grid of x and y
[x,y] = meshgrid(X,Y);
Get the value for your function
f = sqrt(x.^2 + y.^2);
Define your domain
D = (1 <= x.^2 + y.^2);
Set everything outside your domain to nan's so it won't plot
f(~D) = nan;
Plot the surface
surf(x,y,f, 'linestyle', 'none')

How to use plot3 restrict to a certain region in Matlab?

For example, I want to plot the function
f(x,y) =sin(x^2+y^2)/(x^2+y^2), x^2+y^2 <=4π
In Mathematica, I can do it as following:
Plot3D[Sin[x^2 + y^2]/(x^2 + y^2), {x, -4, 4}, {y, -4, 4},
RegionFunction -> (#1^2 + #2^2 <= 4 Pi &)]
Where the RegionFunction specified the region of x,y to plot.
Here's a not particularly elegant solution that sets the function values of the region you don't want to see to -infinity.
[x, y] = meshgrid(-4:0.1:4, -4:0.1:4);
z = sin(x.^2+y.^2)./(x.^2+y.^2);
idx = x.^2 + y.^2 > 4*pi;
z(idx) = -Inf;
surf(x, y, z); axis vis3d;
Edit. Actually, if you try a finer grid (say -4:0.01:4) and add shading interp it doesn't look too bad.
A slight variation to 3lectrologos's solution, with emphasis on keeping what you want:
x = -4*pi:0.01:4*pi;
y = -4*pi:0.01:4*pi;
[X,Y] = meshgrid(x,y);
Clean = (X.^2 + Y.^2)<=4*pi;
Y = Y.*Clean;
X = X.*Clean;
X(~any(X,2),:) = [];
X(:, ~any(X,1)) = [];
Y(~any(Y,2),:) = [];
Y(:, ~any(Y,1)) = [];
F = sin(X.^2+Y.^2)./(X.^2+Y.^2);
mesh(X,Y,F)
Note that in this case, you need to make sure that (0,0) is in your solution profile.
Edit: compressing matrices for easier plotting