Plotting 3-d graphs and level curves in Matlab - matlab

I have the following code below, but I cannot test it since I do not have Matlab with me right now and I am afraid I might not have the time to test it by myself when I finally get it. I'm trying to plot both 3-d graphs and graphs of the level curves in the y and x axis (two dimensions only) of three different types of functions. I would appreciate if someone could point if there is something wrong with the code below.
**************************************************************
**plotting functions -- level curves and 3d graph**
x_val = linspace(0, 100, 200);
y_val = linspace(0, 100, 200);
[x, y] = meshgrid(x_val, y_val);
z = ln(x).+y.;
figure
contour3(y, x, z)
contour(y, x, z)
********************************
z = (x.^1/2)+y.;
figure
contour3(y, x, z)
contour(y, x, z)
*********************************
z = (x.^1/3)+y.;
figure
contour3(y, x, z)
contour(y, x, z)

ln is not a valid matlab symbol, in addition to the excess of dots mentioned and comment formatting above. The following runs on Matlab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%plotting functions -- level curves and 3d graph%%
x_val = linspace(0, 100, 200);
y_val = linspace(0, 100, 200);
[x, y] = meshgrid(x_val, y_val);
z = log(x)+y;
figure
contour3(y, x, z)
figure
contour(y, x, z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = (x.^1/2)+y;
figure
contour3(y, x, z)
figure
contour(y, x, z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = (x^1/3)+y;
figure
contour3(y, x, z)
figure
contour(y, x, z)

For starters, comments in MATLAB are "%" not "*".
You have a few mistakes, trying to do element-wise operators, I think.
Your three assignments of z have too many dots:
z = log(x)+y;
z = (x.^1/2)+y;
z = (x.^1/3)+y;
It is not necessary to use ".+", because MATLAB automatically adds matrices elementwise.

Related

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

Error in mesh(X,Y,Z) in matlab

This is my code:
load mwe16_2.dat;
x = mwe16_2(:,1); % x contains the 1st column(500 values) of ‘mwe16_2’
y = mwe16_2(:,2); % y contains the 2nd column (500 values)of ‘mwe16_2’
z = mwe16_2(:,3); % z contains the 3rd column(500 values) of ‘mwe16_2’
[X, Y, Z] = meshgrid (x, y, z);
mesh (X, Y, Z)
While running the code it is showing the error:
*Error in plot3_d (line 13) mesh(X,Y,Z) in Matlab*
Can someone say the reason for the error and how to correct it?
[X,Y,Z] = meshgrid(x,y,z) produces three 3D arrays from x, y and z vectors. Now if you take a look at mesh you will see that you are not providing this function with proper input.
If you want to illustrate the set of points defined by three vectors of x, y and z, you can consider using scatter3:
figure; scatter3(x, y, z)

Retain meshgrid structure when subsetting a meshgrid

When I subset an array constructed from meshgrid, I cannot work out how to keep its meshgrid structure. Thus, you cannot use it in a call to mesh or surface. I will demonstrate this in my example of constructing the unit sphere.
Possible alternate titles for this question:
How do you make the top half of the meshgrid sphere from scratch in Matlab?
How do you use mesh to plot a subset of a meshgrid?
This is motivated using the following toy example of constructing a sphere of unit radius in Matlab from scratch, so that it is like the one generated by:
[x, y, z] = sphere(100)
mesh(x, y, z)
Using the equation for a sphere:
Define a meshgrid and z to be:
x = linspace(-1, 1, 201);
y = linspace(-1, 1, 201);
[x, y] = meshgrid(x, y);
z = sqrt(1 - x.^2 - y.^2);
So far so good, except z takes imaginary values where the sphere does not exist over the xy-plane, that is, anywhere outside of the unit circle.
A call to mesh now returns an error:
>> mesh(x, y, z)
Error using mesh (line 58)
X, Y, Z, and C cannot be complex.
Thus, a logical step is to remove all complex values:
% get logical vector index where real z is
LI = z == real(z)
x = x(LI)
y = y(LI)
z = z(LI)
But now x, y, and z are no longer 3d arrays, and calling mesh gives another error:
>> mesh(x, y, z)
Error using mesh (line 58)
Z must be a matrix, not a scalar or vector.
So, in general I have no idea how to preserve the meshgrid structure when subsetting the data. Hence, I can't generate the top half of this sphere from scratch.
In general, you can "exclude" values from plotting while maintaining the matrix structure by using NaN value. In your case, try this:
LI = z == real(z);
z(~LI) = NaN;
mesh(x,y,z);

First, second and third derivative of a vector function

I can define the following vector function
f(x, y, z) = [x2 - 1, x3 + y2, z]
In MatLab or in Maple.
I want to find (and evaluate) the first, second and third derivatives of it. How to find: ∇f, ∇2f and ∇3f? Here vector function f is differentiated with respect to vector (x, y, z). I can do the simple job of finding ∇f with Maple as follows:
f(x, y, z) = [x2 - 1, x3 + y2, z]
∇f = Jacobian(f, [x, y, z])
But how to differentiate ∇f?
Is there a function in MatLab (or in Maple) which may take the above function as an input and evaluate its derivatives for a given value of (x, y, z)?

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