Plotting four variables u = f(x, y, z) with ranges of x, y, z in Matlab - 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);

Related

How to obtain slice plots from x,y,z,v data?

I would like to use Slice() function for obtaining figures like this , following the code:
[x, y, z] = meshgrid(-3:0.25:3);
v = x.*exp(-x.^2 - y.^2 - z.^2);
slice(x, y, z, v, [], 0, 0);
colorbar;
Nevertheless, my value v is not a function of my x,y,z meshgrid, my v value is an scalar independent of x,y,z coordinates.
Is there any way of obtaining a v value on the space, knowing only x,y,z coordinates + a v value associated to those coordinates?
Try griddata
xr = linspace( min(x),max(x),10 );
yr = linspace( min(y),max(y),10 );
zr = linspace( min(z),max(z),10 );
[X,Y,Z] = meshgrid(xr,yr,zr);
V = griddata(x,y,z,v,X,Y,Z);
slice(X,Y,Z,V,[],0,0)

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');

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

Contour plot for 3D vector

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

3D Spline Interpolation Matlab

I have two 3D arrays:
A=[
12751 4616 15915
15864 4622 15667
12877 4683 21050
15816 4668 21253
11374 5006 18495
16995 5466 18493
11638 4880 20023
17078 4938 20006
11576 4886 17011
];
and
B=[
12402 2138 15743
10285 3175 15851
10237 3084 21052
12130 2129 21299
8074 3802 18505
14515 1623 18497
8415 3713 19856
14462 1120 20061
8340 3711 17145
14483 1157 16990];
and I want to do spline or 3D interpolation between them using Interp3 in Matlab.How should I define V in VI = interp3(X,Y,Z,V,XI,YI,ZI)?
Or Y in yy = spline(x,Y,xx).
I don't quite understand your data, is B a function of A (or visa versa)? Also, those arrays appear to be 1D, not 3D. Can you clarify this?
In your function call
yy = spline(x, Y, xx)
Y is the dependent variable which you are interpolating: Y is a function of x and the result of the above function call is to return the value of Y at xx. As an example in one dimension, try
x = linspace(0., 2.*pi, 100);
Y = sin(x);
% What is the value of Y (or sin(x)) at pi/2?
xx = pi/2.;
yy = spline(x, Y, xx); % This should result in yy = 1.000
Check out the spline documentation for more information and examples of using this function.
Now this function is only for 1D fitting, and is (I presume) equivalent to yy = interp1(x, Y, xx, 'spline'). If you want to do a three dimensional lookup, you'll have to use interp3, which generalises the above example to 3D. So rather than just one independent coordinate x, we have two more, y, and z and three coordinates for the point at which we want to perform the look up: xx, yy and zz. The function you are interpolating must be a 3D function of the coordinates (x, y, z). Try, as an example:
x = linspace(-1., 1., 100); y = x; z = x;
[X, Y, Z] = meshgrid(x, y, z);
s = exp(-sqrt(X.^2 + Y.^2 + Z.^2));
sinterp = interp3(x, y, z, s, 0., 0., 0.) % Should give sinterp = 0.9827