ndgrid and interpn - matlab - matlab

I have 4 grids:
kgrid which is [77x1];
x which is [15x1];
z which is [9x1];
s which is [2x1];
Then I have a function V which is:
V [77x15x9x2]
I am trying to interpolate V at some kprime points. To do so, I am doing:
[ks, xs, zs, ss] = ndgrid(kgrid, x, z, s);
Vprime = interpn(xs, ks, zs, ss, V, xs, kprime, zs, ss, 'spline');
where kprime is a [77x15x9x2].
All the matrices needed (kgrid, x, z, s, V and kprime) can be found here: http://www.filedropper.com/grids
However I am getting this error when using
Error using griddedInterpolant
Data is not valid NDGRID format.
Error in interpn (line 149)
F = griddedInterpolant(X{:}, V, method,extrap);
Any clue on what could be the issue?

The order of the inputs to interpn need to be the same order as the outputs of your ndgrid call. You have flipped ks and xs.
vprime = interpn(ks, xs, zs, ss, V, kprime, xs, zs, ss);

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

mtaylor MuPAD - Matlab

I am trying to run the function mtaylor from the MuPAD engine in MatLab, which provides a multivariate Taylor expansion of a function. However, it keeps telling me I am trying to expand around an invalid point. Here is a minimal working example of what I tried:
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), [x, y], 4)
Error message:
vError using mupadengine/feval (line 157)
MuPAD error: Error: Invalid expansion point. [mtaylor]
Why doesn't this work?
The reason this works with MuPAD's mtaylor
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), x, 4) % [x] is fine too
and this doesn't
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), [x, y], 4)
is that the [x, y] argument is seen as a single symbolic vector argument/variable rather than a list of variables for the expansion. Your expression, exp(x^2 - y), is not in terms of vector variables, but simple scalars, x and y.
The workaround is to pass the list as a string:
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), '[x, y]', 4)
or
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), ['[' char(x) ',' char(y) ']'], 4)
or use evalin to write the MuPAD command a single string as suggested by #Daniel in the comments:
syms x y;
evalin(symengine,'mtaylor(exp(x^2 - y), [x, y], 4)')
Arrays and Matrices vs. Lists in MuPAD
For further clarification, arrays of symbolic variables in Matlab correspond to the MuPAD array type, which can be created via feval(symengine,'array','1..1','1..2','[x,y]'). More specifically, they are of type Dom::Matrix(), which can be created via V=feval(symengine,'Dom::Matrix()','[x,y]') or just syms x y; V=[x,y].
The mtaylor function requires a list input, which can be created via L=evalin(symengine,'[x,y]'). Thus
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), V, 4)
will produce the same error as in your question, but
syms x y;
feval(symengine,'mtaylor',exp(x^2 - y), L, 4)
will work correctly. Unfortunately, L and V appear identical from within Matlab, but you can use MuPAD's domtype function to differentiate them:
feval(symengine,'domtype',V)
feval(symengine,'domtype',L)
which returns Dom::Matrix() and DOM_LIST.

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

MATLAB: Apply input vector to a vector of functions

Say I have the following definitions:
syms x y z
F = [x^2+y^2+z^2-1; 2*x^2+y^2-4*z; 3*x^2-4*y+z^2];
v = [x, y, z];
I want to evaluate F(x=1, y=2, z=3), meaning apply the stored functions to the input.
Alternatively, I could use function handlers for every member of F, but then I couldn't easily calculate the jacobian:
J = jacobian(F, v)
How can it be done?
To evaluate F for some specific values you can use
subs(F,{x y z},{1 2 3})

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