First, second and third derivative of a vector function - matlab

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

Related

Z must be a matrix, not a scalaqr or vector, matlab

I am trying to make a 3d plot but i am getting an error and im not sure how to solve it. I know that there are other questions out there similar to mine but i tried some of them and it did not work.
fh = sin(x)*cos(y).^3 + 2*cos(x).^5*sin(y)
[X,Y] = meshgrid(1:0.5:10,1:20);
surf(X,Y,fh)
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.
The Z data in this case is what you are passing to surf as fh. It looks like fh is the function you want to use to compute Z, but you need to use the gridded values you generated for X and Y to evaluate it. As your code is now, it is evaluating the function using x and y (case matters!), which you haven't defined for us. Try this instead:
[X, Y] = meshgrid(1:0.5:10, 1:20);
Z = sin(X).*cos(Y).^3 + 2.*cos(X).^5.*sin(Y);
surf(X, Y, Z);
Notice that I used the .* operator (element-wise multiplication) instead of the * operator (matrix multiplication) in the equation.
You could also do this by defining an anonymous function that evaluates the formula for a given set of data:
fh = #(x, y) sin(x).*cos(y).^3 + 2.*cos(x).^5.*sin(y);
[X, Y] = meshgrid(1:0.5:10, 1:20);
surf(X, Y, fh(X, Y));

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)

Plotting 3-d graphs and level curves in 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.

quad for multivariable functions

I have a function f(x, y, z, t) which has 4 different variables, and I want to find the numerical integration of it with quad in case of just one variable:
quad(f(x,y,z,t), x, 0, inf) %// numerical integration in case of x
Is it possible? I assume I need symbolic result. Do you have any other idea?
If you want to "fix" y, z and t to some parameters, say y0, z0 and t0 and integrate the function h(x) = f(x, y0, z0, t0) you can use annonymuous function:
quad( #(x) f( x, y0, z0, t0 ), x, 0, inf );
EDIT:
To get a matlab function that depends on y z and t you can do:
function area = marginalizeX( y0, z0, t0 )
% integrate f(x,y,z,t) over x while fixing y0, z0, and t0
area = quad( #(x) f( x, y0, z0, t0 ), x, 0, inf );